{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "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",
    "import os\n",
    "import tempfile\n",
    "\n",
    "from tensorflow.keras import utils as np_utils\n",
    "from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard\n",
    "from tensorflow.keras.datasets import mnist\n",
    "from tensorflow.keras.layers import Activation, Dense\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.optimizers import RMSprop\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: created new task id=2f9f2f08fa90427aa51e34b839e49fb6\n",
      "ClearML results page: https://demoapp.clearml.allegro.ai/projects/0e152d03acf94ae4bb1f3787e293a9f5/experiments/2f9f2f08fa90427aa51e34b839e49fb6/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": "stdout",
     "output_type": "stream",
     "text": [
      "60000 train samples\n",
      "10000 test samples\n",
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense (Dense)                (None, 512)               401920    \n",
      "_________________________________________________________________\n",
      "activation (Activation)      (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 512)               262656    \n",
      "_________________________________________________________________\n",
      "activation_1 (Activation)    (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 10)                5130      \n",
      "_________________________________________________________________\n",
      "activation_2 (Activation)    (None, 10)                0         \n",
      "=================================================================\n",
      "Total params: 669,706\n",
      "Trainable params: 669,706\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "Train on 60000 samples, validate on 10000 samples\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W0615 20:51:48.301550 139739992581888 ag_logging.py:146] Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x7f174c2f7a60> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/6\n",
      "WARNING: Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x7f174c2f7a60> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Num'\n",
      "60000/60000 [==============================] - 7s 110us/sample - loss: 0.2210 - accuracy: 0.9313 - val_loss: 0.1319 - val_accuracy: 0.9581\n",
      "Epoch 2/6\n",
      "60000/60000 [==============================] - 5s 85us/sample - loss: 0.0814 - accuracy: 0.9756 - val_loss: 0.0814 - val_accuracy: 0.9773\n",
      "Epoch 3/6\n",
      "60000/60000 [==============================] - 6s 92us/sample - loss: 0.0541 - accuracy: 0.9832 - val_loss: 0.0719 - val_accuracy: 0.9789\n",
      "Epoch 4/6\n",
      "60000/60000 [==============================] - 6s 92us/sample - loss: 0.0377 - accuracy: 0.9884 - val_loss: 0.0879 - val_accuracy: 0.9769\n",
      "Epoch 5/6\n",
      "60000/60000 [==============================] - 5s 83us/sample - loss: 0.0290 - accuracy: 0.9911 - val_loss: 0.0713 - val_accuracy: 0.9812\n",
      "Epoch 6/6\n",
      "60000/60000 [==============================] - 5s 86us/sample - loss: 0.0238 - accuracy: 0.9927 - val_loss: 0.0900 - val_accuracy: 0.9804\n",
      "Test score: 0.09002585870867187\n",
      "Test accuracy: 0.9804\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=os.path.join(tempfile.gettempdir(), 'histogram_example'))\n",
    "model_store = ModelCheckpoint(filepath=os.path.join(tempfile.gettempdir(), '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.6.8"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
