{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "# ClearML - 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": [
      "ClearML Task: overwriting (reusing) task id=6de40029e54c41d7a1a24a1f2dc9cad2\n",
      "ClearML results page: https://demoapp.clearml.allegro.ai/projects/087f765c846c4c76a7e9f3d035667d82/experiments/6de40029e54c41d7a1a24a1f2dc9cad2/output/log\n"
     ]
    }
   ],
   "source": [
    "# Connecting ClearML\n",
    "from clearml import Task\n",
    "task = Task.init(project_name = 'examples', task_name = 'notebook example')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57740 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 10384 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 5780 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": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Notice, Updating task_params is traced and updated in ClearML\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": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W1028 20:45:45.150056 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
      "\n",
      "W1028 20:45:45.166742 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
      "\n",
      "W1028 20:45:45.170039 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n",
      "\n",
      "W1028 20:45:45.228762 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n",
      "\n",
      "W1028 20:45:45.236253 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3295: The name tf.log is deprecated. Please use tf.math.log instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60000 train samples\n",
      "10000 test samples\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"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W1028 20:45:46.286724 139687276058368 deprecation.py:323] From /usr/local/lib/python3.7/site-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
      "W1028 20:45:46.357379 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:986: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.\n",
      "\n",
      "W1028 20:45:46.554848 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/callbacks.py:796: The name tf.summary.histogram is deprecated. Please use tf.compat.v1.summary.histogram instead.\n",
      "\n",
      "W1028 20:45:46.574680 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/callbacks.py:850: The name tf.summary.merge_all is deprecated. Please use tf.compat.v1.summary.merge_all instead.\n",
      "\n",
      "W1028 20:45:46.577096 139687276058368 deprecation_wrapper.py:119] From /usr/local/lib/python3.7/site-packages/keras/callbacks.py:853: The name tf.summary.FileWriter is deprecated. Please use tf.compat.v1.summary.FileWriter instead.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/6\n",
      "60000/60000 [==============================] - 4s 74us/step - loss: 0.2136 - acc: 0.9347 - val_loss: 0.1043 - val_acc: 0.9666\n",
      "Epoch 2/6\n",
      "60000/60000 [==============================] - 5s 76us/step - loss: 0.0811 - acc: 0.9751 - val_loss: 0.0691 - val_acc: 0.9772\n",
      "Epoch 3/6\n",
      "60000/60000 [==============================] - 5s 85us/step - loss: 0.0538 - acc: 0.9833 - val_loss: 0.0702 - val_acc: 0.9789\n",
      "Epoch 4/6\n",
      "60000/60000 [==============================] - 5s 82us/step - loss: 0.0385 - acc: 0.9880 - val_loss: 0.0711 - val_acc: 0.9807\n",
      "Epoch 5/6\n",
      "60000/60000 [==============================] - 5s 76us/step - loss: 0.0300 - acc: 0.9905 - val_loss: 0.0846 - val_acc: 0.9788\n",
      "Epoch 6/6\n",
      "60000/60000 [==============================] - 5s 75us/step - loss: 0.0227 - acc: 0.9931 - val_loss: 0.0782 - val_acc: 0.9814\n",
      "Test score: 0.07817659145611801\n",
      "Test accuracy: 0.9814\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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.0"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
