text: {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Lab 02: Training with epochs -- exercise"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "# For Google Colaboratory\n",
      "import sys, os\n",
      "if 'google.colab' in sys.modules:\n",
      "    from google.colab import drive\n",
      "    drive.mount('/content/gdrive')\n",
      "    file_name = 'epoch_exercise.ipynb'\n",
      "    import subprocess\n",
      "    path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
      "    print(path_to_file)\n",
      "    path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
      "    os.chdir(path_to_file)\n",
      "    !pwd"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "import torch\n",
      "import torch.nn as nn\n",
      "import torch.nn.functional as F\n",
      "import torch.optim as optim\n",
      "from random import randint\n",
      "import utils"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Download the data and print the sizes"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "from utils import check_mnist_dataset_exists\n",
      "data_path=check_mnist_dataset_exists()\n",
      "\n",
      "train_data=torch.load(data_path+'mnist/train_data.pt')\n",
      "train_label=torch.load(data_path+'mnist/train_label.pt')\n",
      "test_data=torch.load(data_path+'mnist/test_data.pt')"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Make a ONE layer net class. "
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "class one_layer_net(nn.Module):\n",
      "\n",
      "    def __init__(self, input_size, output_size):\n",
      "        super(one_layer_net , self).__init__()\n",
      "        self.linear_layer = nn.Linear( input_size, output_size , bias=False)\n",
      "        \n",
      "    def forward(self, x):\n",
      "        scores = self.linear_layer(x)\n",
      "        return scores"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Build the net"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "net=one_layer_net(784,10)\n",
      "print(net)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Choose the criterion, optimizer, batchsize, learning rate"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "criterion = nn.CrossEntropyLoss()\n",
      "\n",
      "optimizer=torch.optim.SGD( net.parameters() , lr=0.01 )\n",
      "\n",
      "bs=50"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# You only have stuff to do in this cell"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Do 15 passes through the training set"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "for # COMPLETE\n",
      "    \n",
      "    # COMPLETE\n",
      " \n",
      "    for  # COMPLETE\n",
      "    \n",
      "        optimizer.zero_grad()\n",
      "        \n",
      "        # COMPLETE\n",
      "        # COMPLETE\n",
      "        # COMPLETE\n",
      "\n",
      "        inputs = minibatch_data.view(bs,784)\n",
      "        \n",
      "        inputs.requires_grad_()\n",
      "\n",
      "        scores=net( inputs ) \n",
      "\n",
      "        loss = criterion( scores , minibatch_label) \n",
      " \n",
      "        loss.backward()\n",
      "\n",
      "        optimizer.step()"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Choose image at random from the test set and see how good/bad are the predictions"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {},
     "outputs": [],
     "source": [
      "# choose a picture at random\n",
      "idx=randint(0, 10000-1)\n",
      "im=test_data[idx]\n",
      "\n",
      "# diplay the picture\n",
      "utils.show(im)\n",
      "\n",
      "# feed it to the net and display the confidence scores\n",
      "scores =  net( im.view(1,784)) \n",
      "probs= F.softmax(scores, dim=1)\n",
      "utils.show_prob_mnist(probs)"
     ]
    }
   ],
   "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"
    }
   },
   "nbformat": 4,
   "nbformat_minor": 2
  }
  