{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n",
      "  return f(*args, **kwds)\n",
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import matplotlib\n",
    "%matplotlib inline\n",
    "from main import Environment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup the Environment\n",
    "#### Specify the problem\n",
    "- **cifar**: CIFAR-10 image classification.\n",
    "- **mnist**: MNIST handwritten digit image classification.\n",
    "- **fashion_mnist**: Fashion-MNIST image classification.\n",
    "\n",
    "All models have data_size \"small\" or \"standard\", referring to the dataset with 50k and 5k training examples, respectively. \n",
    "\n",
    "#### Select a model\n",
    "- **conv**: Convolutional neural network.\n",
    "- **mlp**: Multi-layer perceptron with non-linear activations.\n",
    "\n",
    "#### Choose a set of the following algorithms\n",
    "- **TR_uniform**: Second order Trust Region algorithm with spherical trust region shape.\n",
    "- **TR_adagrad**: Second order Trust Region algorithm with ellipsoidal trust region shape (the diagonal Adagrad preconditioning matrix determines the shape of the ellipsoid).\n",
    "- **TR_adagrad**: Second order Trust Region algorithm with ellipsoidal trust region shape (the diagonal Adagrad preconditioning matrix determines the shape of the ellipsoid).\n",
    "- **SGD**\n",
    "- **Adagrad**\n",
    "- **RMSprop**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "problem = \"mnist\"\n",
    "data_size = \"small\" # or \"standard\"\n",
    "model = \"mlp\"\n",
    "methods = [\"TR_adagrad\", \"SGD\", \"TR_rms\"]\n",
    "\n",
    "env = Environment(methods, model, problem, data_size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run the methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32m[i] Running fc-model on mnist with TR_adagrad optimizer.\u001b[0m\n",
      "\u001b[31m[i] No CUDA-capable device found. Running the algorithms on CPU significantly slows down training.\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 2000/2000 [01:27<00:00, 14.68it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32m[i] Running fc-model on mnist with SGD optimizer.\u001b[0m\n",
      "\u001b[31m[i] No CUDA-capable device found. Running the algorithms on CPU significantly slows down training.\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 20000/20000 [00:50<00:00, 393.17it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32m[i] Running fc-model on mnist with TR_rms optimizer.\u001b[0m\n",
      "\u001b[31m[i] No CUDA-capable device found. Running the algorithms on CPU significantly slows down training.\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 27%|██▋       | 5333/20000 [03:21<13:55, 17.56it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[31mTimeout!\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# With 'changes' we can change individual parameters for specific methods\n",
    "changes = {method: {\"max_time\": 200} for method in methods}\n",
    "\n",
    "env.run_training(changes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37612 characters]\n",
      "text/plain": [
       "<Figure size 648x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "env.plot(\"backprops\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31268 characters]\n",
      "text/plain": [
       "<Figure size 648x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "env.plot(\"time\")      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34816 characters]\n",
      "text/plain": [
       "<Figure size 648x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "env.plot(\"iteration\")"
   ]
  },
  {
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
