├── 1- Simple Network using Keras ├── Simple Deep NN.ipynb └── Simple Network using keras ├── 2- DenseNet ├── DenseNet Using Keras └── DenseNet.ipynb ├── 8. Single Object Detection ├── 8_Single_Object_Detection.ipynb └── Single Object Detection ├── 9- Multiple Object Detection └── Multiple Object Detection ├── CIFAR10 ├── CIFAR10 └── lesson7-cifar10.ipynb ├── Dog Breed Identification Kaggle ├── Dog Breed Identification ├── Dog breed classification.ipynb └── dog_breed_updated.ipynb ├── Dog Vs Cats ├── dogs vs cats.ipynb ├── dogvscat └── dummy_data.csv ├── IMDB_Sentiment_Analysis ├── IMDB_Sentiment_Analysis └── lesson4-imdb(1).ipynb ├── Movie Recommendation System ├── Movie Recommendation System └── lesson5-movielens.ipynb ├── Nietzsche ├── RNN.ipynb ├── lstm.ipynb └── nietzsche ├── Planet Earth Competition ├── Amazonian-multi-label.ipynb └── Multi-label_classificn ├── Pytorch_Expt ├── 1) Pytorch-Matrices and Reproducibility.ipynb ├── 2) Connection between numpy and torch and vice-versa.ipynb ├── 3) CPU to GPU toggling and vice-versa.ipynb ├── 4) Torch Tensor operations.ipynb ├── 5) Variables.ipynb ├── 6) Linear Regression.ipynb ├── 7) Logistic Regression.ipynb ├── 8) Neural Network.ipynb ├── 9) CNN.ipynb └── pytorch └── README.md /1- Simple Network using Keras/Simple Deep NN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "aNyZv-Ec52ot" 8 | }, 9 | "source": [ 10 | "# **Import Libraries and modules**" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 0, 16 | "metadata": { 17 | "colab": { 18 | "autoexec": { 19 | "startup": false, 20 | "wait_interval": 0 21 | } 22 | }, 23 | "colab_type": "code", 24 | "collapsed": true, 25 | "id": "3m3w1Cw49Zkt" 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "# https://keras.io/\n", 30 | "!pip install -q keras\n", 31 | "import keras" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 1, 37 | "metadata": { 38 | "colab": { 39 | "autoexec": { 40 | "startup": false, 41 | "wait_interval": 0 42 | }, 43 | "base_uri": "https://localhost:8080/", 44 | "height": 34 45 | }, 46 | "colab_type": "code", 47 | "executionInfo": { 48 | "elapsed": 1703, 49 | "status": "ok", 50 | "timestamp": 1526009535023, 51 | "user": { 52 | "displayName": "ashis kumar", 53 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 54 | "userId": "112959655216491997395" 55 | }, 56 | "user_tz": -330 57 | }, 58 | "id": "Eso6UHE080D4", 59 | "outputId": "6570a846-bb80-4943-f047-fbe4bc46a8a9" 60 | }, 61 | "outputs": [ 62 | { 63 | "name": "stderr", 64 | "output_type": "stream", 65 | "text": [ 66 | "Using TensorFlow backend.\n" 67 | ] 68 | }, 69 | { 70 | "ename": "ModuleNotFoundError", 71 | "evalue": "No module named 'tensorflow'", 72 | "output_type": "error", 73 | "traceback": [ 74 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 75 | "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", 76 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodels\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mSequential\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlayers\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mDense\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mDropout\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mActivation\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mFlatten\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mAdd\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlayers\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mConvolution2D\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mMaxPooling2D\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 77 | "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\keras\\__init__.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0m__future__\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mabsolute_import\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mutils\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mactivations\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mapplications\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 78 | "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\keras\\utils\\__init__.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mdata_utils\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mio_utils\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mconv_utils\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;31m# Globally-importable utils.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 79 | "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\keras\\utils\\conv_utils.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0msix\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmoves\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mbackend\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mK\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", 80 | "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\keras\\backend\\__init__.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 82\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0m_BACKEND\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'tensorflow'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 83\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstderr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Using TensorFlow backend.\\n'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 84\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0mtensorflow_backend\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 85\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 86\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Unknown backend: '\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_BACKEND\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 81 | "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0m__future__\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mprint_function\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtraining\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmoving_averages\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mops\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtensor_array_ops\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 82 | "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'tensorflow'" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "import numpy as np\n", 88 | "\n", 89 | "from keras.models import Sequential\n", 90 | "from keras.layers import Dense, Dropout, Activation, Flatten, Add\n", 91 | "from keras.layers import Convolution2D, MaxPooling2D\n", 92 | "from keras.utils import np_utils\n", 93 | "\n", 94 | "from keras.datasets import mnist" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": { 100 | "colab_type": "text", 101 | "id": "zByEi95J86RD" 102 | }, 103 | "source": [ 104 | "### Load pre-shuffled MNIST data into train and test sets" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 0, 110 | "metadata": { 111 | "colab": { 112 | "autoexec": { 113 | "startup": false, 114 | "wait_interval": 0 115 | } 116 | }, 117 | "colab_type": "code", 118 | "collapsed": true, 119 | "id": "7eRM0QWN83PV" 120 | }, 121 | "outputs": [], 122 | "source": [ 123 | "(X_train, y_train), (X_test, y_test) = mnist.load_data()" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 3, 129 | "metadata": { 130 | "colab": { 131 | "autoexec": { 132 | "startup": false, 133 | "wait_interval": 0 134 | }, 135 | "base_uri": "https://localhost:8080/", 136 | "height": 299 137 | }, 138 | "colab_type": "code", 139 | "executionInfo": { 140 | "elapsed": 824, 141 | "status": "ok", 142 | "timestamp": 1526009541300, 143 | "user": { 144 | "displayName": "ashis kumar", 145 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 146 | "userId": "112959655216491997395" 147 | }, 148 | "user_tz": -330 149 | }, 150 | "id": "4a4Be72j8-ZC", 151 | "outputId": "d35a5a6a-fa92-4f83-9917-6997995f52d0" 152 | }, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "(60000, 28, 28)\n" 159 | ] 160 | }, 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "" 165 | ] 166 | }, 167 | "execution_count": 3, 168 | "metadata": { 169 | "tags": [] 170 | }, 171 | "output_type": "execute_result" 172 | }, 173 | { 174 | "data": { 175 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPoAAAD4CAYAAADFJPs2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAADq5JREFUeJzt3X+MVPW5x/H3uriAQFuwCi1pQvTW\nJ7fhDwJRytWlq1Dkkt6rZsGKP2LEhEaLVq/VWEiMYKIE3aD8uE1IFQikEREs0BqjWFNj4u9YbLU+\nVlOJCAQU4QrFFVbuHztsdxbmO7OzZ2aWfT6vfzrnPHvOPI5+en6fb92xY8cQkb7ttFo3ICKVp6CL\nBKCgiwSgoIsEoKCLBNCvSt+jU/silVdXqFB20M1sMfBD2kP8C3d/vdx1iUhllbXrbmY/Ar7v7hOA\nG4ElmXYlIpkq9xh9EvA7AHf/GzDUzL6RWVcikqlygz4C2Ntpem9unoj0QlmddS94EkBEaq/coO8k\nfwv+XWBXz9sRkUooN+jPAtMBzGwssNPdv8isKxHJVF25T6+Z2UJgIvA18HN335b4c11HF6m8gofQ\nZQe9mxR0kcorGHTdAisSgIIuEoCCLhKAgi4SgIIuEoCCLhKAgi4SgIIuEoCCLhKAgi4SgIIuEoCC\nLhKAgi4SgIIuEoCCLhKAgi4SgIIuEoCCLhKAgi4SgIIuEoCCLhKAgi4SgIIuEoCCLhKAgi4SgIIu\nEoCCLhKAgi4SgIIuEkC/WjcglfH1118n662trZl+38CBAzl8+HDH9OrVqwv+7aFDh5Lrevfdd5P1\nhx9+OFmfO3du3vTSpUu55ZZbAFi2bFly2YEDBybrLS0tyfpNN92UrNdKWUE3syZgPfBObtZf3P2W\nrJoSkWz1ZIv+J3efnlknIlIxOkYXCaDu2LFj3V4ot+v+v8AHwDBgvrs/l1ik+18iIt1VV7BQZtBH\nAhcBTwDnAC8A/+buXxVYREGvMp2M+5dAJ+MKBr2sY3R3/wRYl5v80Mx2AyOBf5SzPhGprLKO0c3s\nGjP7Ze7zCGA48EmWjYlIdsrddR8C/Bb4FtBA+zH604lFQu66HzhwIFlva2tL1rdt25Y3ffHFF/PC\nCy90TD/77LMFl92/f39y3StWrEjWu6utrY36+vpM1jVq1KhkfdKkScn6o48+mjfdubchQ4Ykl21s\n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\n", 176 | "text/plain": [ 177 | "" 178 | ] 179 | }, 180 | "metadata": { 181 | "tags": [] 182 | }, 183 | "output_type": "display_data" 184 | } 185 | ], 186 | "source": [ 187 | "print (X_train.shape)\n", 188 | "from matplotlib import pyplot as plt\n", 189 | "%matplotlib inline\n", 190 | "plt.imshow(X_train[0])" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 0, 196 | "metadata": { 197 | "colab": { 198 | "autoexec": { 199 | "startup": false, 200 | "wait_interval": 0 201 | } 202 | }, 203 | "colab_type": "code", 204 | "collapsed": true, 205 | "id": "dkmprriw9AnZ" 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "X_train = X_train.reshape(X_train.shape[0], 28, 28,1)\n", 210 | "X_test = X_test.reshape(X_test.shape[0], 28, 28,1)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 0, 216 | "metadata": { 217 | "colab": { 218 | "autoexec": { 219 | "startup": false, 220 | "wait_interval": 0 221 | } 222 | }, 223 | "colab_type": "code", 224 | "collapsed": true, 225 | "id": "X2m4YS4E9CRh" 226 | }, 227 | "outputs": [], 228 | "source": [ 229 | "X_train = X_train.astype('float32')\n", 230 | "X_test = X_test.astype('float32')\n", 231 | "X_train /= 255\n", 232 | "X_test /= 255" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 6, 238 | "metadata": { 239 | "colab": { 240 | "autoexec": { 241 | "startup": false, 242 | "wait_interval": 0 243 | }, 244 | "base_uri": "https://localhost:8080/", 245 | "height": 34 246 | }, 247 | "colab_type": "code", 248 | "executionInfo": { 249 | "elapsed": 770, 250 | "status": "ok", 251 | "timestamp": 1526009545588, 252 | "user": { 253 | "displayName": "ashis kumar", 254 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 255 | "userId": "112959655216491997395" 256 | }, 257 | "user_tz": -330 258 | }, 259 | "id": "0Mn0vAYD9DvB", 260 | "outputId": "59a552ac-ba07-4465-9ef2-75bf0053de50" 261 | }, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "array([5, 0, 4, 1, 9, 2, 1, 3, 1, 4], dtype=uint8)" 267 | ] 268 | }, 269 | "execution_count": 6, 270 | "metadata": { 271 | "tags": [] 272 | }, 273 | "output_type": "execute_result" 274 | } 275 | ], 276 | "source": [ 277 | "y_train[:10]" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 0, 283 | "metadata": { 284 | "colab": { 285 | "autoexec": { 286 | "startup": false, 287 | "wait_interval": 0 288 | } 289 | }, 290 | "colab_type": "code", 291 | "collapsed": true, 292 | "id": "ZG8JiXR39FHC" 293 | }, 294 | "outputs": [], 295 | "source": [ 296 | "# Convert 1-dimensional class arrays to 10-dimensional class matrices\n", 297 | "Y_train = np_utils.to_categorical(y_train, 10)\n", 298 | "Y_test = np_utils.to_categorical(y_test, 10)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 8, 304 | "metadata": { 305 | "colab": { 306 | "autoexec": { 307 | "startup": false, 308 | "wait_interval": 0 309 | }, 310 | "base_uri": "https://localhost:8080/", 311 | "height": 187 312 | }, 313 | "colab_type": "code", 314 | "executionInfo": { 315 | "elapsed": 721, 316 | "status": "ok", 317 | "timestamp": 1526009548254, 318 | "user": { 319 | "displayName": "ashis kumar", 320 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 321 | "userId": "112959655216491997395" 322 | }, 323 | "user_tz": -330 324 | }, 325 | "id": "fYlFRvKS9HMB", 326 | "outputId": "f965d23f-2e09-4625-dd0d-df8dbaa324d7" 327 | }, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "array([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],\n", 333 | " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 334 | " [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],\n", 335 | " [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 336 | " [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", 337 | " [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n", 338 | " [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 339 | " [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n", 340 | " [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 341 | " [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]], dtype=float32)" 342 | ] 343 | }, 344 | "execution_count": 8, 345 | "metadata": { 346 | "tags": [] 347 | }, 348 | "output_type": "execute_result" 349 | } 350 | ], 351 | "source": [ 352 | "Y_train[:10]\n" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 14, 358 | "metadata": { 359 | "colab": { 360 | "autoexec": { 361 | "startup": false, 362 | "wait_interval": 0 363 | }, 364 | "base_uri": "https://localhost:8080/", 365 | "height": 139 366 | }, 367 | "colab_type": "code", 368 | "executionInfo": { 369 | "elapsed": 1194, 370 | "status": "ok", 371 | "timestamp": 1526009612873, 372 | "user": { 373 | "displayName": "ashis kumar", 374 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 375 | "userId": "112959655216491997395" 376 | }, 377 | "user_tz": -330 378 | }, 379 | "id": "osKqT73Q9JJB", 380 | "outputId": "5a1f1796-95b9-4f8b-8479-4b7ed3db1acc" 381 | }, 382 | "outputs": [ 383 | { 384 | "name": "stderr", 385 | "output_type": "stream", 386 | "text": [ 387 | "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:5: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (5, 1), activation=\"relu\", input_shape=(28, 28, 1...)`\n", 388 | " \"\"\"\n", 389 | "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:6: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (1, 5), activation=\"relu\")`\n", 390 | " \n", 391 | "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:9: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), activation=\"relu\")`\n", 392 | " if __name__ == '__main__':\n" 393 | ] 394 | } 395 | ], 396 | "source": [ 397 | "from keras.layers import Activation\n", 398 | "model = Sequential()\n", 399 | "\n", 400 | " \n", 401 | "model.add(Convolution2D(32, 5,1, activation='relu', input_shape=(28,28,1)))\n", 402 | "model.add(Convolution2D(32, 1,5, activation='relu'))\n", 403 | "# model.add(Dropout(0.7))\n", 404 | "model.add(MaxPooling2D(pool_size=2, strides=None, padding='valid'))\n", 405 | "model.add(Convolution2D(32, 3, 3, activation='relu'))\n", 406 | "model.add(Convolution2D(10, 10))\n", 407 | "# model.add(Convolution2D(10, 1, activation='relu'))\n", 408 | "# model.add(Convolution2D(10, 24))\n", 409 | "# model.add(MaxPooling2D(pool_size=2, strides=None, padding='valid'))\n", 410 | "# model.add(Convolution2D(10, 1, activation='relu'))\n", 411 | "# model.add(Convolution2D(10, 9, activation='relu'))\n", 412 | "# model.add(Convolution2D(32, 3, activation='relu'))\n", 413 | "# model.add(Convolution2D(32, 5, activation='relu'))\n", 414 | "# model.add(Convolution2D(10, 19, activation='relu'))\n", 415 | "# model.add(MaxPooling2D(pool_size=2, strides=None, padding='valid'))\n", 416 | "# model.add(Convolution2D(10, 1, activation='relu'))\n", 417 | "# model.add(Convolution2D(32, 2, 2, activation='relu'))\n", 418 | "# model.add(Convolution2D(32, 2, 2, activation='relu'))\n", 419 | "# model.add(Convolution2D(10, 1, activation='relu'))\n", 420 | "# model.add(Convolution2D(10, 10, activation='relu'))\n", 421 | "# # model.add(Convolution2D(10, 3,3))\n", 422 | "# # model.add(Convolution2D(10, 11,11, activation='relu'))\n", 423 | "model.add(Flatten())\n", 424 | "model.add(Activation('softmax'))" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 15, 430 | "metadata": { 431 | "colab": { 432 | "autoexec": { 433 | "startup": false, 434 | "wait_interval": 0 435 | }, 436 | "base_uri": "https://localhost:8080/", 437 | "height": 374 438 | }, 439 | "colab_type": "code", 440 | "executionInfo": { 441 | "elapsed": 1021, 442 | "status": "ok", 443 | "timestamp": 1526009614204, 444 | "user": { 445 | "displayName": "ashis kumar", 446 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 447 | "userId": "112959655216491997395" 448 | }, 449 | "user_tz": -330 450 | }, 451 | "id": "TzdAYg1k9K7Z", 452 | "outputId": "7f336b21-4bf5-4180-c977-1dc9cd503802" 453 | }, 454 | "outputs": [ 455 | { 456 | "name": "stdout", 457 | "output_type": "stream", 458 | "text": [ 459 | "_________________________________________________________________\n", 460 | "Layer (type) Output Shape Param # \n", 461 | "=================================================================\n", 462 | "conv2d_11 (Conv2D) (None, 24, 28, 32) 192 \n", 463 | "_________________________________________________________________\n", 464 | "conv2d_12 (Conv2D) (None, 24, 24, 32) 5152 \n", 465 | "_________________________________________________________________\n", 466 | "max_pooling2d_3 (MaxPooling2 (None, 12, 12, 32) 0 \n", 467 | "_________________________________________________________________\n", 468 | "conv2d_13 (Conv2D) (None, 10, 10, 32) 9248 \n", 469 | "_________________________________________________________________\n", 470 | "conv2d_14 (Conv2D) (None, 1, 1, 10) 32010 \n", 471 | "_________________________________________________________________\n", 472 | "flatten_2 (Flatten) (None, 10) 0 \n", 473 | "_________________________________________________________________\n", 474 | "activation_2 (Activation) (None, 10) 0 \n", 475 | "=================================================================\n", 476 | "Total params: 46,602\n", 477 | "Trainable params: 46,602\n", 478 | "Non-trainable params: 0\n", 479 | "_________________________________________________________________\n" 480 | ] 481 | } 482 | ], 483 | "source": [ 484 | "model.summary()" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 0, 490 | "metadata": { 491 | "colab": { 492 | "autoexec": { 493 | "startup": false, 494 | "wait_interval": 0 495 | } 496 | }, 497 | "colab_type": "code", 498 | "collapsed": true, 499 | "id": "Zp6SuGrL9M3h" 500 | }, 501 | "outputs": [], 502 | "source": [ 503 | "model.compile(loss='categorical_crossentropy',\n", 504 | " optimizer='adam',\n", 505 | " metrics=['accuracy'])" 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": 17, 511 | "metadata": { 512 | "colab": { 513 | "autoexec": { 514 | "startup": false, 515 | "wait_interval": 0 516 | }, 517 | "base_uri": "https://localhost:8080/", 518 | "height": 428 519 | }, 520 | "colab_type": "code", 521 | "executionInfo": { 522 | "elapsed": 208976, 523 | "status": "ok", 524 | "timestamp": 1526009830649, 525 | "user": { 526 | "displayName": "ashis kumar", 527 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 528 | "userId": "112959655216491997395" 529 | }, 530 | "user_tz": -330 531 | }, 532 | "id": "4xWoKhPY9Of5", 533 | "outputId": "9dbe6f47-89bc-48fe-fee0-1c1e90972883" 534 | }, 535 | "outputs": [ 536 | { 537 | "name": "stderr", 538 | "output_type": "stream", 539 | "text": [ 540 | "/usr/local/lib/python3.6/dist-packages/keras/models.py:981: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.\n", 541 | " warnings.warn('The `nb_epoch` argument in `fit` '\n" 542 | ] 543 | }, 544 | { 545 | "name": "stdout", 546 | "output_type": "stream", 547 | "text": [ 548 | "Epoch 1/10\n", 549 | "60000/60000 [==============================] - 23s 380us/step - loss: 0.1465 - acc: 0.9554\n", 550 | "Epoch 2/10\n", 551 | "50944/60000 [========================>.....] - ETA: 3s - loss: 0.0509 - acc: 0.985060000/60000 [==============================] - 21s 344us/step - loss: 0.0491 - acc: 0.9851\n", 552 | "Epoch 3/10\n", 553 | "60000/60000 [==============================] - 21s 347us/step - loss: 0.0348 - acc: 0.9890\n", 554 | "Epoch 4/10\n", 555 | "13056/60000 [=====>........................] - ETA: 16s - loss: 0.0229 - acc: 0.992860000/60000 [==============================] - 21s 345us/step - loss: 0.0256 - acc: 0.9919\n", 556 | "Epoch 5/10\n", 557 | "60000/60000 [==============================] - 20s 342us/step - loss: 0.0204 - acc: 0.9932\n", 558 | "Epoch 6/10\n", 559 | " 2048/60000 [>.............................] - ETA: 20s - loss: 0.0086 - acc: 0.997160000/60000 [==============================] - 21s 342us/step - loss: 0.0158 - acc: 0.9953\n", 560 | "Epoch 7/10\n", 561 | "58976/60000 [============================>.] - ETA: 0s - loss: 0.0124 - acc: 0.996360000/60000 [==============================] - 20s 341us/step - loss: 0.0123 - acc: 0.9963\n", 562 | "Epoch 8/10\n", 563 | "60000/60000 [==============================] - 21s 343us/step - loss: 0.0105 - acc: 0.9965\n", 564 | "Epoch 9/10\n", 565 | "15328/60000 [======>.......................] - ETA: 15s - loss: 0.0060 - acc: 0.997960000/60000 [==============================] - 21s 345us/step - loss: 0.0092 - acc: 0.9970\n", 566 | "Epoch 10/10\n", 567 | "60000/60000 [==============================] - 20s 340us/step - loss: 0.0068 - acc: 0.9979\n" 568 | ] 569 | }, 570 | { 571 | "data": { 572 | "text/plain": [ 573 | "" 574 | ] 575 | }, 576 | "execution_count": 17, 577 | "metadata": { 578 | "tags": [] 579 | }, 580 | "output_type": "execute_result" 581 | } 582 | ], 583 | "source": [ 584 | "model.fit(X_train, Y_train, batch_size=32, nb_epoch=10, verbose=1)" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 0, 590 | "metadata": { 591 | "colab": { 592 | "autoexec": { 593 | "startup": false, 594 | "wait_interval": 0 595 | } 596 | }, 597 | "colab_type": "code", 598 | "collapsed": true, 599 | "id": "AtsH-lLk-eLb" 600 | }, 601 | "outputs": [], 602 | "source": [ 603 | "score = model.evaluate(X_test, Y_test, verbose=0)" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": 19, 609 | "metadata": { 610 | "colab": { 611 | "autoexec": { 612 | "startup": false, 613 | "wait_interval": 0 614 | }, 615 | "base_uri": "https://localhost:8080/", 616 | "height": 34 617 | }, 618 | "colab_type": "code", 619 | "executionInfo": { 620 | "elapsed": 734, 621 | "status": "ok", 622 | "timestamp": 1526009833748, 623 | "user": { 624 | "displayName": "ashis kumar", 625 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 626 | "userId": "112959655216491997395" 627 | }, 628 | "user_tz": -330 629 | }, 630 | "id": "mkX8JMv79q9r", 631 | "outputId": "4c8319e6-025a-4ab4-8955-40b594bd381b" 632 | }, 633 | "outputs": [ 634 | { 635 | "name": "stdout", 636 | "output_type": "stream", 637 | "text": [ 638 | "[0.03862601956458843, 0.9916]\n" 639 | ] 640 | } 641 | ], 642 | "source": [ 643 | "print(score)" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 0, 649 | "metadata": { 650 | "colab": { 651 | "autoexec": { 652 | "startup": false, 653 | "wait_interval": 0 654 | } 655 | }, 656 | "colab_type": "code", 657 | "collapsed": true, 658 | "id": "OCWoJkwE9suh" 659 | }, 660 | "outputs": [], 661 | "source": [ 662 | "y_pred = model.predict(X_test)" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": 21, 668 | "metadata": { 669 | "colab": { 670 | "autoexec": { 671 | "startup": false, 672 | "wait_interval": 0 673 | }, 674 | "base_uri": "https://localhost:8080/", 675 | "height": 340 676 | }, 677 | "colab_type": "code", 678 | "executionInfo": { 679 | "elapsed": 723, 680 | "status": "ok", 681 | "timestamp": 1526009846860, 682 | "user": { 683 | "displayName": "ashis kumar", 684 | "photoUrl": "//lh4.googleusercontent.com/-fAW8BbSQZCA/AAAAAAAAAAI/AAAAAAAABEI/EOTcAAbxBFw/s50-c-k-no/photo.jpg", 685 | "userId": "112959655216491997395" 686 | }, 687 | "user_tz": -330 688 | }, 689 | "id": "Ym7iCFBm9uBs", 690 | "outputId": "a3ca5819-4555-4ecc-a01b-cee11bac851c" 691 | }, 692 | "outputs": [ 693 | { 694 | "name": "stdout", 695 | "output_type": "stream", 696 | "text": [ 697 | "[[1.6766694e-19 2.4293749e-23 3.0154017e-15 3.3653812e-11 1.6824322e-23\n", 698 | " 9.0364089e-18 8.2460712e-31 1.0000000e+00 2.7956351e-18 1.5539864e-13]\n", 699 | " [2.1175965e-16 8.7530441e-18 1.0000000e+00 3.7259122e-22 9.3698532e-26\n", 700 | " 8.5974933e-29 1.4114961e-15 2.8623161e-24 7.7001959e-20 1.3076943e-21]\n", 701 | " [1.1459237e-11 9.9999762e-01 9.3107960e-10 1.0585906e-10 1.7662133e-07\n", 702 | " 1.1981100e-08 6.7403488e-10 1.7797121e-06 3.9206162e-07 2.1283518e-11]\n", 703 | " [9.9999976e-01 5.2022729e-18 6.4555056e-10 1.6351479e-15 8.4766558e-14\n", 704 | " 1.4844793e-13 1.8069882e-07 2.6000471e-12 4.5274504e-10 8.4324281e-09]\n", 705 | " [6.2784036e-16 8.6798642e-17 1.4563554e-14 8.8526510e-16 1.0000000e+00\n", 706 | " 1.4550795e-16 5.3067024e-14 6.0328484e-13 2.7289279e-10 3.8328860e-08]\n", 707 | " [1.3903134e-12 9.9999130e-01 6.9174377e-10 4.0553008e-12 8.6209099e-08\n", 708 | " 4.8363737e-11 1.2644228e-12 6.7650171e-06 1.9244071e-06 4.4529869e-11]\n", 709 | " [1.3140152e-28 1.5243668e-19 5.7061122e-17 6.3732687e-21 9.9999988e-01\n", 710 | " 6.6687357e-15 4.0213277e-24 2.0031264e-16 1.0195425e-07 1.4205691e-10]\n", 711 | " [4.6198767e-24 1.5927705e-19 4.4902453e-15 9.3174437e-14 8.9086294e-09\n", 712 | " 3.5618342e-14 2.6446297e-21 4.0003313e-19 5.4621745e-12 1.0000000e+00]\n", 713 | " [1.4649459e-14 3.4486828e-22 9.2192002e-21 7.2164560e-17 7.8567924e-17\n", 714 | " 9.8924446e-01 1.0755590e-02 1.5131058e-19 1.8540610e-09 3.5583641e-12]]\n", 715 | "[7 2 1 0 4 1 4 9 5]\n" 716 | ] 717 | } 718 | ], 719 | "source": [ 720 | "print(y_pred[:9])\n", 721 | "print(y_test[:9])" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 0, 727 | "metadata": { 728 | "colab": { 729 | "autoexec": { 730 | "startup": false, 731 | "wait_interval": 0 732 | } 733 | }, 734 | "colab_type": "code", 735 | "collapsed": true, 736 | "id": "CT--y98_dr2T" 737 | }, 738 | "outputs": [], 739 | "source": [ 740 | "layer_dict = dict([(layer.name, layer) for layer in model.layers])" 741 | ] 742 | }, 743 | { 744 | "cell_type": "code", 745 | "execution_count": 0, 746 | "metadata": { 747 | "colab": { 748 | "autoexec": { 749 | "startup": false, 750 | "wait_interval": 0 751 | } 752 | }, 753 | "colab_type": "code", 754 | "collapsed": true, 755 | "id": "2GY4Upv4dsUR" 756 | }, 757 | "outputs": [], 758 | "source": [ 759 | "# import numpy as np\n", 760 | "# from matplotlib import pyplot as plt\n", 761 | "# from keras import backend as K\n", 762 | "# %matplotlib inline\n", 763 | "# # util function to convert a tensor into a valid image\n", 764 | "# def deprocess_image(x):\n", 765 | "# # normalize tensor: center on 0., ensure std is 0.1\n", 766 | "# x -= x.mean()\n", 767 | "# x /= (x.std() + 1e-5)\n", 768 | "# x *= 0.1\n", 769 | "\n", 770 | "# # clip to [0, 1]\n", 771 | "# x += 0.5\n", 772 | "# x = np.clip(x, 0, 1)\n", 773 | "\n", 774 | "# # convert to RGB array\n", 775 | "# x *= 255\n", 776 | "# #x = x.transpose((1, 2, 0))\n", 777 | "# x = np.clip(x, 0, 255).astype('uint8')\n", 778 | "# return x\n", 779 | "\n", 780 | "# def vis_img_in_filter(img = np.array(X_train[2]).reshape((1, 28, 28, 1)).astype(np.float64), \n", 781 | "# layer_name = 'conv2d_14'):\n", 782 | "# layer_output = layer_dict[layer_name].output\n", 783 | "# img_ascs = list()\n", 784 | "# for filter_index in range(layer_output.shape[3]):\n", 785 | "# # build a loss function that maximizes the activation\n", 786 | "# # of the nth filter of the layer considered\n", 787 | "# loss = K.mean(layer_output[:, :, :, filter_index])\n", 788 | "\n", 789 | "# # compute the gradient of the input picture wrt this loss\n", 790 | "# grads = K.gradients(loss, model.input)[0]\n", 791 | "\n", 792 | "# # normalization trick: we normalize the gradient\n", 793 | "# grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)\n", 794 | "\n", 795 | "# # this function returns the loss and grads given the input picture\n", 796 | "# iterate = K.function([model.input], [loss, grads])\n", 797 | "\n", 798 | "# # step size for gradient ascent\n", 799 | "# step = 5.\n", 800 | "\n", 801 | "# img_asc = np.array(img)\n", 802 | "# # run gradient ascent for 20 steps\n", 803 | "# for i in range(20):\n", 804 | "# loss_value, grads_value = iterate([img_asc])\n", 805 | "# img_asc += grads_value * step\n", 806 | "\n", 807 | "# img_asc = img_asc[0]\n", 808 | "# img_ascs.append(deprocess_image(img_asc).reshape((28, 28)))\n", 809 | " \n", 810 | "# if layer_output.shape[3] >= 35:\n", 811 | "# plot_x, plot_y = 6, 6\n", 812 | "# elif layer_output.shape[3] >= 23:\n", 813 | "# plot_x, plot_y = 4, 6\n", 814 | "# elif layer_output.shape[3] >= 11:\n", 815 | "# plot_x, plot_y = 2, 6\n", 816 | "# else:\n", 817 | "# plot_x, plot_y = 1, 2\n", 818 | "# fig, ax = plt.subplots(plot_x, plot_y, figsize = (12, 12))\n", 819 | "# ax[0, 0].imshow(img.reshape((28, 28)), cmap = 'gray')\n", 820 | "# ax[0, 0].set_title('Input image')\n", 821 | "# fig.suptitle('Input image and %s filters' % (layer_name,))\n", 822 | "# fig.tight_layout(pad = 0.3, rect = [0, 0, 0.9, 0.9])\n", 823 | "# for (x, y) in [(i, j) for i in range(plot_x) for j in range(plot_y)]:\n", 824 | "# if x == 0 and y == 0:\n", 825 | "# continue\n", 826 | "# ax[x, y].imshow(img_ascs[x * plot_y + y - 1], cmap = 'gray')\n", 827 | "# ax[x, y].set_title('filter %d' % (x * plot_y + y - 1))\n", 828 | "\n", 829 | "# vis_img_in_filter()" 830 | ] 831 | }, 832 | { 833 | "cell_type": "code", 834 | "execution_count": 0, 835 | "metadata": { 836 | "colab": { 837 | "autoexec": { 838 | "startup": false, 839 | "wait_interval": 0 840 | } 841 | }, 842 | "colab_type": "code", 843 | "collapsed": true, 844 | "id": "9tvptcn8dxvp" 845 | }, 846 | "outputs": [], 847 | "source": [] 848 | } 849 | ], 850 | "metadata": { 851 | "accelerator": "GPU", 852 | "colab": { 853 | "collapsed_sections": [], 854 | "default_view": {}, 855 | "name": "ASHIS_BATCH_3_ASSIGNMENT4A.ipynb", 856 | "provenance": [ 857 | { 858 | "file_id": "1gZYwZdkgXBJRr624SqWJ9f452BmKNkNT", 859 | "timestamp": 1526009491441 860 | }, 861 | { 862 | "file_id": "1JURGwe4e5Z7928Zv2eiJVPCQNc702huM", 863 | "timestamp": 1521864568638 864 | } 865 | ], 866 | "version": "0.3.2", 867 | "views": {} 868 | }, 869 | "kernelspec": { 870 | "display_name": "Python 3", 871 | "language": "python", 872 | "name": "python3" 873 | }, 874 | "language_info": { 875 | "codemirror_mode": { 876 | "name": "ipython", 877 | "version": 3 878 | }, 879 | "file_extension": ".py", 880 | "mimetype": "text/x-python", 881 | "name": "python", 882 | "nbconvert_exporter": "python", 883 | "pygments_lexer": "ipython3", 884 | "version": "3.6.2" 885 | } 886 | }, 887 | "nbformat": 4, 888 | "nbformat_minor": 1 889 | } 890 | -------------------------------------------------------------------------------- /1- Simple Network using Keras/Simple Network using keras: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /2- DenseNet/DenseNet Using Keras: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /8. Single Object Detection/Single Object Detection: -------------------------------------------------------------------------------- 1 | 2 | ## IMPLEMENTING FASTAI SINGLE OBJECT DETECTION 3 | 1. load the data using pathlib (or, any other library) 4 | 5 | 2. Convert the data into a python container or pandas dataframe. 6 | a. from json to dictionaries. 7 | 8 | 3. Make constants for easy accessability and preprocess the data. 9 | a. making a csv file that can be fed into the ImageClassifierData 10 | 11 | 4. Define fuctions for displaying the given image. 12 | a. show_image - returning ax. 13 | b. bb_hw - Swaping the coordinates for convenience. 14 | c. draw_outline - Drawing the bounding box, using patheffects. 15 | d. draw_rectangle - Drawing the rectangle patches, using patches. c. draw_text - writing the annotaion class on the image. 16 | 17 | 5. Largest Item Classifier. 18 | a. get the largest bounding box. 19 | b. make a custom csv file to fed it into our model. 20 | 21 | 6. fastai routine for model fitting. 22 | a. donot crop during augmentations. 23 | b. make two different dataloaders for predicting bounding box and class. 24 | c. don't forget to specify the regression/classification parameter. 25 | 26 | 7. Making a custom dataset, merging the above data loaders. 27 | a. refer ConcatLblDataset. 28 | b. make this as your md. 29 | 30 | 8. Add a custom head at the end of your convnet model. 31 | a. hint: relu--Dropout--Linear--relu--batchnorm--drpout--linear 32 | 33 | 9. Define your learner and its optimizer. 34 | a. learn.opt_fn 35 | 36 | 10. Defining custom LOSS funCTION, learn.crit, learn.metrics. a. L1 loss + Cross Entropy Loss = F.l1_loss(bb_i, bb_t) + F.cross_entropy(c_i, c_t)*k b. 37 | 38 | The BEST PART, learn.fit a. adjust use_clr. 39 | 40 | -------------------------------------------------------------------------------- /9- Multiple Object Detection/Multiple Object Detection: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CIFAR10/CIFAR10: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Dog Breed Identification Kaggle/Dog Breed Identification: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Dog Vs Cats/dogvscat: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /IMDB_Sentiment_Analysis/IMDB_Sentiment_Analysis: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Movie Recommendation System/Movie Recommendation System: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Nietzsche/lstm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "rnn_modified.ipynb", 7 | "version": "0.3.2", 8 | "provenance": [], 9 | "collapsed_sections": [] 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | }, 15 | "accelerator": "GPU" 16 | }, 17 | "cells": [ 18 | { 19 | "metadata": { 20 | "id": "e_EObDalmGg0", 21 | "colab_type": "code", 22 | "outputId": "963407e4-93e9-4ab2-d171-170f0b55ae3b", 23 | "colab": { 24 | "base_uri": "https://localhost:8080/", 25 | "height": 2719 26 | } 27 | }, 28 | "cell_type": "code", 29 | "source": [ 30 | "! pip install fastai==0.7.0\n", 31 | "! pip install torchtext==0.2.3" 32 | ], 33 | "execution_count": 1, 34 | "outputs": [ 35 | { 36 | "output_type": "stream", 37 | "text": [ 38 | "Collecting fastai==0.7.0\n", 39 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/50/6d/9d0d6e17a78b0598d5e8c49a0d03ffc7ff265ae62eca3e2345fab14edb9b/fastai-0.7.0-py3-none-any.whl (112kB)\n", 40 | "\u001b[K 100% |████████████████████████████████| 122kB 3.8MB/s \n", 41 | "\u001b[?25hCollecting jupyter (from fastai==0.7.0)\n", 42 | " Downloading https://files.pythonhosted.org/packages/83/df/0f5dd132200728a86190397e1ea87cd76244e42d39ec5e88efd25b2abd7e/jupyter-1.0.0-py2.py3-none-any.whl\n", 43 | "Collecting widgetsnbextension (from fastai==0.7.0)\n", 44 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/8a/81/35789a3952afb48238289171728072d26d6e76649ddc8b3588657a2d78c1/widgetsnbextension-3.4.2-py2.py3-none-any.whl (2.2MB)\n", 45 | "\u001b[K 100% |████████████████████████████████| 2.2MB 13.7MB/s \n", 46 | "\u001b[?25hCollecting graphviz (from fastai==0.7.0)\n", 47 | " Downloading https://files.pythonhosted.org/packages/1f/e2/ef2581b5b86625657afd32030f90cf2717456c1d2b711ba074bf007c0f1a/graphviz-0.10.1-py2.py3-none-any.whl\n", 48 | "Collecting feather-format (from fastai==0.7.0)\n", 49 | " Downloading https://files.pythonhosted.org/packages/08/55/940b97cc6f19a19f5dab9efef2f68a0ce43a7632f858b272391f0b851a7e/feather-format-0.4.0.tar.gz\n", 50 | "Requirement already satisfied: opencv-python in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (3.4.4.19)\n", 51 | "Requirement already satisfied: pyzmq in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (17.0.0)\n", 52 | "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (1.14.6)\n", 53 | "Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (1.1.0)\n", 54 | "Requirement already satisfied: jsonschema in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2.6.0)\n", 55 | "Requirement already satisfied: ipython in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (5.5.0)\n", 56 | "Collecting torchtext (from fastai==0.7.0)\n", 57 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/c6/bc/b28b9efb4653c03e597ed207264eea45862b5260f48e9f010b5068d64db1/torchtext-0.3.1-py3-none-any.whl (62kB)\n", 58 | "\u001b[K 100% |████████████████████████████████| 71kB 20.9MB/s \n", 59 | "\u001b[?25hRequirement already satisfied: webencodings in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.5.1)\n", 60 | "Requirement already satisfied: simplegeneric in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.8.1)\n", 61 | "Requirement already satisfied: testpath in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.4.2)\n", 62 | "Requirement already satisfied: ipykernel in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (4.6.1)\n", 63 | "Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (4.28.1)\n", 64 | "Requirement already satisfied: bleach in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (3.0.2)\n", 65 | "Requirement already satisfied: MarkupSafe in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (1.1.0)\n", 66 | "Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.22.0)\n", 67 | "Requirement already satisfied: decorator in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (4.3.0)\n", 68 | "Requirement already satisfied: pyparsing in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2.3.0)\n", 69 | "Collecting sklearn-pandas (from fastai==0.7.0)\n", 70 | " Downloading https://files.pythonhosted.org/packages/7e/9c/c94f46b40b86d2c77c46c4c1b858fc66c117b4390665eca28f2e0812db45/sklearn_pandas-1.7.0-py2.py3-none-any.whl\n", 71 | "Collecting html5lib (from fastai==0.7.0)\n", 72 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/a5/62/bbd2be0e7943ec8504b517e62bab011b4946e1258842bc159e5dfde15b96/html5lib-1.0.1-py2.py3-none-any.whl (117kB)\n", 73 | "\u001b[K 100% |████████████████████████████████| 122kB 31.2MB/s \n", 74 | "\u001b[?25hRequirement already satisfied: traitlets in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (4.3.2)\n", 75 | "Requirement already satisfied: pickleshare in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.7.5)\n", 76 | "Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.2.0)\n", 77 | "Collecting bcolz (from fastai==0.7.0)\n", 78 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/5c/4e/23942de9d5c0fb16f10335fa83e52b431bcb8c0d4a8419c9ac206268c279/bcolz-1.2.1.tar.gz (1.5MB)\n", 79 | "\u001b[K 100% |████████████████████████████████| 1.5MB 15.4MB/s \n", 80 | "\u001b[?25hRequirement already satisfied: entrypoints in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.2.3)\n", 81 | "Requirement already satisfied: certifi in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2018.10.15)\n", 82 | "Collecting jedi (from fastai==0.7.0)\n", 83 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/7a/1a/9bd24a185873b998611c2d8d4fb15cd5e8a879ead36355df7ee53e9111bf/jedi-0.13.1-py2.py3-none-any.whl (177kB)\n", 84 | "\u001b[K 100% |████████████████████████████████| 184kB 28.2MB/s \n", 85 | "\u001b[?25hCollecting torchvision (from fastai==0.7.0)\n", 86 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/ca/0d/f00b2885711e08bd71242ebe7b96561e6f6d01fdb4b9dcf4d37e2e13c5e1/torchvision-0.2.1-py2.py3-none-any.whl (54kB)\n", 87 | "\u001b[K 100% |████████████████████████████████| 61kB 25.4MB/s \n", 88 | "\u001b[?25hRequirement already satisfied: cycler in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.10.0)\n", 89 | "Requirement already satisfied: Jinja2 in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2.10)\n", 90 | "Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2.1.2)\n", 91 | "Collecting ipywidgets (from fastai==0.7.0)\n", 92 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/30/9a/a008c7b1183fac9e52066d80a379b3c64eab535bd9d86cdc29a0b766fd82/ipywidgets-7.4.2-py2.py3-none-any.whl (111kB)\n", 93 | "\u001b[K 100% |████████████████████████████████| 112kB 28.7MB/s \n", 94 | "\u001b[?25hCollecting plotnine (from fastai==0.7.0)\n", 95 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/84/02/b171c828560aea3a5da1efda464230dac3ef4f4834b88e0bd52ad14a08f0/plotnine-0.5.1-py2.py3-none-any.whl (3.6MB)\n", 96 | "\u001b[K 100% |████████████████████████████████| 3.6MB 10.7MB/s \n", 97 | "\u001b[?25hRequirement already satisfied: Pygments in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2.1.3)\n", 98 | "Requirement already satisfied: PyYAML in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (3.13)\n", 99 | "Collecting isoweek (from fastai==0.7.0)\n", 100 | " Downloading https://files.pythonhosted.org/packages/c2/d4/fe7e2637975c476734fcbf53776e650a29680194eb0dd21dbdc020ca92de/isoweek-1.3.3-py2.py3-none-any.whl\n", 101 | "Requirement already satisfied: seaborn in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.7.1)\n", 102 | "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2.5.3)\n", 103 | "Requirement already satisfied: wcwidth in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.1.7)\n", 104 | "Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (2018.7)\n", 105 | "Requirement already satisfied: Pillow in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (4.0.0)\n", 106 | "Collecting pandas-summary (from fastai==0.7.0)\n", 107 | " Downloading https://files.pythonhosted.org/packages/97/55/ea54109a4e7a8e7342bdf23e9382c858224263d984b0d95610568e564f59/pandas_summary-0.0.5-py2.py3-none-any.whl\n", 108 | "Requirement already satisfied: ptyprocess in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (0.6.0)\n", 109 | "Requirement already satisfied: tornado in /usr/local/lib/python3.6/dist-packages (from fastai==0.7.0) (4.5.3)\n", 110 | "Collecting torch<0.4 (from fastai==0.7.0)\n", 111 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/5b/a5/e8b50b55b1abac9f1e3346c4242f1e42a82d368a8442cbd50c532922f6c4/torch-0.3.1-cp36-cp36m-manylinux1_x86_64.whl (496.4MB)\n", 112 | "\u001b[K 100% |████████████████████████████████| 496.4MB 29kB/s \n", 113 | "\u001b[?25hCollecting jupyter-console (from jupyter->fastai==0.7.0)\n", 114 | " Downloading https://files.pythonhosted.org/packages/cb/ee/6374ae8c21b7d0847f9c3722dcdfac986b8e54fa9ad9ea66e1eb6320d2b8/jupyter_console-6.0.0-py2.py3-none-any.whl\n", 115 | "Requirement already satisfied: notebook in /usr/local/lib/python3.6/dist-packages (from jupyter->fastai==0.7.0) (5.2.2)\n", 116 | "Collecting qtconsole (from jupyter->fastai==0.7.0)\n", 117 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/e0/7a/8aefbc0ed078dec7951ac9a06dcd1869243ecd7bcbce26fa47bf5e469a8f/qtconsole-4.4.3-py2.py3-none-any.whl (113kB)\n", 118 | "\u001b[K 100% |████████████████████████████████| 122kB 29.6MB/s \n", 119 | "\u001b[?25hRequirement already satisfied: nbconvert in /usr/local/lib/python3.6/dist-packages (from jupyter->fastai==0.7.0) (5.4.0)\n", 120 | "Collecting pyarrow>=0.4.0 (from feather-format->fastai==0.7.0)\n", 121 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/36/94/23135312f97b20d6457294606fb70fad43ef93b7bffe567088ebe3623703/pyarrow-0.11.1-cp36-cp36m-manylinux1_x86_64.whl (11.6MB)\n", 122 | "\u001b[K 100% |████████████████████████████████| 11.6MB 3.2MB/s \n", 123 | "\u001b[?25hRequirement already satisfied: pexpect; sys_platform != \"win32\" in /usr/local/lib/python3.6/dist-packages (from ipython->fastai==0.7.0) (4.6.0)\n", 124 | "Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.6/dist-packages (from ipython->fastai==0.7.0) (40.6.2)\n", 125 | "Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in /usr/local/lib/python3.6/dist-packages (from ipython->fastai==0.7.0) (1.0.15)\n", 126 | "Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from torchtext->fastai==0.7.0) (2.18.4)\n", 127 | "Requirement already satisfied: jupyter-client in /usr/local/lib/python3.6/dist-packages (from ipykernel->fastai==0.7.0) (5.2.3)\n", 128 | "Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from bleach->fastai==0.7.0) (1.11.0)\n", 129 | "Requirement already satisfied: scikit-learn>=0.15.0 in /usr/local/lib/python3.6/dist-packages (from sklearn-pandas->fastai==0.7.0) (0.19.2)\n", 130 | "Collecting parso>=0.3.0 (from jedi->fastai==0.7.0)\n", 131 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/09/51/9c48a46334be50c13d25a3afe55fa05c445699304c5ad32619de953a2305/parso-0.3.1-py2.py3-none-any.whl (88kB)\n", 132 | "\u001b[K 100% |████████████████████████████████| 92kB 22.2MB/s \n", 133 | "\u001b[?25hRequirement already satisfied: nbformat>=4.2.0 in /usr/local/lib/python3.6/dist-packages (from ipywidgets->fastai==0.7.0) (4.4.0)\n", 134 | "Requirement already satisfied: statsmodels>=0.8.0 in /usr/local/lib/python3.6/dist-packages (from plotnine->fastai==0.7.0) (0.8.0)\n", 135 | "Collecting descartes>=1.1.0 (from plotnine->fastai==0.7.0)\n", 136 | " Downloading https://files.pythonhosted.org/packages/e5/b6/1ed2eb03989ae574584664985367ba70cd9cf8b32ee8cad0e8aaeac819f3/descartes-1.1.0-py3-none-any.whl\n", 137 | "Requirement already satisfied: patsy>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from plotnine->fastai==0.7.0) (0.5.1)\n", 138 | "Collecting mizani>=0.5.2 (from plotnine->fastai==0.7.0)\n", 139 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/10/3a/1d1c5563b6aeb5fffda694b70d649a0f728a112b79a66b85a6af4814a643/mizani-0.5.2-py2.py3-none-any.whl (58kB)\n", 140 | "\u001b[K 100% |████████████████████████████████| 61kB 19.5MB/s \n", 141 | "\u001b[?25hRequirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from Pillow->fastai==0.7.0) (0.46)\n", 142 | "Requirement already satisfied: terminado>=0.3.3; sys_platform != \"win32\" in /usr/local/lib/python3.6/dist-packages (from notebook->jupyter->fastai==0.7.0) (0.8.1)\n", 143 | "Requirement already satisfied: jupyter-core in /usr/local/lib/python3.6/dist-packages (from notebook->jupyter->fastai==0.7.0) (4.4.0)\n", 144 | "Requirement already satisfied: defusedxml in /usr/local/lib/python3.6/dist-packages (from nbconvert->jupyter->fastai==0.7.0) (0.5.0)\n", 145 | "Requirement already satisfied: mistune>=0.8.1 in /usr/local/lib/python3.6/dist-packages (from nbconvert->jupyter->fastai==0.7.0) (0.8.4)\n", 146 | "Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.6/dist-packages (from nbconvert->jupyter->fastai==0.7.0) (1.4.2)\n", 147 | "Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->torchtext->fastai==0.7.0) (2.6)\n", 148 | "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->torchtext->fastai==0.7.0) (3.0.4)\n", 149 | "Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->torchtext->fastai==0.7.0) (1.22)\n", 150 | "Collecting palettable (from mizani>=0.5.2->plotnine->fastai==0.7.0)\n", 151 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/56/8a/84537c0354f0d1f03bf644b71bf8e0a50db9c1294181905721a5f3efbf66/palettable-3.1.1-py2.py3-none-any.whl (77kB)\n", 152 | "\u001b[K 100% |████████████████████████████████| 81kB 24.1MB/s \n", 153 | "\u001b[?25hBuilding wheels for collected packages: feather-format, bcolz\n", 154 | " Running setup.py bdist_wheel for feather-format ... \u001b[?25l-\b \bdone\n", 155 | "\u001b[?25h Stored in directory: /root/.cache/pip/wheels/85/7d/12/2dfa5c0195f921ac935f5e8f27deada74972edc0ae9988a9c1\n", 156 | " Running setup.py bdist_wheel for bcolz ... \u001b[?25l-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \b-\b \b\\\b \b|\b \b/\b \bdone\n", 157 | "\u001b[?25h Stored in directory: /root/.cache/pip/wheels/9f/78/26/fb8c0acb91a100dc8914bf236c4eaa4b207cb876893c40b745\n", 158 | "Successfully built feather-format bcolz\n", 159 | "\u001b[31mjupyter-console 6.0.0 has requirement prompt-toolkit<2.1.0,>=2.0.0, but you'll have prompt-toolkit 1.0.15 which is incompatible.\u001b[0m\n", 160 | "\u001b[31mtorchvision 0.2.1 has requirement pillow>=4.1.1, but you'll have pillow 4.0.0 which is incompatible.\u001b[0m\n", 161 | "\u001b[31mmizani 0.5.2 has requirement pandas>=0.23.4, but you'll have pandas 0.22.0 which is incompatible.\u001b[0m\n", 162 | "\u001b[31mplotnine 0.5.1 has requirement matplotlib>=3.0.0, but you'll have matplotlib 2.1.2 which is incompatible.\u001b[0m\n", 163 | "\u001b[31mplotnine 0.5.1 has requirement pandas>=0.23.4, but you'll have pandas 0.22.0 which is incompatible.\u001b[0m\n", 164 | "Installing collected packages: jupyter-console, qtconsole, widgetsnbextension, ipywidgets, jupyter, graphviz, pyarrow, feather-format, torch, torchtext, sklearn-pandas, html5lib, bcolz, parso, jedi, torchvision, descartes, palettable, mizani, plotnine, isoweek, pandas-summary, fastai\n", 165 | "Successfully installed bcolz-1.2.1 descartes-1.1.0 fastai-0.7.0 feather-format-0.4.0 graphviz-0.10.1 html5lib-1.0.1 ipywidgets-7.4.2 isoweek-1.3.3 jedi-0.13.1 jupyter-1.0.0 jupyter-console-6.0.0 mizani-0.5.2 palettable-3.1.1 pandas-summary-0.0.5 parso-0.3.1 plotnine-0.5.1 pyarrow-0.11.1 qtconsole-4.4.3 sklearn-pandas-1.7.0 torch-0.3.1 torchtext-0.3.1 torchvision-0.2.1 widgetsnbextension-3.4.2\n", 166 | "Collecting torchtext==0.2.3\n", 167 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/78/90/474d5944d43001a6e72b9aaed5c3e4f77516fbef2317002da2096fd8b5ea/torchtext-0.2.3.tar.gz (42kB)\n", 168 | "\u001b[K 100% |████████████████████████████████| 51kB 1.9MB/s \n", 169 | "\u001b[?25hRequirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from torchtext==0.2.3) (4.28.1)\n", 170 | "Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from torchtext==0.2.3) (2.18.4)\n", 171 | "Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->torchtext==0.2.3) (1.22)\n", 172 | "Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->torchtext==0.2.3) (2.6)\n", 173 | "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->torchtext==0.2.3) (3.0.4)\n", 174 | "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->torchtext==0.2.3) (2018.10.15)\n", 175 | "Building wheels for collected packages: torchtext\n", 176 | " Running setup.py bdist_wheel for torchtext ... \u001b[?25l-\b \bdone\n", 177 | "\u001b[?25h Stored in directory: /root/.cache/pip/wheels/42/a6/f4/b267328bde6bb680094a0c173e8e5627ccc99543abded97204\n", 178 | "Successfully built torchtext\n", 179 | "Installing collected packages: torchtext\n", 180 | " Found existing installation: torchtext 0.3.1\n", 181 | " Uninstalling torchtext-0.3.1:\n", 182 | " Successfully uninstalled torchtext-0.3.1\n", 183 | "Successfully installed torchtext-0.2.3\n" 184 | ], 185 | "name": "stdout" 186 | } 187 | ] 188 | }, 189 | { 190 | "metadata": { 191 | "id": "i946v-PZmO4I", 192 | "colab_type": "code", 193 | "colab": {} 194 | }, 195 | "cell_type": "code", 196 | "source": [ 197 | "%reload_ext autoreload\n", 198 | "%autoreload 2\n", 199 | "%matplotlib inline\n", 200 | "\n", 201 | "from fastai.io import *\n", 202 | "from fastai.conv_learner import *\n", 203 | "\n", 204 | "from fastai.column_data import *" 205 | ], 206 | "execution_count": 0, 207 | "outputs": [] 208 | }, 209 | { 210 | "metadata": { 211 | "id": "4vl8n3ocmWhr", 212 | "colab_type": "code", 213 | "colab": {} 214 | }, 215 | "cell_type": "code", 216 | "source": [ 217 | "PATH='data/nietzsche/'" 218 | ], 219 | "execution_count": 0, 220 | "outputs": [] 221 | }, 222 | { 223 | "metadata": { 224 | "id": "SGYyuV-qmWKB", 225 | "colab_type": "code", 226 | "outputId": "8d80ea08-560d-4551-e971-98a4e3660276", 227 | "colab": { 228 | "base_uri": "https://localhost:8080/", 229 | "height": 54 230 | } 231 | }, 232 | "cell_type": "code", 233 | "source": [ 234 | "get_data(\"https://s3.amazonaws.com/text-datasets/nietzsche.txt\", f'{PATH}nietzsche.txt')\n", 235 | "text = open(f'{PATH}nietzsche.txt').read()\n", 236 | "print('corpus length:', len(text))" 237 | ], 238 | "execution_count": 5, 239 | "outputs": [ 240 | { 241 | "output_type": "stream", 242 | "text": [ 243 | "nietzsche.txt: 606kB [00:01, 355kB/s] " 244 | ], 245 | "name": "stderr" 246 | }, 247 | { 248 | "output_type": "stream", 249 | "text": [ 250 | "corpus length: 600893\n" 251 | ], 252 | "name": "stdout" 253 | }, 254 | { 255 | "output_type": "stream", 256 | "text": [ 257 | "\n" 258 | ], 259 | "name": "stderr" 260 | } 261 | ] 262 | }, 263 | { 264 | "metadata": { 265 | "id": "Ybi66gB7mWGg", 266 | "colab_type": "code", 267 | "outputId": "8244775a-7b0b-4bb9-a7c1-fd6afa71da3b", 268 | "colab": { 269 | "base_uri": "https://localhost:8080/", 270 | "height": 35 271 | } 272 | }, 273 | "cell_type": "code", 274 | "source": [ 275 | "chars = sorted(list(set(text)))\n", 276 | "vocab_size = len(chars)+1\n", 277 | "print('total chars:', vocab_size)" 278 | ], 279 | "execution_count": 6, 280 | "outputs": [ 281 | { 282 | "output_type": "stream", 283 | "text": [ 284 | "total chars: 85\n" 285 | ], 286 | "name": "stdout" 287 | } 288 | ] 289 | }, 290 | { 291 | "metadata": { 292 | "id": "Mja-YgeTmWBd", 293 | "colab_type": "code", 294 | "outputId": "206ec9b2-3229-4673-c443-d0b923b0ae5e", 295 | "colab": { 296 | "base_uri": "https://localhost:8080/", 297 | "height": 35 298 | } 299 | }, 300 | "cell_type": "code", 301 | "source": [ 302 | "chars.insert(0, \"\\0\")\n", 303 | "\n", 304 | "''.join(chars[1:-6])" 305 | ], 306 | "execution_count": 7, 307 | "outputs": [ 308 | { 309 | "output_type": "execute_result", 310 | "data": { 311 | "text/plain": [ 312 | "'\\n !\"\\'(),-.0123456789:;=?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxy'" 313 | ] 314 | }, 315 | "metadata": { 316 | "tags": [] 317 | }, 318 | "execution_count": 7 319 | } 320 | ] 321 | }, 322 | { 323 | "metadata": { 324 | "id": "_qDYPzMjmV7t", 325 | "colab_type": "code", 326 | "colab": {} 327 | }, 328 | "cell_type": "code", 329 | "source": [ 330 | "char_indices = {c: i for i, c in enumerate(chars)}\n", 331 | "indices_char = {i: c for i, c in enumerate(chars)}" 332 | ], 333 | "execution_count": 0, 334 | "outputs": [] 335 | }, 336 | { 337 | "metadata": { 338 | "id": "lsYODVhxmV2B", 339 | "colab_type": "code", 340 | "outputId": "87f1ae04-3701-42b8-a145-723bf2b7e37a", 341 | "colab": { 342 | "base_uri": "https://localhost:8080/", 343 | "height": 35 344 | } 345 | }, 346 | "cell_type": "code", 347 | "source": [ 348 | "idx = [char_indices[c] for c in text]\n", 349 | "\n", 350 | "idx[:10]" 351 | ], 352 | "execution_count": 9, 353 | "outputs": [ 354 | { 355 | "output_type": "execute_result", 356 | "data": { 357 | "text/plain": [ 358 | "[40, 42, 29, 30, 25, 27, 29, 1, 1, 1]" 359 | ] 360 | }, 361 | "metadata": { 362 | "tags": [] 363 | }, 364 | "execution_count": 9 365 | } 366 | ] 367 | }, 368 | { 369 | "metadata": { 370 | "id": "A4J1S4GmmVvz", 371 | "colab_type": "code", 372 | "outputId": "59fdd318-fdd9-41f1-c45c-0c0ba478faf2", 373 | "colab": { 374 | "base_uri": "https://localhost:8080/", 375 | "height": 35 376 | } 377 | }, 378 | "cell_type": "code", 379 | "source": [ 380 | "from torchtext import vocab, data\n", 381 | "\n", 382 | "from fastai.nlp import *\n", 383 | "from fastai.lm_rnn import *\n", 384 | "\n", 385 | "PATH='/content/data/nietzsche/'\n", 386 | "\n", 387 | "TRN_PATH = 'TRN/'\n", 388 | "VAL_PATH = 'VAL/'\n", 389 | "TRN = f'{PATH}{TRN_PATH}'\n", 390 | "VAL = f'{PATH}{VAL_PATH}'\n", 391 | "\n", 392 | "# Note: The student needs to practice her shell skills and prepare her own dataset before proceeding:\n", 393 | "# - trn/trn.txt (first 80% of nietzsche.txt)\n", 394 | "# - val/val.txt (last 20% of nietzsche.txt)\n", 395 | "\n", 396 | "%ls {PATH}" 397 | ], 398 | "execution_count": 10, 399 | "outputs": [ 400 | { 401 | "output_type": "stream", 402 | "text": [ 403 | "nietzsche.txt\n" 404 | ], 405 | "name": "stdout" 406 | } 407 | ] 408 | }, 409 | { 410 | "metadata": { 411 | "id": "FC3jsNAuno85", 412 | "colab_type": "code", 413 | "outputId": "f49cf861-c8aa-44f3-876a-4a23c05084c7", 414 | "colab": { 415 | "base_uri": "https://localhost:8080/", 416 | "height": 35 417 | } 418 | }, 419 | "cell_type": "code", 420 | "source": [ 421 | "ls " 422 | ], 423 | "execution_count": 18, 424 | "outputs": [ 425 | { 426 | "output_type": "stream", 427 | "text": [ 428 | "nietzsche.txt \u001b[0m\u001b[01;34mTRN\u001b[0m/ trn.txt \u001b[01;34mVAL\u001b[0m/ val.txt\n" 429 | ], 430 | "name": "stdout" 431 | } 432 | ] 433 | }, 434 | { 435 | "metadata": { 436 | "id": "ND9l4KK-mw8x", 437 | "colab_type": "code", 438 | "outputId": "2b7565ad-3283-4d5c-9423-a774dc67fd86", 439 | "colab": { 440 | "base_uri": "https://localhost:8080/", 441 | "height": 35 442 | } 443 | }, 444 | "cell_type": "code", 445 | "source": [ 446 | "%cd data/nietzsche/\n", 447 | "\n" 448 | ], 449 | "execution_count": 12, 450 | "outputs": [ 451 | { 452 | "output_type": "stream", 453 | "text": [ 454 | "/content/data/nietzsche\n" 455 | ], 456 | "name": "stdout" 457 | } 458 | ] 459 | }, 460 | { 461 | "metadata": { 462 | "id": "C8C5HX-jtt5-", 463 | "colab_type": "code", 464 | "colab": {} 465 | }, 466 | "cell_type": "code", 467 | "source": [ 468 | "!mkdir VAL\n", 469 | "!mkdir TRN" 470 | ], 471 | "execution_count": 0, 472 | "outputs": [] 473 | }, 474 | { 475 | "metadata": { 476 | "id": "FgTmoAVmmywB", 477 | "colab_type": "code", 478 | "colab": {} 479 | }, 480 | "cell_type": "code", 481 | "source": [ 482 | "f= open(\"trn.txt\",\"w+\")\n", 483 | "for i in range(len(text[:530000])):\n", 484 | " f.write(text[i])\n", 485 | "f.close() \n", 486 | "\n", 487 | "f= open(\"val.txt\",\"w+\")\n", 488 | "for i in range(len(text[530000:])):\n", 489 | " f.write(text[i])\n", 490 | "f.close() " 491 | ], 492 | "execution_count": 0, 493 | "outputs": [] 494 | }, 495 | { 496 | "metadata": { 497 | "id": "j0bUmGgRm2Xn", 498 | "colab_type": "code", 499 | "colab": { 500 | "base_uri": "https://localhost:8080/", 501 | "height": 54 502 | }, 503 | "outputId": "7ce705a1-1df4-4455-9a03-b9a931781e09" 504 | }, 505 | "cell_type": "code", 506 | "source": [ 507 | "# ! wc -m val.txt\n", 508 | "! mv -v /content/data/nietzsche/val.txt /content/data/nietzsche/VAL/\n", 509 | "! mv -v /content/data/nietzsche/trn.txt /content/data/nietzsche/TRN/" 510 | ], 511 | "execution_count": 19, 512 | "outputs": [ 513 | { 514 | "output_type": "stream", 515 | "text": [ 516 | "renamed '/content/data/nietzsche/val.txt' -> '/content/data/nietzsche/VAL/val.txt'\n", 517 | "renamed '/content/data/nietzsche/trn.txt' -> '/content/data/nietzsche/TRN/trn.txt'\n" 518 | ], 519 | "name": "stdout" 520 | } 521 | ] 522 | }, 523 | { 524 | "metadata": { 525 | "id": "WFAlGMc0m2S7", 526 | "colab_type": "code", 527 | "outputId": "1769f4d3-16b2-45aa-a3ef-eeee39916314", 528 | "colab": { 529 | "base_uri": "https://localhost:8080/", 530 | "height": 35 531 | } 532 | }, 533 | "cell_type": "code", 534 | "source": [ 535 | "TEXT = data.Field(lower=True, tokenize=list)\n", 536 | "bs=64; bptt=50; n_fac=100; n_hidden=1024\n", 537 | "\n", 538 | "FILES = dict(train=TRN_PATH, validation=VAL_PATH, test=VAL_PATH)\n", 539 | "md = LanguageModelData.from_text_files(PATH, TEXT, **FILES, bs=bs, bptt=bptt, min_freq=3)\n", 540 | "\n", 541 | "len(md.trn_dl), md.nt, len(md.trn_ds), len(md.trn_ds[0].text)" 542 | ], 543 | "execution_count": 41, 544 | "outputs": [ 545 | { 546 | "output_type": "execute_result", 547 | "data": { 548 | "text/plain": [ 549 | "(161, 55, 1, 521287)" 550 | ] 551 | }, 552 | "metadata": { 553 | "tags": [] 554 | }, 555 | "execution_count": 41 556 | } 557 | ] 558 | }, 559 | { 560 | "metadata": { 561 | "id": "Y6bVkS0lnYL0", 562 | "colab_type": "code", 563 | "colab": {} 564 | }, 565 | "cell_type": "code", 566 | "source": [ 567 | "# 512*2" 568 | ], 569 | "execution_count": 0, 570 | "outputs": [] 571 | }, 572 | { 573 | "metadata": { 574 | "id": "fMjA-hzHm2L8", 575 | "colab_type": "code", 576 | "colab": {} 577 | }, 578 | "cell_type": "code", 579 | "source": [ 580 | "from fastai import sgdr\n", 581 | "\n", 582 | "n_hidden=1024" 583 | ], 584 | "execution_count": 0, 585 | "outputs": [] 586 | }, 587 | { 588 | "metadata": { 589 | "id": "oMsaCXnGm2Fz", 590 | "colab_type": "code", 591 | "colab": {} 592 | }, 593 | "cell_type": "code", 594 | "source": [ 595 | "class CharSeqStatefulLSTM(nn.Module):\n", 596 | " def __init__(self, vocab_size, n_fac, bs, nl):\n", 597 | " super().__init__()\n", 598 | " self.vocab_size,self.nl = vocab_size,nl\n", 599 | " self.e = nn.Embedding(vocab_size, n_fac)\n", 600 | " self.rnn = nn.LSTM(n_fac, n_hidden, nl, dropout=0.5)\n", 601 | " self.l_out = nn.Linear(n_hidden, vocab_size)\n", 602 | " self.init_hidden(bs)\n", 603 | " \n", 604 | " def forward(self, cs):\n", 605 | " bs = cs[0].size(0)\n", 606 | " if self.h[0].size(1) != bs: self.init_hidden(bs)\n", 607 | " outp,h = self.rnn(self.e(cs), self.h)\n", 608 | " self.h = repackage_var(h)\n", 609 | " return F.log_softmax(self.l_out(outp), dim=-1).view(-1, self.vocab_size)\n", 610 | " \n", 611 | " def init_hidden(self, bs):\n", 612 | " self.h = (V(torch.zeros(self.nl, bs, n_hidden)),\n", 613 | " V(torch.zeros(self.nl, bs, n_hidden)))" 614 | ], 615 | "execution_count": 0, 616 | "outputs": [] 617 | }, 618 | { 619 | "metadata": { 620 | "id": "TeVU_mzvm1_o", 621 | "colab_type": "code", 622 | "colab": {} 623 | }, 624 | "cell_type": "code", 625 | "source": [ 626 | "m = CharSeqStatefulLSTM(md.nt, n_fac, 512, 3).cuda()\n", 627 | "lo = LayerOptimizer(optim.Adam, m, 1e-2, 1e-5)" 628 | ], 629 | "execution_count": 0, 630 | "outputs": [] 631 | }, 632 | { 633 | "metadata": { 634 | "id": "cHmNzQLHm16x", 635 | "colab_type": "code", 636 | "colab": {} 637 | }, 638 | "cell_type": "code", 639 | "source": [ 640 | "os.makedirs(f'{PATH}models', exist_ok=True)" 641 | ], 642 | "execution_count": 0, 643 | "outputs": [] 644 | }, 645 | { 646 | "metadata": { 647 | "id": "94g1SnAYm12M", 648 | "colab_type": "code", 649 | "outputId": "1fc93547-442c-49db-ce1b-9559ce90c670", 650 | "colab": { 651 | "base_uri": "https://localhost:8080/", 652 | "height": 109 653 | } 654 | }, 655 | "cell_type": "code", 656 | "source": [ 657 | "fit(m, md, 2, lo.opt, F.nll_loss)" 658 | ], 659 | "execution_count": 46, 660 | "outputs": [ 661 | { 662 | "output_type": "display_data", 663 | "data": { 664 | "application/vnd.jupyter.widget-view+json": { 665 | "model_id": "63e6d3e4e4164134bda6bd1f18edbb09", 666 | "version_minor": 0, 667 | "version_major": 2 668 | }, 669 | "text/plain": [ 670 | "HBox(children=(IntProgress(value=0, description='Epoch', max=2, style=ProgressStyle(description_width='initial…" 671 | ] 672 | }, 673 | "metadata": { 674 | "tags": [] 675 | } 676 | }, 677 | { 678 | "output_type": "stream", 679 | "text": [ 680 | "epoch trn_loss val_loss \n", 681 | " 0 3.058062 3.044033 \n", 682 | " 1 3.041689 3.044638 \n" 683 | ], 684 | "name": "stdout" 685 | }, 686 | { 687 | "output_type": "execute_result", 688 | "data": { 689 | "text/plain": [ 690 | "[array([3.04464])]" 691 | ] 692 | }, 693 | "metadata": { 694 | "tags": [] 695 | }, 696 | "execution_count": 46 697 | } 698 | ] 699 | }, 700 | { 701 | "metadata": { 702 | "id": "nqfTfBrGm1wk", 703 | "colab_type": "code", 704 | "outputId": "f766fca4-fab6-49e4-f8e1-ee690fef0c64", 705 | "colab": { 706 | "base_uri": "https://localhost:8080/", 707 | "height": 348 708 | } 709 | }, 710 | "cell_type": "code", 711 | "source": [ 712 | "on_end = lambda sched, cycle: save_model(m, f'{PATH}models/cyc_{cycle}')\n", 713 | "cb = [CosAnneal(lo, len(md.trn_dl), cycle_mult=2, on_cycle_end=on_end)]\n", 714 | "fit(m, md, 2**4-1, lo.opt, F.nll_loss, callbacks=cb)" 715 | ], 716 | "execution_count": 47, 717 | "outputs": [ 718 | { 719 | "output_type": "display_data", 720 | "data": { 721 | "application/vnd.jupyter.widget-view+json": { 722 | "model_id": "a7f06a8337b64227b410b0f7926c5e14", 723 | "version_minor": 0, 724 | "version_major": 2 725 | }, 726 | "text/plain": [ 727 | "HBox(children=(IntProgress(value=0, description='Epoch', max=15, style=ProgressStyle(description_width='initia…" 728 | ] 729 | }, 730 | "metadata": { 731 | "tags": [] 732 | } 733 | }, 734 | { 735 | "output_type": "stream", 736 | "text": [ 737 | "epoch trn_loss val_loss \n", 738 | " 0 3.016774 2.989205 \n", 739 | " 1 3.004507 2.997172 \n", 740 | " 2 2.940385 2.875598 \n", 741 | " 3 3.025433 3.003709 \n", 742 | " 4 2.844473 2.683357 \n", 743 | " 5 2.066826 1.826603 \n", 744 | " 6 1.743862 1.643534 \n", 745 | " 7 1.852061 1.697437 \n", 746 | " 8 1.712708 1.567653 \n", 747 | " 9 1.605037 1.470101 \n", 748 | " 10 1.517391 1.40102 \n", 749 | " 11 1.4405 1.334305 \n", 750 | " 12 1.372975 1.280644 \n", 751 | " 13 1.3177 1.238514 \n", 752 | " 14 1.290842 1.226868 \n" 753 | ], 754 | "name": "stdout" 755 | }, 756 | { 757 | "output_type": "execute_result", 758 | "data": { 759 | "text/plain": [ 760 | "[array([1.22687])]" 761 | ] 762 | }, 763 | "metadata": { 764 | "tags": [] 765 | }, 766 | "execution_count": 47 767 | } 768 | ] 769 | }, 770 | { 771 | "metadata": { 772 | "id": "rYFvB2Ovm1rT", 773 | "colab_type": "code", 774 | "outputId": "80cebd7c-1f8c-4374-d441-10298f547361", 775 | "colab": { 776 | "base_uri": "https://localhost:8080/", 777 | "height": 1229 778 | } 779 | }, 780 | "cell_type": "code", 781 | "source": [ 782 | "on_end = lambda sched, cycle: save_model(m, f'{PATH}models/cyc_{cycle}')\n", 783 | "cb = [CosAnneal(lo, len(md.trn_dl), cycle_mult=2, on_cycle_end=on_end)]\n", 784 | "fit(m, md, 2**6-1, lo.opt, F.nll_loss, callbacks=cb)" 785 | ], 786 | "execution_count": 48, 787 | "outputs": [ 788 | { 789 | "output_type": "display_data", 790 | "data": { 791 | "application/vnd.jupyter.widget-view+json": { 792 | "model_id": "7bcd5f5167b6418ab197da6e5d0f278e", 793 | "version_minor": 0, 794 | "version_major": 2 795 | }, 796 | "text/plain": [ 797 | "HBox(children=(IntProgress(value=0, description='Epoch', max=63, style=ProgressStyle(description_width='initia…" 798 | ] 799 | }, 800 | "metadata": { 801 | "tags": [] 802 | } 803 | }, 804 | { 805 | "output_type": "stream", 806 | "text": [ 807 | "epoch trn_loss val_loss \n", 808 | " 0 1.44048 1.313112 \n", 809 | " 1 1.488313 1.350004 \n", 810 | " 2 1.36552 1.267718 \n", 811 | " 3 1.488569 1.367849 \n", 812 | " 4 1.425236 1.302421 \n", 813 | " 5 1.334832 1.226359 \n", 814 | " 6 1.267407 1.196797 \n", 815 | " 7 1.476726 1.369066 \n", 816 | " 8 1.456075 1.341615 \n", 817 | " 9 1.408075 1.300758 \n", 818 | " 10 1.357125 1.252813 \n", 819 | " 11 1.306475 1.204362 \n", 820 | " 12 1.254089 1.154594 \n", 821 | " 13 1.202228 1.122787 \n", 822 | " 14 1.17207 1.11179 \n", 823 | " 15 1.457128 1.349205 \n", 824 | " 16 1.436052 1.320655 \n", 825 | " 17 1.41067 1.296875 \n", 826 | " 18 1.390619 1.289492 \n", 827 | " 19 1.368401 1.262586 \n", 828 | " 20 1.34629 1.249168 \n", 829 | " 21 1.319573 1.218542 \n", 830 | " 22 1.295882 1.193858 \n", 831 | " 23 1.264732 1.168474 \n", 832 | " 24 1.235027 1.142553 \n", 833 | " 25 1.197942 1.112822 \n", 834 | " 26 1.163695 1.076679 \n", 835 | " 27 1.127845 1.043957 \n", 836 | " 28 1.093791 1.015454 \n", 837 | " 29 1.061664 0.999674 \n", 838 | " 30 1.04863 0.99671 \n", 839 | " 31 1.407821 1.308871 \n", 840 | " 32 1.400106 1.283351 \n", 841 | " 33 1.380994 1.269118 \n", 842 | " 34 1.370093 1.258498 \n", 843 | " 35 1.361586 1.259069 \n", 844 | " 36 1.348636 1.241996 \n", 845 | " 37 1.339977 1.234151 \n", 846 | " 38 1.333243 1.225624 \n", 847 | " 39 1.323631 1.220659 \n", 848 | " 40 1.311093 1.208488 \n", 849 | " 41 1.302932 1.208082 \n", 850 | " 42 1.293679 1.189558 \n", 851 | " 43 1.280434 1.181382 \n", 852 | " 44 1.26867 1.169116 \n", 853 | " 45 1.256692 1.161918 \n", 854 | " 46 1.239744 1.145786 \n", 855 | " 47 1.223163 1.125951 \n", 856 | " 48 1.208923 1.114017 \n", 857 | " 49 1.190714 1.089468 \n", 858 | " 50 1.166025 1.072272 \n", 859 | " 51 1.140798 1.053369 \n", 860 | " 52 1.114408 1.027219 \n", 861 | " 53 1.087719 1.003101 \n", 862 | " 54 1.058354 0.970238 \n", 863 | " 55 1.029075 0.945873 \n", 864 | " 56 0.999371 0.91872 \n", 865 | " 57 0.988584 0.896895 \n", 866 | " 58 0.934806 0.868248 \n", 867 | " 59 0.924662 0.8472 \n", 868 | " 60 0.89058 0.829339 \n", 869 | " 61 0.901599 0.823408 \n", 870 | " 62 0.896875 0.824536 \n" 871 | ], 872 | "name": "stdout" 873 | }, 874 | { 875 | "output_type": "execute_result", 876 | "data": { 877 | "text/plain": [ 878 | "[array([0.82454])]" 879 | ] 880 | }, 881 | "metadata": { 882 | "tags": [] 883 | }, 884 | "execution_count": 48 885 | } 886 | ] 887 | }, 888 | { 889 | "metadata": { 890 | "id": "wd9dtdN4m1k1", 891 | "colab_type": "code", 892 | "colab": {} 893 | }, 894 | "cell_type": "code", 895 | "source": [ 896 | "def get_next(inp):\n", 897 | " idxs = TEXT.numericalize(inp)\n", 898 | " p = m(VV(idxs.transpose(0,1)))\n", 899 | " r = torch.multinomial(p[-1].exp(), 1)\n", 900 | " return TEXT.vocab.itos[to_np(r)[0]]" 901 | ], 902 | "execution_count": 0, 903 | "outputs": [] 904 | }, 905 | { 906 | "metadata": { 907 | "id": "LGMLp81Em1ce", 908 | "colab_type": "code", 909 | "colab": {} 910 | }, 911 | "cell_type": "code", 912 | "source": [ 913 | "def get_next_n(inp, n):\n", 914 | " res = inp\n", 915 | " for i in range(n):\n", 916 | " c = get_next(inp)\n", 917 | " res += c\n", 918 | " inp = inp[1:]+c\n", 919 | " return res" 920 | ], 921 | "execution_count": 0, 922 | "outputs": [] 923 | }, 924 | { 925 | "metadata": { 926 | "id": "UPvpc6vFnGUC", 927 | "colab_type": "code", 928 | "colab": { 929 | "base_uri": "https://localhost:8080/", 930 | "height": 55 931 | }, 932 | "outputId": "4a2331b3-2eed-41d3-a54f-f0b4121ddac7" 933 | }, 934 | "cell_type": "code", 935 | "source": [ 936 | "print(get_next_n('for thos', 400))" 937 | ], 938 | "execution_count": 51, 939 | "outputs": [ 940 | { 941 | "output_type": "stream", 942 | "text": [ 943 | "for those bringing. it betrays (happinessimpally, requirement, down unduchmile for it (would lie fol actions! what isworda wrings. in case gives himself and begin to it. the proper rus! it may belief in the dreads to the tow, somethings at things; itbestin at otherwism. does not some new [look at a distracting--it was that attempters; such arrangement, shame-posterity insomuch as ever of feiting upon the \n" 944 | ], 945 | "name": "stdout" 946 | } 947 | ] 948 | }, 949 | { 950 | "metadata": { 951 | "id": "OurXTuTSnGOg", 952 | "colab_type": "code", 953 | "colab": {} 954 | }, 955 | "cell_type": "code", 956 | "source": [ 957 | "" 958 | ], 959 | "execution_count": 0, 960 | "outputs": [] 961 | }, 962 | { 963 | "metadata": { 964 | "id": "3jhQ9JlqnGJq", 965 | "colab_type": "code", 966 | "colab": {} 967 | }, 968 | "cell_type": "code", 969 | "source": [ 970 | "" 971 | ], 972 | "execution_count": 0, 973 | "outputs": [] 974 | }, 975 | { 976 | "metadata": { 977 | "id": "PHrgjqZenGCu", 978 | "colab_type": "code", 979 | "colab": {} 980 | }, 981 | "cell_type": "code", 982 | "source": [ 983 | "" 984 | ], 985 | "execution_count": 0, 986 | "outputs": [] 987 | }, 988 | { 989 | "metadata": { 990 | "id": "lBC6c9UqnF8f", 991 | "colab_type": "code", 992 | "colab": {} 993 | }, 994 | "cell_type": "code", 995 | "source": [ 996 | "" 997 | ], 998 | "execution_count": 0, 999 | "outputs": [] 1000 | }, 1001 | { 1002 | "metadata": { 1003 | "id": "aTrGKWYdnFy5", 1004 | "colab_type": "code", 1005 | "colab": {} 1006 | }, 1007 | "cell_type": "code", 1008 | "source": [ 1009 | "" 1010 | ], 1011 | "execution_count": 0, 1012 | "outputs": [] 1013 | }, 1014 | { 1015 | "metadata": { 1016 | "id": "Fd-N04E0nFrl", 1017 | "colab_type": "code", 1018 | "colab": {} 1019 | }, 1020 | "cell_type": "code", 1021 | "source": [ 1022 | "" 1023 | ], 1024 | "execution_count": 0, 1025 | "outputs": [] 1026 | }, 1027 | { 1028 | "metadata": { 1029 | "id": "xFmPep6-mRmw", 1030 | "colab_type": "code", 1031 | "colab": {} 1032 | }, 1033 | "cell_type": "code", 1034 | "source": [ 1035 | "" 1036 | ], 1037 | "execution_count": 0, 1038 | "outputs": [] 1039 | } 1040 | ] 1041 | } -------------------------------------------------------------------------------- /Nietzsche/nietzsche: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Planet Earth Competition/Multi-label_classificn: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Pytorch_Expt/1) Pytorch-Matrices and Reproducibility.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Collecting torch==0.4.0 from http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl\n", 13 | " Downloading http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl (31.8MB)\n", 14 | "Installing collected packages: torch\n", 15 | "Successfully installed torch-0.4.0\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "! pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl " 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Collecting torchvision\n", 33 | " Downloading https://files.pythonhosted.org/packages/ca/0d/f00b2885711e08bd71242ebe7b96561e6f6d01fdb4b9dcf4d37e2e13c5e1/torchvision-0.2.1-py2.py3-none-any.whl (54kB)\n", 34 | "Requirement already satisfied: torch in c:\\users\\ashis.panda\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages (from torchvision) (0.4.0)\n", 35 | "Requirement already satisfied: pillow>=4.1.1 in c:\\users\\ashis.panda\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages (from torchvision) (4.2.1)\n", 36 | "Requirement already satisfied: six in c:\\users\\ashis.panda\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages (from torchvision) (1.10.0)\n", 37 | "Requirement already satisfied: numpy in c:\\users\\ashis.panda\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages (from torchvision) (1.13.1)\n", 38 | "Requirement already satisfied: olefile in c:\\users\\ashis.panda\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages (from pillow>=4.1.1->torchvision) (0.44)\n", 39 | "Installing collected packages: torchvision\n", 40 | "Successfully installed torchvision-0.2.1\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "! pip3 install torchvision" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "import torch\n", 57 | "import torchvision" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 5, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "tensor([[ 1., 1.],\n", 69 | " [ 1., 1.]])" 70 | ] 71 | }, 72 | "execution_count": 5, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "torch.ones(2,2)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 7, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "tensor([[ 0.3772, 0.5237],\n", 90 | " [ 0.2242, 0.5101]])" 91 | ] 92 | }, 93 | "execution_count": 7, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "torch.rand(2,2)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "# Generating Seed\n", 107 | "##### For the purpose of reproducibility" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 10, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "tensor([[ 0.4963, 0.7682],\n", 119 | " [ 0.0885, 0.1320]])" 120 | ] 121 | }, 122 | "execution_count": 10, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "torch.manual_seed(0)\n", 129 | "torch.rand(2,2)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "#### How to do it in GPU ? " 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 12, 142 | "metadata": { 143 | "collapsed": true 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "if torch.cuda.is_available():\n", 148 | " torch.cuda.manual_seed_all(0)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": { 155 | "collapsed": true 156 | }, 157 | "outputs": [], 158 | "source": [] 159 | } 160 | ], 161 | "metadata": { 162 | "kernelspec": { 163 | "display_name": "Python 3", 164 | "language": "python", 165 | "name": "python3" 166 | }, 167 | "language_info": { 168 | "codemirror_mode": { 169 | "name": "ipython", 170 | "version": 3 171 | }, 172 | "file_extension": ".py", 173 | "mimetype": "text/x-python", 174 | "name": "python", 175 | "nbconvert_exporter": "python", 176 | "pygments_lexer": "ipython3", 177 | "version": "3.6.2" 178 | } 179 | }, 180 | "nbformat": 4, 181 | "nbformat_minor": 2 182 | } 183 | -------------------------------------------------------------------------------- /Pytorch_Expt/2) Connection between numpy and torch and vice-versa.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "import torch" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 9, 18 | "metadata": { 19 | "scrolled": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "np_arr=np.ones((2,2))" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 10, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "array([[ 1., 1.],\n", 35 | " [ 1., 1.]])" 36 | ] 37 | }, 38 | "execution_count": 10, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "np_arr" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 11, 50 | "metadata": { 51 | "collapsed": true 52 | }, 53 | "outputs": [], 54 | "source": [ 55 | "tor_tensor=torch.from_numpy(np_arr)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 12, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "tensor([[ 1., 1.],\n", 67 | " [ 1., 1.]], dtype=torch.float64)" 68 | ] 69 | }, 70 | "execution_count": 12, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "tor_tensor" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 13, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "torch.Tensor" 88 | ] 89 | }, 90 | "execution_count": 13, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "type(tor_tensor)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 17, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "ename": "TypeError", 106 | "evalue": "can't convert np.ndarray of type numpy.int8. The only supported types are: double, float, float16, int64, int32, and uint8.", 107 | "output_type": "error", 108 | "traceback": [ 109 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 110 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 111 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mnp_arr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mones\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mint8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtor_tensor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfrom_numpy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp_arr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 112 | "\u001b[1;31mTypeError\u001b[0m: can't convert np.ndarray of type numpy.int8. The only supported types are: double, float, float16, int64, int32, and uint8." 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "np_arr=np.ones((2,2),dtype=np.int8)\n", 118 | "tor_tensor=torch.from_numpy(np_arr)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 18, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [ 129 | "# The only supported types are: double, float, float16, int64, int32, and uint8.\n", 130 | "# Only compatible ones with torch" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "# Torch to numpy" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 21, 143 | "metadata": { 144 | "collapsed": true 145 | }, 146 | "outputs": [], 147 | "source": [ 148 | "tor_tensor=torch.ones(2,2)\n", 149 | "tor_2_np=tor_tensor.numpy()" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 23, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "numpy.ndarray" 161 | ] 162 | }, 163 | "execution_count": 23, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "type(tor_2_np)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": { 176 | "collapsed": true 177 | }, 178 | "outputs": [], 179 | "source": [] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.6.2" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 2 203 | } 204 | -------------------------------------------------------------------------------- /Pytorch_Expt/3) CPU to GPU toggling and vice-versa.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### TORCH TENSOR HAS TO BE ON GPU FOR MODELS TO RUN SMOOTHLY" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 5, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import torch\n", 19 | "import numpy" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "# When we create torch tensor by-default its on CPU . To move it to GPU , type .cuda()" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 7, 36 | "metadata": { 37 | "collapsed": true 38 | }, 39 | "outputs": [], 40 | "source": [ 41 | "tor_tensor=torch.ones(2,2)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 12, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "# To operate on GPU\n", 51 | "if torch.cuda.is_available():\n", 52 | " tor_tensor.cuda()" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 14, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "tensor([[ 1., 1.],\n", 64 | " [ 1., 1.]])" 65 | ] 66 | }, 67 | "execution_count": 14, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "# To operate on CPU\n", 74 | "tor_tensor.cpu()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": { 81 | "collapsed": true 82 | }, 83 | "outputs": [], 84 | "source": [] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.6.2" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 2 108 | } 109 | -------------------------------------------------------------------------------- /Pytorch_Expt/4) Torch Tensor operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import torch\n", 12 | "import numpy" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 8, 18 | "metadata": { 19 | "collapsed": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "a=torch.ones(2,2)\n", 24 | "b=torch.ones(2,2)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 10, 30 | "metadata": { 31 | "collapsed": true 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "# Reshaping" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 11, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "tensor([ 1., 1., 1., 1.])" 47 | ] 48 | }, 49 | "execution_count": 11, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "# If we need 4 columns\n", 56 | "a.view(4)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 12, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "torch.Size([4])" 68 | ] 69 | }, 70 | "execution_count": 12, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "\n", 77 | "a.view(4).size()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 13, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "tensor([[ 2., 2.],\n", 89 | " [ 2., 2.]])" 90 | ] 91 | }, 92 | "execution_count": 13, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "# Element Addition/\n", 99 | "\n", 100 | "a+b" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 16, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "c=torch.add(a,b)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 15, 115 | "metadata": { 116 | "collapsed": true 117 | }, 118 | "outputs": [], 119 | "source": [ 120 | "# In-Place addition" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 17, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "tensor([[ 3., 3.],\n", 132 | " [ 3., 3.]])" 133 | ] 134 | }, 135 | "execution_count": 17, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "c.add_(a)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 19, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "tensor([[ 3., 3.],\n", 153 | " [ 3., 3.]])" 154 | ] 155 | }, 156 | "execution_count": 19, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "c \n", 163 | "# c no more matrix of 2" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 20, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "tensor([[ 0., 0.],\n", 175 | " [ 0., 0.]])" 176 | ] 177 | }, 178 | "execution_count": 20, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "a-b" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 21, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "tensor([[ 0., 0.],\n", 196 | " [ 0., 0.]])" 197 | ] 198 | }, 199 | "execution_count": 21, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [ 205 | "a.sub(b)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 22, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/plain": [ 216 | "tensor([[ 0., 0.],\n", 217 | " [ 0., 0.]])" 218 | ] 219 | }, 220 | "execution_count": 22, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ], 225 | "source": [ 226 | "a.sub_(b)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 23, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "tensor([[ 0., 0.],\n", 238 | " [ 0., 0.]])" 239 | ] 240 | }, 241 | "execution_count": 23, 242 | "metadata": {}, 243 | "output_type": "execute_result" 244 | } 245 | ], 246 | "source": [ 247 | "a*b" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 25, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "tensor([[ 0., 0.],\n", 259 | " [ 0., 0.]])" 260 | ] 261 | }, 262 | "execution_count": 25, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "torch.mul(a,b)" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 26, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "tensor([[ 0., 0.],\n", 280 | " [ 0., 0.]])" 281 | ] 282 | }, 283 | "execution_count": 26, 284 | "metadata": {}, 285 | "output_type": "execute_result" 286 | } 287 | ], 288 | "source": [ 289 | "a.mul_(b)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 28, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "data": { 299 | "text/plain": [ 300 | "tensor([[ 0., 0.],\n", 301 | " [ 0., 0.]])" 302 | ] 303 | }, 304 | "execution_count": 28, 305 | "metadata": {}, 306 | "output_type": "execute_result" 307 | } 308 | ], 309 | "source": [ 310 | "a/b" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 29, 316 | "metadata": {}, 317 | "outputs": [ 318 | { 319 | "data": { 320 | "text/plain": [ 321 | "tensor([[ 0., 0.],\n", 322 | " [ 0., 0.]])" 323 | ] 324 | }, 325 | "execution_count": 29, 326 | "metadata": {}, 327 | "output_type": "execute_result" 328 | } 329 | ], 330 | "source": [ 331 | "torch.div(a,b)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 30, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "data": { 341 | "text/plain": [ 342 | "tensor([[ 0., 0.],\n", 343 | " [ 0., 0.]])" 344 | ] 345 | }, 346 | "execution_count": 30, 347 | "metadata": {}, 348 | "output_type": "execute_result" 349 | } 350 | ], 351 | "source": [ 352 | "a.div_(b)" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "# Tensor Mean\n" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 36, 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "data": { 369 | "text/plain": [ 370 | "torch.Size([2])" 371 | ] 372 | }, 373 | "execution_count": 36, 374 | "metadata": {}, 375 | "output_type": "execute_result" 376 | } 377 | ], 378 | "source": [ 379 | "a=torch.tensor([1.0,2.0])\n", 380 | "a.size()" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 39, 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "data": { 390 | "text/plain": [ 391 | "tensor(1.5000)" 392 | ] 393 | }, 394 | "execution_count": 39, 395 | "metadata": {}, 396 | "output_type": "execute_result" 397 | } 398 | ], 399 | "source": [ 400 | "a.mean(dim=0)" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 42, 406 | "metadata": {}, 407 | "outputs": [ 408 | { 409 | "data": { 410 | "text/plain": [ 411 | "torch.Size([2, 2])" 412 | ] 413 | }, 414 | "execution_count": 42, 415 | "metadata": {}, 416 | "output_type": "execute_result" 417 | } 418 | ], 419 | "source": [ 420 | "a=torch.Tensor([[1.0,2.0],[3.0,5.0]])\n", 421 | "a.size()" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 43, 427 | "metadata": {}, 428 | "outputs": [ 429 | { 430 | "data": { 431 | "text/plain": [ 432 | "tensor([ 2.0000, 3.5000])" 433 | ] 434 | }, 435 | "execution_count": 43, 436 | "metadata": {}, 437 | "output_type": "execute_result" 438 | } 439 | ], 440 | "source": [ 441 | "a.mean(dim=0)" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": 44, 447 | "metadata": {}, 448 | "outputs": [ 449 | { 450 | "data": { 451 | "text/plain": [ 452 | "tensor([ 1.5000, 4.0000])" 453 | ] 454 | }, 455 | "execution_count": 44, 456 | "metadata": {}, 457 | "output_type": "execute_result" 458 | } 459 | ], 460 | "source": [ 461 | "a.mean(dim=1)" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 45, 467 | "metadata": {}, 468 | "outputs": [ 469 | { 470 | "data": { 471 | "text/plain": [ 472 | "tensor([ 1.4142, 2.1213])" 473 | ] 474 | }, 475 | "execution_count": 45, 476 | "metadata": {}, 477 | "output_type": "execute_result" 478 | } 479 | ], 480 | "source": [ 481 | "a.std(dim=0)" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 47, 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "tensor([ 0.7071, 1.4142])" 493 | ] 494 | }, 495 | "execution_count": 47, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "a.std(dim=1)" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": null, 507 | "metadata": { 508 | "collapsed": true 509 | }, 510 | "outputs": [], 511 | "source": [] 512 | } 513 | ], 514 | "metadata": { 515 | "kernelspec": { 516 | "display_name": "Python 3", 517 | "language": "python", 518 | "name": "python3" 519 | }, 520 | "language_info": { 521 | "codemirror_mode": { 522 | "name": "ipython", 523 | "version": 3 524 | }, 525 | "file_extension": ".py", 526 | "mimetype": "text/x-python", 527 | "name": "python", 528 | "nbconvert_exporter": "python", 529 | "pygments_lexer": "ipython3", 530 | "version": "3.6.2" 531 | } 532 | }, 533 | "nbformat": 4, 534 | "nbformat_minor": 2 535 | } 536 | -------------------------------------------------------------------------------- /Pytorch_Expt/5) Variables.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "# Variables are wrapper around tensor that allows it to accumulate its gradient" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 5, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "import torch\n", 21 | "from torch.autograd import Variable" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 7, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "tensor([[ 1., 1.],\n", 33 | " [ 1., 1.]])" 34 | ] 35 | }, 36 | "execution_count": 7, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "a=Variable(torch.ones(2,2),requires_grad=True)\n", 43 | "a" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 8, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "tensor([[ 1., 1.],\n", 55 | " [ 1., 1.]])" 56 | ] 57 | }, 58 | "execution_count": 8, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "torch.ones(2,2)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 9, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "tensor([[ 1., 1.],\n", 76 | " [ 1., 1.]])" 77 | ] 78 | }, 79 | "execution_count": 9, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "b=Variable(torch.ones(2,2),requires_grad=True)\n", 86 | "b" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 12, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [ 97 | "##### Behaves same as tensor" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 11, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "tensor([[ 2., 2.],\n", 109 | " [ 2., 2.]])" 110 | ] 111 | }, 112 | "execution_count": 11, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "a+b" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 13, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [ 129 | "#### Where it behaves differently" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 29, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "tensor(1.)" 141 | ] 142 | }, 143 | "execution_count": 29, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "x=Variable(torch.ones(2),requires_grad=True)\n", 150 | "x[1]" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 30, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "tensor([ 1., 1.])" 162 | ] 163 | }, 164 | "execution_count": 30, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "x" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 31, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "tensor([ 20., 20.])" 182 | ] 183 | }, 184 | "execution_count": 31, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "y=5*(x+1)**2\n", 191 | "y" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 32, 197 | "metadata": { 198 | "collapsed": true 199 | }, 200 | "outputs": [], 201 | "source": [ 202 | "o=0.5*torch.sum(y)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 33, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "tensor(20.)" 214 | ] 215 | }, 216 | "execution_count": 33, 217 | "metadata": {}, 218 | "output_type": "execute_result" 219 | } 220 | ], 221 | "source": [ 222 | "o" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 34, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "o.backward()" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 35, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "tensor([ 10., 10.])" 243 | ] 244 | }, 245 | "execution_count": 35, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "x.grad" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": { 258 | "collapsed": true 259 | }, 260 | "outputs": [], 261 | "source": [] 262 | } 263 | ], 264 | "metadata": { 265 | "kernelspec": { 266 | "display_name": "Python 3", 267 | "language": "python", 268 | "name": "python3" 269 | }, 270 | "language_info": { 271 | "codemirror_mode": { 272 | "name": "ipython", 273 | "version": 3 274 | }, 275 | "file_extension": ".py", 276 | "mimetype": "text/x-python", 277 | "name": "python", 278 | "nbconvert_exporter": "python", 279 | "pygments_lexer": "ipython3", 280 | "version": "3.6.2" 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 2 285 | } 286 | -------------------------------------------------------------------------------- /Pytorch_Expt/6) Linear Regression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import torch\n", 12 | "import numpy as np" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 5, 18 | "metadata": { 19 | "collapsed": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "x=[i for i in range(11)]" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 6, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "(11,)" 35 | ] 36 | }, 37 | "execution_count": 6, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "x=np.array(x,dtype=np.float32)\n", 44 | "x.shape" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 27, 50 | "metadata": { 51 | "collapsed": true 52 | }, 53 | "outputs": [], 54 | "source": [ 55 | "x=x.reshape(-1,1)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 9, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "y=[2*i+1 for i in x]\n", 65 | "y=np.array(y,dtype=np.float32)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 10, 71 | "metadata": { 72 | "collapsed": true 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "y=y.reshape(-1,1)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 12, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "(11, 1)" 88 | ] 89 | }, 90 | "execution_count": 12, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "y.shape" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 14, 102 | "metadata": { 103 | "collapsed": true 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "import torch.nn as nn #Helps in building our linear regression model\n", 108 | "from torch.autograd import Variable" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 15, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "class LinearRegressionModel(nn.Module):\n", 120 | " def __init__(self,input_size,output_size):\n", 121 | " super(LinearRegressionModel,self).__init__()\n", 122 | " self.Linear=nn.Linear(input_dim,output_dim)\n", 123 | " \n", 124 | " def forward(self,x):\n", 125 | " out=self.Linear(x)\n", 126 | " return out\n", 127 | " " 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 16, 133 | "metadata": { 134 | "collapsed": true 135 | }, 136 | "outputs": [], 137 | "source": [ 138 | "input_dim=1;output_dim=1\n", 139 | "model=LinearRegressionModel(input_dim,output_dim)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 17, 145 | "metadata": { 146 | "collapsed": true 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "criterion=nn.MSELoss()\n", 151 | "criterion()" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 22, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "lr=.01\n", 161 | "optimizer=torch.optim.SGD(model.parameters(),lr=lr)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 34, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "epoch 1,loss 0.0016315259272232652\n", 174 | "epoch 2,loss 0.0016133084427565336\n", 175 | "epoch 3,loss 0.0015953128458932042\n", 176 | "epoch 4,loss 0.0015774951316416264\n", 177 | "epoch 5,loss 0.0015598710160702467\n", 178 | "epoch 6,loss 0.001542450045235455\n", 179 | "epoch 7,loss 0.0015252376906573772\n", 180 | "epoch 8,loss 0.001508189714513719\n", 181 | "epoch 9,loss 0.0014913371996954083\n", 182 | "epoch 10,loss 0.0014747177483513951\n", 183 | "epoch 11,loss 0.001458222744986415\n", 184 | "epoch 12,loss 0.0014419540530070662\n", 185 | "epoch 13,loss 0.0014258642913773656\n", 186 | "epoch 14,loss 0.001409929129295051\n", 187 | "epoch 15,loss 0.0013941898941993713\n", 188 | "epoch 16,loss 0.001378609100356698\n", 189 | "epoch 17,loss 0.0013632270274683833\n", 190 | "epoch 18,loss 0.00134799734223634\n", 191 | "epoch 19,loss 0.001332932384684682\n", 192 | "epoch 20,loss 0.0013180618407204747\n", 193 | "epoch 21,loss 0.001303332974202931\n", 194 | "epoch 22,loss 0.0012887964257970452\n", 195 | "epoch 23,loss 0.0012744003906846046\n", 196 | "epoch 24,loss 0.0012601787457242608\n", 197 | "epoch 25,loss 0.00124610576312989\n", 198 | "epoch 26,loss 0.0012321937829256058\n", 199 | "epoch 27,loss 0.0012184069491922855\n", 200 | "epoch 28,loss 0.0012048074277117848\n", 201 | "epoch 29,loss 0.0011913711205124855\n", 202 | "epoch 30,loss 0.0011780587956309319\n", 203 | "epoch 31,loss 0.0011649099178612232\n", 204 | "epoch 32,loss 0.0011518904939293861\n", 205 | "epoch 33,loss 0.0011390205472707748\n", 206 | "epoch 34,loss 0.0011263111373409629\n", 207 | "epoch 35,loss 0.0011137353722006083\n", 208 | "epoch 36,loss 0.0011012795148417354\n", 209 | "epoch 37,loss 0.0010890086414292455\n", 210 | "epoch 38,loss 0.0010768395150080323\n", 211 | "epoch 39,loss 0.0010648125316947699\n", 212 | "epoch 40,loss 0.0010529140708968043\n", 213 | "epoch 41,loss 0.0010411525145173073\n", 214 | "epoch 42,loss 0.0010295481188222766\n", 215 | "epoch 43,loss 0.0010180381359532475\n", 216 | "epoch 44,loss 0.0010066633112728596\n", 217 | "epoch 45,loss 0.0009954205015674233\n", 218 | "epoch 46,loss 0.0009843084262683988\n", 219 | "epoch 47,loss 0.0009733210899867117\n", 220 | "epoch 48,loss 0.0009624563390389085\n", 221 | "epoch 49,loss 0.0009516876307316124\n", 222 | "epoch 50,loss 0.0009410800412297249\n", 223 | "epoch 51,loss 0.0009305551066063344\n", 224 | "epoch 52,loss 0.0009201742941513658\n", 225 | "epoch 53,loss 0.0009098873124457896\n", 226 | "epoch 54,loss 0.0008997339755296707\n", 227 | "epoch 55,loss 0.0008896944927982986\n", 228 | "epoch 56,loss 0.0008797584450803697\n", 229 | "epoch 57,loss 0.0008699255413375795\n", 230 | "epoch 58,loss 0.0008602034067735076\n", 231 | "epoch 59,loss 0.0008506214362569153\n", 232 | "epoch 60,loss 0.0008411132148467004\n", 233 | "epoch 61,loss 0.0008317126194015145\n", 234 | "epoch 62,loss 0.000822423433419317\n", 235 | "epoch 63,loss 0.0008132521179504693\n", 236 | "epoch 64,loss 0.0008041519904509187\n", 237 | "epoch 65,loss 0.0007951901643536985\n", 238 | "epoch 66,loss 0.0007863030186854303\n", 239 | "epoch 67,loss 0.0007775260019116104\n", 240 | "epoch 68,loss 0.0007688467740081251\n", 241 | "epoch 69,loss 0.0007602537516504526\n", 242 | "epoch 70,loss 0.0007517725462093949\n", 243 | "epoch 71,loss 0.0007433712598867714\n", 244 | "epoch 72,loss 0.0007350765517912805\n", 245 | "epoch 73,loss 0.0007268680492416024\n", 246 | "epoch 74,loss 0.0007187444134615362\n", 247 | "epoch 75,loss 0.0007107233395799994\n", 248 | "epoch 76,loss 0.0007027967367321253\n", 249 | "epoch 77,loss 0.0006949385278858244\n", 250 | "epoch 78,loss 0.0006871753139421344\n", 251 | "epoch 79,loss 0.00067951186792925\n", 252 | "epoch 80,loss 0.0006719142547808588\n", 253 | "epoch 81,loss 0.0006644079694524407\n", 254 | "epoch 82,loss 0.000656989635899663\n", 255 | "epoch 83,loss 0.0006496643181890249\n", 256 | "epoch 84,loss 0.0006424146122299135\n", 257 | "epoch 85,loss 0.0006352357449941337\n", 258 | "epoch 86,loss 0.0006281389505602419\n", 259 | "epoch 87,loss 0.0006211238214746118\n", 260 | "epoch 88,loss 0.0006141876219771802\n", 261 | "epoch 89,loss 0.0006073332042433321\n", 262 | "epoch 90,loss 0.0006005429313518107\n", 263 | "epoch 91,loss 0.0005938505055382848\n", 264 | "epoch 92,loss 0.0005872133770026267\n", 265 | "epoch 93,loss 0.0005806606495752931\n", 266 | "epoch 94,loss 0.0005741675267927349\n", 267 | "epoch 95,loss 0.0005677519948221743\n", 268 | "epoch 96,loss 0.0005614099209196866\n", 269 | "epoch 97,loss 0.0005551419453695416\n", 270 | "epoch 98,loss 0.0005489459726959467\n", 271 | "epoch 99,loss 0.0005428239237517118\n", 272 | "epoch 100,loss 0.0005367445410229266\n" 273 | ] 274 | }, 275 | { 276 | "name": "stderr", 277 | "output_type": "stream", 278 | "text": [ 279 | "C:\\Users\\ashis.panda\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:22: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n" 280 | ] 281 | } 282 | ], 283 | "source": [ 284 | "epochs=100\n", 285 | "for ep in range(epochs):\n", 286 | " ep+=1\n", 287 | " inputs=Variable(torch.from_numpy(x))\n", 288 | " labels=Variable(torch.from_numpy(y))\n", 289 | " \n", 290 | "# Clear gradients wrt every parameter in each epoch\n", 291 | " optimizer.zero_grad()\n", 292 | " \n", 293 | "# y=a*x+b . The forward function is being called here\n", 294 | " outputs=model(inputs)\n", 295 | " \n", 296 | "# Calculate loss\n", 297 | " loss=criterion(outputs,labels)\n", 298 | " \n", 299 | "# Calculate backpropagation\n", 300 | " loss.backward()\n", 301 | " \n", 302 | "# Update parameters\n", 303 | " optimizer.step()\n", 304 | " \n", 305 | " print('epoch {},loss {}'.format(ep,loss.data[0]))" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 43, 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "predicted=model(Variable(torch.from_numpy(x))).data.numpy()" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 44, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "array([[ 0.9569028 ],\n", 326 | " [ 2.96310925],\n", 327 | " [ 4.96931601],\n", 328 | " [ 6.97552252],\n", 329 | " [ 8.98172855],\n", 330 | " [ 10.98793507],\n", 331 | " [ 12.99414158],\n", 332 | " [ 15.00034809],\n", 333 | " [ 17.00655556],\n", 334 | " [ 19.01276207],\n", 335 | " [ 21.01896858]], dtype=float32)" 336 | ] 337 | }, 338 | "execution_count": 44, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "predicted" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 45, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "array([[ 1.],\n", 356 | " [ 3.],\n", 357 | " [ 5.],\n", 358 | " [ 7.],\n", 359 | " [ 9.],\n", 360 | " [ 11.],\n", 361 | " [ 13.],\n", 362 | " [ 15.],\n", 363 | " [ 17.],\n", 364 | " [ 19.],\n", 365 | " [ 21.]], dtype=float32)" 366 | ] 367 | }, 368 | "execution_count": 45, 369 | "metadata": {}, 370 | "output_type": "execute_result" 371 | } 372 | ], 373 | "source": [ 374 | "y" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 46, 380 | "metadata": { 381 | "collapsed": true 382 | }, 383 | "outputs": [], 384 | "source": [ 385 | "# We can see predicted values are near about close to actual values" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 49, 391 | "metadata": { 392 | "collapsed": true 393 | }, 394 | "outputs": [], 395 | "source": [ 396 | "save_model=True\n", 397 | "if save_model is True:\n", 398 | " torch.save(model.state_dict(),'practice.pkl')\n", 399 | " \n", 400 | "# This will save the model parameters i.e alpha and Beta as a pickle file" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 50, 406 | "metadata": { 407 | "collapsed": true 408 | }, 409 | "outputs": [], 410 | "source": [ 411 | "load_model=True\n", 412 | "if load_model is True:\n", 413 | " model.load_state_dict(torch.load('practice.pkl'))" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": null, 419 | "metadata": { 420 | "collapsed": true 421 | }, 422 | "outputs": [], 423 | "source": [] 424 | } 425 | ], 426 | "metadata": { 427 | "kernelspec": { 428 | "display_name": "Python 3", 429 | "language": "python", 430 | "name": "python3" 431 | }, 432 | "language_info": { 433 | "codemirror_mode": { 434 | "name": "ipython", 435 | "version": 3 436 | }, 437 | "file_extension": ".py", 438 | "mimetype": "text/x-python", 439 | "name": "python", 440 | "nbconvert_exporter": "python", 441 | "pygments_lexer": "ipython3", 442 | "version": "3.6.2" 443 | } 444 | }, 445 | "nbformat": 4, 446 | "nbformat_minor": 2 447 | } 448 | -------------------------------------------------------------------------------- /Pytorch_Expt/7) Logistic Regression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import torch\n", 12 | "import torch.nn as nn\n", 13 | "import torchvision.transforms as transforms\n", 14 | "import torchvision.datasets as dsets\n", 15 | "from torch.autograd import Variable" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 2, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "train_dataset=dsets.MNIST(root='./data' ,train=True,transform=transforms.ToTensor())" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 4, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "60000" 36 | ] 37 | }, 38 | "execution_count": 4, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "len(train_dataset)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 5, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "torch.Size([1, 28, 28])" 56 | ] 57 | }, 58 | "execution_count": 5, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "train_dataset[0][0].size()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 6, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "tensor(5)" 76 | ] 77 | }, 78 | "execution_count": 6, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "train_dataset[0][1]" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 7, 90 | "metadata": { 91 | "collapsed": true 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "import matplotlib.pyplot as plt\n", 96 | "%matplotlib inline\n", 97 | "import numpy as np" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 8, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "(1, 28, 28)" 109 | ] 110 | }, 111 | "execution_count": 8, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "train_dataset[0][0].numpy().shape" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 9, 123 | "metadata": { 124 | "collapsed": true 125 | }, 126 | "outputs": [], 127 | "source": [ 128 | "show_img=train_dataset[0][0].numpy().reshape(28,28)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 10, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "" 140 | ] 141 | }, 142 | "execution_count": 10, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | }, 146 | { 147 | "data": { 148 | "image/png": "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\n", 149 | "text/plain": [ 150 | "" 151 | ] 152 | }, 153 | "metadata": {}, 154 | "output_type": "display_data" 155 | } 156 | ], 157 | "source": [ 158 | "plt.imshow(show_img,cmap='gray')" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 11, 164 | "metadata": { 165 | "collapsed": true 166 | }, 167 | "outputs": [], 168 | "source": [ 169 | "# train_dataset has image pixels and labels as a tuple" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 12, 175 | "metadata": { 176 | "collapsed": true 177 | }, 178 | "outputs": [], 179 | "source": [ 180 | "test_dataset=dsets.MNIST(root='./data' ,train=False,transform=transforms.ToTensor())" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 13, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "10000" 192 | ] 193 | }, 194 | "execution_count": 13, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "len(test_dataset)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 14, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "tuple" 212 | ] 213 | }, 214 | "execution_count": 14, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "type(test_dataset[0])" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 16, 226 | "metadata": { 227 | "collapsed": true 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "batch_size=100\n", 232 | "\n", 233 | "n_iters=3000\n", 234 | "\n", 235 | "num_epochs=n_iters/(len(train_dataset)/batch_size)" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 19, 241 | "metadata": { 242 | "collapsed": true 243 | }, 244 | "outputs": [], 245 | "source": [ 246 | "train_loader=torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 21, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "True" 258 | ] 259 | }, 260 | "execution_count": 21, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "import collections\n", 267 | "isinstance(train_loader,collections.Iterable)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 22, 273 | "metadata": { 274 | "collapsed": true 275 | }, 276 | "outputs": [], 277 | "source": [ 278 | "test_loader=torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 23, 284 | "metadata": {}, 285 | "outputs": [ 286 | { 287 | "data": { 288 | "text/plain": [ 289 | "True" 290 | ] 291 | }, 292 | "execution_count": 23, 293 | "metadata": {}, 294 | "output_type": "execute_result" 295 | } 296 | ], 297 | "source": [ 298 | "isinstance(test_loader,collections.Iterable)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "markdown", 303 | "metadata": {}, 304 | "source": [ 305 | "### Building Model" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 98, 311 | "metadata": { 312 | "collapsed": true 313 | }, 314 | "outputs": [], 315 | "source": [ 316 | "class LogisticRegressionModel(nn.Module):\n", 317 | "# input_dim is x and output_dim is y\n", 318 | " def __init__(self,input_dim,output_dim):\n", 319 | " super(LogisticRegressionModel,self).__init__()\n", 320 | " self.Linear=nn.Linear(input_dim,output_dim)\n", 321 | " \n", 322 | " def forward(self,x):\n", 323 | " out=self.Linear(x)\n", 324 | " return out" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 99, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "torch.Size([1, 28, 28])" 336 | ] 337 | }, 338 | "execution_count": 99, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "train_dataset[0][0].size()\n", 345 | "# flatten this one to 784 so that our model can interpret it" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 100, 351 | "metadata": {}, 352 | "outputs": [], 353 | "source": [ 354 | "input_dim=28*28\n", 355 | "output_dim=10\n", 356 | "model=LogisticRegressionModel(input_dim,output_dim)" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 101, 362 | "metadata": { 363 | "collapsed": true 364 | }, 365 | "outputs": [], 366 | "source": [ 367 | "criterion=nn.CrossEntropyLoss()" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 102, 373 | "metadata": {}, 374 | "outputs": [], 375 | "source": [ 376 | "learning_rate=.001\n", 377 | "optimizer=torch.optim.SGD(model.parameters(),lr=learning_rate)" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 103, 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "name": "stdout", 387 | "output_type": "stream", 388 | "text": [ 389 | "\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "print(model.parameters())" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 104, 400 | "metadata": {}, 401 | "outputs": [ 402 | { 403 | "name": "stdout", 404 | "output_type": "stream", 405 | "text": [ 406 | "2\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "print(len(list(model.parameters())))" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 105, 417 | "metadata": { 418 | "collapsed": true 419 | }, 420 | "outputs": [], 421 | "source": [ 422 | "# In y=A*x+b" 423 | ] 424 | }, 425 | { 426 | "cell_type": "markdown", 427 | "metadata": {}, 428 | "source": [ 429 | "# A" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": 106, 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "name": "stdout", 439 | "output_type": "stream", 440 | "text": [ 441 | "torch.Size([10, 784])\n" 442 | ] 443 | } 444 | ], 445 | "source": [ 446 | "\n", 447 | "print(list(model.parameters())[0].size())\n", 448 | "# Weights" 449 | ] 450 | }, 451 | { 452 | "cell_type": "markdown", 453 | "metadata": {}, 454 | "source": [ 455 | "# B" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 107, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "name": "stdout", 465 | "output_type": "stream", 466 | "text": [ 467 | "torch.Size([10])\n" 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "print(list(model.parameters())[1].size())\n", 473 | "# Bias" 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "metadata": {}, 479 | "source": [ 480 | "# Build Model" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 108, 486 | "metadata": {}, 487 | "outputs": [ 488 | { 489 | "name": "stderr", 490 | "output_type": "stream", 491 | "text": [ 492 | "C:\\Users\\ashis.panda\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:35: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n" 493 | ] 494 | }, 495 | { 496 | "name": "stdout", 497 | "output_type": "stream", 498 | "text": [ 499 | "Iterations 500. Loss 1.9107856750488281. Acuracy 68.\n", 500 | "Iterations 1000. Loss 1.5539742708206177. Acuracy 76.\n", 501 | "Iterations 1500. Loss 1.3121310472488403. Acuracy 79.\n", 502 | "Iterations 2000. Loss 1.1845715045928955. Acuracy 80.\n", 503 | "Iterations 2500. Loss 0.917949378490448. Acuracy 82.\n", 504 | "Iterations 3000. Loss 0.9815507531166077. Acuracy 82.\n" 505 | ] 506 | } 507 | ], 508 | "source": [ 509 | "iter=0\n", 510 | "for epoch in range(int(num_epochs)):\n", 511 | " for i,(images,labels) in enumerate(train_loader):\n", 512 | " images=Variable(images.view(-1,28*28))\n", 513 | "# print(images.size())\n", 514 | " labels=Variable(labels)\n", 515 | " \n", 516 | " optimizer.zero_grad()\n", 517 | " \n", 518 | " outputs=model(images)\n", 519 | " \n", 520 | " loss=criterion(outputs,labels)\n", 521 | " \n", 522 | " loss.backward()\n", 523 | " \n", 524 | " optimizer.step()\n", 525 | " \n", 526 | " iter+=1\n", 527 | " \n", 528 | " if iter%500==0:\n", 529 | " correct=0\n", 530 | " total=0\n", 531 | " \n", 532 | " for images,labels in test_loader:\n", 533 | " images=Variable(images.view(-1,28*28))\n", 534 | " outputs=model(images)\n", 535 | " \n", 536 | " _,predicted=torch.max(outputs.data,1)\n", 537 | " \n", 538 | " total+=labels.size(0)\n", 539 | " \n", 540 | " correct+=(predicted==labels).sum()\n", 541 | " accuracy=100*correct/total\n", 542 | " \n", 543 | " print('Iterations {}. Loss {}. Acuracy {}.'.format(iter,loss.data[0],accuracy))" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": null, 549 | "metadata": { 550 | "collapsed": true 551 | }, 552 | "outputs": [], 553 | "source": [] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": null, 558 | "metadata": { 559 | "collapsed": true 560 | }, 561 | "outputs": [], 562 | "source": [] 563 | } 564 | ], 565 | "metadata": { 566 | "kernelspec": { 567 | "display_name": "Python 3", 568 | "language": "python", 569 | "name": "python3" 570 | }, 571 | "language_info": { 572 | "codemirror_mode": { 573 | "name": "ipython", 574 | "version": 3 575 | }, 576 | "file_extension": ".py", 577 | "mimetype": "text/x-python", 578 | "name": "python", 579 | "nbconvert_exporter": "python", 580 | "pygments_lexer": "ipython3", 581 | "version": "3.6.2" 582 | } 583 | }, 584 | "nbformat": 4, 585 | "nbformat_minor": 2 586 | } 587 | -------------------------------------------------------------------------------- /Pytorch_Expt/8) Neural Network.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Build a Feedforwad Neural Network with Pytorch" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "#### Model 1:- Hidden Layer Feedforward Neural Network (Sigmoid Activations)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 5, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "# 1) Load Dataset\n", 26 | "# 2) Make dataset iterable\n", 27 | "# 3)Create Model class\n", 28 | "# 4) Instantiate Model Class\n", 29 | "# 5) Instantiate Loss Class\n", 30 | "# 6) Instantiate optimizer Class\n", 31 | "# 7) Tran Model" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 6, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "# Step 1:-\n", 43 | "import torch\n", 44 | "import torch.nn as nn\n", 45 | "import torchvision.transforms as transforms\n", 46 | "import torchvision.datasets as dsets\n", 47 | "from torch.autograd import Variable" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "#### Read Data" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 8, 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "train_dataset=dsets.MNIST(root='./data' ,train=True,transform=transforms.ToTensor())\n", 66 | "test_dataset=dsets.MNIST(root='./data' ,train=False,transform=transforms.ToTensor())" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "#### Make datasets iterable" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 12, 79 | "metadata": { 80 | "collapsed": true 81 | }, 82 | "outputs": [], 83 | "source": [ 84 | "batch_size=100\n", 85 | "\n", 86 | "n_iters=3000\n", 87 | "\n", 88 | "num_epochs=n_iters/(len(train_dataset)/batch_size)\n", 89 | "\n", 90 | "train_loader=torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)\n", 91 | "\n", 92 | "test_loader=torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "#### Create Model Class" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 24, 105 | "metadata": { 106 | "collapsed": true 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "class FeedForwardNeuralNetModel(nn.Module):\n", 111 | "# input_dim is x and output_dim is y\n", 112 | " def __init__(self,input_size,hidden_size,num_classes):\n", 113 | " super(FeedForwardNeuralNetModel,self).__init__()\n", 114 | " self.fc1=nn.Linear(input_dim,hidden_dim)\n", 115 | " \n", 116 | " self.sigmoid=nn.Sigmoid()\n", 117 | " \n", 118 | " self.fc2=nn.Linear(hidden_dim,output_dim)\n", 119 | " \n", 120 | " def forward(self,x):\n", 121 | " out=self.fc1(x)\n", 122 | " out=self.sigmoid(out)\n", 123 | " out=self.fc2(out)\n", 124 | " \n", 125 | " return out" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "#### Instantiate Model Class" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 25, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "input_dim=28*28\n", 142 | "hidden_dim=100\n", 143 | "output_dim=10\n", 144 | "\n", 145 | "model=FeedForwardNeuralNetModel(input_dim,hidden_dim,output_dim)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "#### Instantiate Loss Class" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 26, 158 | "metadata": { 159 | "collapsed": true 160 | }, 161 | "outputs": [], 162 | "source": [ 163 | "criterion=nn.CrossEntropyLoss()" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "#### Instantiate Optimizer Class" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 27, 176 | "metadata": { 177 | "collapsed": true 178 | }, 179 | "outputs": [], 180 | "source": [ 181 | "learning_rate=.1\n", 182 | "optimizer=torch.optim.SGD(model.parameters(),lr=learning_rate)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 28, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stderr", 192 | "output_type": "stream", 193 | "text": [ 194 | "C:\\Users\\ashis.panda\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:35: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n" 195 | ] 196 | }, 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "Iterations 500. Loss 0.5427381992340088. Acuracy 86.\n", 202 | "Iterations 1000. Loss 0.42156586050987244. Acuracy 89.\n", 203 | "Iterations 1500. Loss 0.37555691599845886. Acuracy 90.\n", 204 | "Iterations 2000. Loss 0.31000134348869324. Acuracy 91.\n", 205 | "Iterations 2500. Loss 0.26587727665901184. Acuracy 91.\n", 206 | "Iterations 3000. Loss 0.21983307600021362. Acuracy 92.\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "iter=0\n", 212 | "for epoch in range(int(num_epochs)):\n", 213 | " for i,(images,labels) in enumerate(train_loader):\n", 214 | " images=Variable(images.view(-1,28*28))\n", 215 | "# print(images.size())\n", 216 | " labels=Variable(labels)\n", 217 | " \n", 218 | " optimizer.zero_grad()\n", 219 | " \n", 220 | " outputs=model(images)\n", 221 | " \n", 222 | " loss=criterion(outputs,labels)\n", 223 | " \n", 224 | " loss.backward()\n", 225 | " \n", 226 | " optimizer.step()\n", 227 | " \n", 228 | " iter+=1\n", 229 | " \n", 230 | " if iter%500==0:\n", 231 | " correct=0\n", 232 | " total=0\n", 233 | " \n", 234 | " for images,labels in test_loader:\n", 235 | " images=Variable(images.view(-1,28*28))\n", 236 | " outputs=model(images)\n", 237 | " \n", 238 | " _,predicted=torch.max(outputs.data,1)\n", 239 | " \n", 240 | " total+=labels.size(0)\n", 241 | " \n", 242 | " correct+=(predicted==labels).sum()\n", 243 | " accuracy=100*correct/total\n", 244 | " \n", 245 | " print('Iterations {}. Loss {}. Acuracy {}.'.format(iter,loss.data[0],accuracy))" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 30, 251 | "metadata": {}, 252 | "outputs": [], 253 | "source": [ 254 | "# ! pip install fastai" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": null, 260 | "metadata": { 261 | "collapsed": true 262 | }, 263 | "outputs": [], 264 | "source": [] 265 | } 266 | ], 267 | "metadata": { 268 | "kernelspec": { 269 | "display_name": "Python 3", 270 | "language": "python", 271 | "name": "python3" 272 | }, 273 | "language_info": { 274 | "codemirror_mode": { 275 | "name": "ipython", 276 | "version": 3 277 | }, 278 | "file_extension": ".py", 279 | "mimetype": "text/x-python", 280 | "name": "python", 281 | "nbconvert_exporter": "python", 282 | "pygments_lexer": "ipython3", 283 | "version": "3.6.2" 284 | } 285 | }, 286 | "nbformat": 4, 287 | "nbformat_minor": 2 288 | } 289 | -------------------------------------------------------------------------------- /Pytorch_Expt/9) CNN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "# 1) Load Dataset\n", 12 | "# 2) Make dataset iterable\n", 13 | "# 3)Create Model class\n", 14 | "# 4) Instantiate Model Class\n", 15 | "# 5) Instantiate Loss Class\n", 16 | "# 6) Instantiate optimizer Class\n", 17 | "# 7) Tran Model" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": { 24 | "collapsed": true 25 | }, 26 | "outputs": [], 27 | "source": [ 28 | "# Step 1:-\n", 29 | "import torch\n", 30 | "import torch.nn as nn\n", 31 | "import torchvision.transforms as transforms\n", 32 | "import torchvision.datasets as dsets\n", 33 | "from torch.autograd import Variable\n", 34 | "# Variables is for instantiating and assigning variables to a object" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "metadata": { 41 | "collapsed": true 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "# Reaad data" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 4, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "train_dataset=dsets.MNIST(root='./data' ,train=True,transform=transforms.ToTensor())#Train data\n", 57 | "test_dataset=dsets.MNIST(root='./data' ,train=False,transform=transforms.ToTensor())#Test data" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 12, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "tensor([ 5, 0, 4, ..., 5, 6, 8])" 69 | ] 70 | }, 71 | "execution_count": 12, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "train_dataset.train_labels" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 5, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "torch.Size([60000, 28, 28])\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "print(train_dataset.train_data.size())" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 6, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "torch.Size([10000, 28, 28])\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "print(test_dataset.test_data.size())" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "metadata": { 118 | "collapsed": true 119 | }, 120 | "outputs": [], 121 | "source": [ 122 | "# Make dataset iterable" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 13, 128 | "metadata": { 129 | "collapsed": true 130 | }, 131 | "outputs": [], 132 | "source": [ 133 | "batch_size=100\n", 134 | "\n", 135 | "n_iters=3000\n", 136 | "\n", 137 | "num_epochs=n_iters/(len(train_dataset)/batch_size)\n", 138 | "\n", 139 | "train_loader=torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)\n", 140 | "\n", 141 | "test_loader=torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=False)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 32, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "# Create Model Class" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 14, 158 | "metadata": { 159 | "collapsed": true 160 | }, 161 | "outputs": [], 162 | "source": [ 163 | "# All goodness from pytorch -nn.Module\n", 164 | "class CNNModel(nn.Module):\n", 165 | " def __init__(self):\n", 166 | " super(CNNModel,self).__init__() #Super inheritance function\n", 167 | "# Convolution-1\n", 168 | " self.cnn1=nn.Conv2d(in_channels=1,out_channels=16,kernel_size=5, stride=1,padding=2) \n", 169 | "# #2dimension, in_channels=1 bcoz of mnist grayscale, Output channels = 16(no of kernels)hence 16 feature maps\n", 170 | "# \n", 171 | " self.relu1=nn.ReLU()\n", 172 | "# After eveery convolution layer pass it through the non linearity, Relu is best non linear function\n", 173 | "\n", 174 | "\n", 175 | "# Max pool 1\n", 176 | " self.maxpool1=nn.MaxPool2d(kernel_size=2)\n", 177 | "# Convolution 2\n", 178 | " self.cnn2=nn.Conv2d(in_channels=16,out_channels=32,kernel_size=5,stride=1,padding=2)\n", 179 | "# in_channels=16 and output-channels=32\n", 180 | " self.relu2=nn.ReLU()\n", 181 | "# MAxpool 2\n", 182 | " self.maxpool2=nn.MaxPool2d(kernel_size=2)\n", 183 | "# Fully connected 1 --- 32*7*7 has been calculated and 10 is the mnist dataset output\n", 184 | " self.fc1=nn.Linear(32*7*7,10)\n", 185 | " \n", 186 | " def forward(self,x):\n", 187 | "# Convolution 1\n", 188 | " out=self.cnn1(x)\n", 189 | " out=self.relu1(out)\n", 190 | " \n", 191 | "# MAxpool1\n", 192 | " out=self.maxpool1(out)\n", 193 | " \n", 194 | "# Convolution 2\n", 195 | " out=self.cnn2(out)\n", 196 | " out=self.relu2(out)\n", 197 | "# Maxpool 2\n", 198 | " out=self.maxpool2(out)\n", 199 | "# Resize\n", 200 | "# Original size (100,32,7,7) Input size \n", 201 | "# out.size(0):-100 is the batch size of 100\n", 202 | "# New out size (100,32*7*7) , this is what linear function requires, we cannot feed a linear funcion of 3d here but \n", 203 | "# only 1 single dimension here.\n", 204 | " out=out.view(out.size(0),-1)\n", 205 | "# -1 means reshape to the remaining value which is 32*7*7 here, so that output can be feed to the linear function\n", 206 | " \n", 207 | " out=self.fc1(out)\n", 208 | " \n", 209 | " return out\n", 210 | " \n", 211 | " " 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "# Step 4 :-Instantiate Model Class" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 15, 224 | "metadata": { 225 | "collapsed": true 226 | }, 227 | "outputs": [], 228 | "source": [ 229 | "model=CNNModel()" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 16, 235 | "metadata": { 236 | "collapsed": true 237 | }, 238 | "outputs": [], 239 | "source": [ 240 | "# Step 5:-Instantiate Loss class\n", 241 | "# CNN-CrossEntropyLoss\n", 242 | "# Feedforward NN- CrossEntropyLoss\n", 243 | "# LogisticRegression- CrossEntropyLoss\n", 244 | "# Linear Regression - MSE" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 17, 250 | "metadata": { 251 | "collapsed": true 252 | }, 253 | "outputs": [], 254 | "source": [ 255 | "criterion=nn.CrossEntropyLoss()" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 18, 261 | "metadata": { 262 | "collapsed": true 263 | }, 264 | "outputs": [], 265 | "source": [ 266 | "# Step 6 -Instantiate Optimizer Class\n" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 19, 272 | "metadata": { 273 | "collapsed": true 274 | }, 275 | "outputs": [], 276 | "source": [ 277 | "learning_rate=.01\n", 278 | "optimizer=torch.optim.SGD(model.parameters(),lr=learning_rate)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 20, 284 | "metadata": { 285 | "collapsed": true 286 | }, 287 | "outputs": [], 288 | "source": [ 289 | "# Parameters in depth" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 21, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "name": "stdout", 299 | "output_type": "stream", 300 | "text": [ 301 | "CNNModel(\n", 302 | " (cnn1): Conv2d(1, 16, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n", 303 | " (relu1): ReLU()\n", 304 | " (maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n", 305 | " (cnn2): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n", 306 | " (relu2): ReLU()\n", 307 | " (maxpool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n", 308 | " (fc1): Linear(in_features=1568, out_features=10, bias=True)\n", 309 | ")\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "print(model)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 22, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "\n" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "print(model.parameters())" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 23, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "6\n" 344 | ] 345 | } 346 | ], 347 | "source": [ 348 | "print(len(list(model.parameters())))" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 24, 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "torch.Size([16, 1, 5, 5])\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "print(list(model.parameters())[0].size())\n", 366 | "# 16 kernels of 5*5" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 25, 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "name": "stdout", 376 | "output_type": "stream", 377 | "text": [ 378 | "torch.Size([16])\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "print(list(model.parameters())[1].size())" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 26, 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "name": "stdout", 393 | "output_type": "stream", 394 | "text": [ 395 | "torch.Size([32, 16, 5, 5])\n" 396 | ] 397 | } 398 | ], 399 | "source": [ 400 | "print(list(model.parameters())[2].size())\n", 401 | "# 32 kernels of 5*5 and depth 16" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 27, 407 | "metadata": {}, 408 | "outputs": [ 409 | { 410 | "name": "stdout", 411 | "output_type": "stream", 412 | "text": [ 413 | "torch.Size([32])\n" 414 | ] 415 | } 416 | ], 417 | "source": [ 418 | "print(list(model.parameters())[3].size())" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 28, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "name": "stdout", 428 | "output_type": "stream", 429 | "text": [ 430 | "torch.Size([10, 1568])\n" 431 | ] 432 | } 433 | ], 434 | "source": [ 435 | "print(list(model.parameters())[4].size())\n", 436 | "# Reshaping gives 1568" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 29, 442 | "metadata": {}, 443 | "outputs": [ 444 | { 445 | "name": "stdout", 446 | "output_type": "stream", 447 | "text": [ 448 | "torch.Size([10])\n" 449 | ] 450 | } 451 | ], 452 | "source": [ 453 | "print(list(model.parameters())[5].size())" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 30, 459 | "metadata": { 460 | "collapsed": true 461 | }, 462 | "outputs": [], 463 | "source": [ 464 | "## Step 7 -Train Model\n", 465 | "###### 1) convert input/labels into Variables \n", 466 | "###### cnn input (1,28,28)\n", 467 | "###### Feedforward nn input(1,28*28)\n", 468 | "###### 2) Clear Gradient buffers\n", 469 | "###### 3) Get outputs given inputs\n", 470 | "###### 4) Get Loss\n", 471 | "###### 5)Get gradients wrt parameters\n", 472 | "###### 6) Update parameters using gradients\n", 473 | "###### 7)Repeat" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 31, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "name": "stderr", 483 | "output_type": "stream", 484 | "text": [ 485 | "C:\\Users\\ashis.panda\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:36: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n" 486 | ] 487 | }, 488 | { 489 | "name": "stdout", 490 | "output_type": "stream", 491 | "text": [ 492 | "Iterations 500. Loss 0.4349491000175476. Acuracy 88.01.\n", 493 | "Iterations 1000. Loss 0.3766486644744873. Acuracy 92.05.\n", 494 | "Iterations 1500. Loss 0.3643883764743805. Acuracy 94.17.\n", 495 | "Iterations 2000. Loss 0.0985739678144455. Acuracy 95.45.\n", 496 | "Iterations 2500. Loss 0.16918666660785675. Acuracy 96.34.\n", 497 | "Iterations 3000. Loss 0.05423809587955475. Acuracy 96.92.\n" 498 | ] 499 | } 500 | ], 501 | "source": [ 502 | "iter=0\n", 503 | "for epoch in range(int(num_epochs)):\n", 504 | " for i,(images,labels) in enumerate(train_loader):# 100 images per batch size, so 100 images at once . Every iteration\n", 505 | "# feed 100 images.\n", 506 | " images=Variable(images)\n", 507 | "# print(images.size())\n", 508 | " labels=Variable(labels)\n", 509 | " \n", 510 | " optimizer.zero_grad()\n", 511 | " \n", 512 | " outputs=model.forward(images)\n", 513 | " \n", 514 | " loss=criterion(outputs,labels)\n", 515 | " \n", 516 | " loss.backward()\n", 517 | " \n", 518 | " optimizer.step()\n", 519 | " \n", 520 | " iter+=1\n", 521 | " \n", 522 | " if iter%500==0:\n", 523 | " correct=0\n", 524 | " total=0\n", 525 | " \n", 526 | " for images,labels in test_loader:\n", 527 | " images=Variable(images)\n", 528 | " outputs=model(images)\n", 529 | " \n", 530 | " _,predicted=torch.max(outputs.data,1)\n", 531 | " \n", 532 | " total+=labels.size(0)\n", 533 | " \n", 534 | " correct+=(predicted==labels).sum()\n", 535 | " accuracy=100*int(correct)/int(total)\n", 536 | " \n", 537 | " print('Iterations {}. Loss {}. Acuracy {}.'.format(iter,loss.data[0],accuracy))" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": null, 543 | "metadata": { 544 | "collapsed": true 545 | }, 546 | "outputs": [], 547 | "source": [] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": null, 552 | "metadata": { 553 | "collapsed": true 554 | }, 555 | "outputs": [], 556 | "source": [] 557 | } 558 | ], 559 | "metadata": { 560 | "kernelspec": { 561 | "display_name": "Python 3", 562 | "language": "python", 563 | "name": "python3" 564 | }, 565 | "language_info": { 566 | "codemirror_mode": { 567 | "name": "ipython", 568 | "version": 3 569 | }, 570 | "file_extension": ".py", 571 | "mimetype": "text/x-python", 572 | "name": "python", 573 | "nbconvert_exporter": "python", 574 | "pygments_lexer": "ipython3", 575 | "version": "3.6.2" 576 | } 577 | }, 578 | "nbformat": 4, 579 | "nbformat_minor": 2 580 | } 581 | -------------------------------------------------------------------------------- /Pytorch_Expt/pytorch: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deep_Learning-Experiment 2 | 3 | -I started my Deep Learning journey during early 2017 , All my Deep Learning experiments have been summarized in this repository. Hope you find this useful and interesting. Check out my blog to understand this repository in detail . 4 | Blog Link- https://medium.com/@GeneAshis . 5 | --------------------------------------------------------------------------------