{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.5/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n",
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "# Trains - Example of integrating plots and training on jupyter notebook. \n",
    "# In this example, simple graphs are shown, then an MNIST classifier is trained using Keras.\n",
    "\n",
    "from keras.callbacks import TensorBoard, ModelCheckpoint\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers.core import Dense, Dropout, Activation\n",
    "from keras.optimizers import SGD, Adam, RMSprop\n",
    "from keras.utils import np_utils\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TRAINS Task: overwriting (reusing) task id=8d23de406d0a4159a496b64c7eba0e32\n",
      "======> WARNING! UNCOMMITTED CHANGES IN REPOSITORY https://github.com/allegroai/trains.git <======\n",
      "TRAINS results page: https://demoapp.trainsai.io/projects/087f765c846c4c76a7e9f3d035667d82/experiments/8d23de406d0a4159a496b64c7eba0e32/output/log\n"
     ]
    }
   ],
   "source": [
    "# Connecting TRAINS\n",
    "from trains import Task\n",
    "task = Task.init(project_name = 'examples', task_name = 'notebook example')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Set script parameters\n",
    "task_params = {'num_scatter_samples': 60, 'sin_max_value': 20, 'sin_steps': 30}\n",
    "task_params = task.connect(task_params)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simple plots. You can view the plots in experiments results page  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57780 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 10432 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 5792 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "N = task_params['num_scatter_samples']\n",
    "x = np.random.rand(N)\n",
    "y = np.random.rand(N)\n",
    "colors = np.random.rand(N)\n",
    "area = (50 * np.random.rand(N))**2  # 0 to 15 point radii\n",
    "plt.scatter(x, y, s=area, c=colors, alpha=0.5)\n",
    "plt.title('Nice Circles')\n",
    "plt.show()\n",
    "\n",
    "x = np.linspace(0, task_params['sin_max_value'], task_params['sin_steps'])\n",
    "y = np.sin(x)\n",
    "plt.plot(x, y, 'o', color='black')\n",
    "plt.title('Sinus Dots')\n",
    "plt.show()\n",
    "\n",
    "m = np.eye(32, 32, dtype=np.uint8)\n",
    "plt.imshow(m)\n",
    "plt.title('sample output')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Keras training example\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Notice, Updating task_params is traced and updated in TRAINS\n",
    "task_params['batch_size'] = 128\n",
    "task_params['nb_classes'] = 10\n",
    "task_params['nb_epoch'] = 6\n",
    "task_params['hidden_dim'] = 512\n",
    "batch_size = task_params['batch_size']\n",
    "nb_classes = task_params['nb_classes']\n",
    "nb_epoch = task_params['nb_epoch']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60000 train samples\n",
      "10000 test samples\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_1 (Dense)              (None, 512)               401920    \n",
      "_________________________________________________________________\n",
      "activation_1 (Activation)    (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 512)               262656    \n",
      "_________________________________________________________________\n",
      "activation_2 (Activation)    (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 10)                5130      \n",
      "_________________________________________________________________\n",
      "activation_3 (Activation)    (None, 10)                0         \n",
      "=================================================================\n",
      "Total params: 669,706\n",
      "Trainable params: 669,706\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/6\n",
      "60000/60000 [==============================] - 4s 64us/step - loss: 0.2129 - acc: 0.9350 - val_loss: 0.1012 - val_acc: 0.9682\n",
      "Epoch 2/6\n",
      "60000/60000 [==============================] - 4s 68us/step - loss: 0.0813 - acc: 0.9752 - val_loss: 0.0684 - val_acc: 0.9779\n",
      "Epoch 3/6\n",
      "60000/60000 [==============================] - 4s 62us/step - loss: 0.0540 - acc: 0.9830 - val_loss: 0.0736 - val_acc: 0.9793\n",
      "Epoch 4/6\n",
      "60000/60000 [==============================] - 4s 64us/step - loss: 0.0387 - acc: 0.9880 - val_loss: 0.0859 - val_acc: 0.9761\n",
      "Epoch 5/6\n",
      "60000/60000 [==============================] - 4s 63us/step - loss: 0.0304 - acc: 0.9904 - val_loss: 0.0875 - val_acc: 0.9766\n",
      "Epoch 6/6\n",
      "60000/60000 [==============================] - 4s 64us/step - loss: 0.0220 - acc: 0.9933 - val_loss: 0.0847 - val_acc: 0.9793\n",
      "Test score: 0.08471047468512916\n",
      "Test accuracy: 0.9793\n"
     ]
    }
   ],
   "source": [
    "# the data, shuffled and split between train and test sets\n",
    "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
    "\n",
    "X_train = X_train.reshape(60000, 784)\n",
    "X_test = X_test.reshape(10000, 784)\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "X_train /= 255.\n",
    "X_test /= 255.\n",
    "print(X_train.shape[0], 'train samples')\n",
    "print(X_test.shape[0], 'test samples')\n",
    "\n",
    "# convert class vectors to binary class matrices\n",
    "Y_train = np_utils.to_categorical(y_train, nb_classes)\n",
    "Y_test = np_utils.to_categorical(y_test, nb_classes)\n",
    "\n",
    "hidden_dim = task_params['hidden_dim']\n",
    "model = Sequential()\n",
    "model.add(Dense(hidden_dim, input_shape=(784,)))\n",
    "model.add(Activation('relu'))\n",
    "# model.add(Dropout(0.2))\n",
    "model.add(Dense(hidden_dim))\n",
    "model.add(Activation('relu'))\n",
    "# model.add(Dropout(0.2))\n",
    "model.add(Dense(10))\n",
    "model.add(Activation('softmax'))\n",
    "\n",
    "model.summary()\n",
    "\n",
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer=RMSprop(),\n",
    "              metrics=['accuracy'])\n",
    "\n",
    "board = TensorBoard(histogram_freq=1, log_dir='/tmp/histogram_example')\n",
    "model_store = ModelCheckpoint(filepath='/tmp/weight.{epoch}.hdf5')\n",
    "\n",
    "model.fit(X_train, Y_train,\n",
    "                    batch_size=batch_size, epochs=nb_epoch,\n",
    "                    callbacks=[board, model_store],\n",
    "                    verbose=1, validation_data=(X_test, Y_test))\n",
    "score = model.evaluate(X_test, Y_test, verbose=0)\n",
    "print('Test score:', score[0])\n",
    "print('Test accuracy:', score[1])\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
