|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "6584924f", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stdout", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "No. of Training examples: 60000\n", |
| 14 | + "No. of Test examples: 10000\n" |
| 15 | + ] |
| 16 | + } |
| 17 | + ], |
| 18 | + "source": [ |
| 19 | + "import torch as tch\n", |
| 20 | + "import torchvision.datasets as dt\n", |
| 21 | + "import torchvision.transforms as trans\n", |
| 22 | + "import torch.nn as nn\n", |
| 23 | + "import matplotlib.pyplot as plt\n", |
| 24 | + "from time import time\n", |
| 25 | + "from torch.utils.tensorboard import SummaryWriter\n", |
| 26 | + "\n", |
| 27 | + "writer = SummaryWriter()\n", |
| 28 | + "\n", |
| 29 | + "train = dt.MNIST(root=\"./datasets\", train=True, transform=trans.ToTensor(), download=True)\n", |
| 30 | + "test = dt.MNIST(root=\"./datasets\", train=False, transform=trans.ToTensor(), download=True)\n", |
| 31 | + "print(\"No. of Training examples: \",len(train))\n", |
| 32 | + "print(\"No. of Test examples: \",len(test))\n", |
| 33 | + "\n", |
| 34 | + "train_batch = tch.utils.data.DataLoader(train, batch_size=30, shuffle=True)" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 3, |
| 40 | + "id": "148134c6", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [ |
| 43 | + { |
| 44 | + "name": "stdout", |
| 45 | + "output_type": "stream", |
| 46 | + "text": [ |
| 47 | + "Epoch Number : 0 = Loss : 0.5081581976301968\n", |
| 48 | + "Epoch Number : 1 = Loss : 0.2607014692854136\n", |
| 49 | + "Epoch Number : 2 = Loss : 0.20602162138931454\n", |
| 50 | + "Epoch Number : 3 = Loss : 0.16912483075121418\n", |
| 51 | + "Epoch Number : 4 = Loss : 0.14195448012137785\n", |
| 52 | + "Epoch Number : 5 = Loss : 0.12255496634542942\n", |
| 53 | + "Epoch Number : 6 = Loss : 0.10741885769902729\n", |
| 54 | + "Epoch Number : 7 = Loss : 0.09534787850477733\n", |
| 55 | + "Epoch Number : 8 = Loss : 0.08542947270977311\n", |
| 56 | + "Epoch Number : 9 = Loss : 0.07763797796762083\n", |
| 57 | + "Epoch Number : 10 = Loss : 0.07077616441179999\n", |
| 58 | + "Epoch Number : 11 = Loss : 0.06475602737592999\n", |
| 59 | + "Epoch Number : 12 = Loss : 0.05975847951992182\n", |
| 60 | + "Epoch Number : 13 = Loss : 0.05511430356022902\n", |
| 61 | + "Epoch Number : 14 = Loss : 0.05111963468440808\n", |
| 62 | + "Epoch Number : 15 = Loss : 0.04776831939723342\n", |
| 63 | + "Epoch Number : 16 = Loss : 0.04453270577394869\n", |
| 64 | + "Epoch Number : 17 = Loss : 0.0416670137080946\n", |
| 65 | + "\n", |
| 66 | + "Training Time (in minutes) : 2.8615051031112673\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "input = 784\n", |
| 72 | + "hidden = 490\n", |
| 73 | + "output = 10\n", |
| 74 | + "\n", |
| 75 | + "model = nn.Sequential(nn.Linear(input, hidden),\n", |
| 76 | + " nn.LeakyReLU(),\n", |
| 77 | + " nn.Linear(hidden, output),\n", |
| 78 | + " nn.LogSoftmax(dim=1))\n", |
| 79 | + "\n", |
| 80 | + "lossfn = nn.NLLLoss()\n", |
| 81 | + "images, labels = next(iter(train_batch))\n", |
| 82 | + "images = images.view(images.shape[0], -1)\n", |
| 83 | + "\n", |
| 84 | + "logps = model(images)\n", |
| 85 | + "loss = lossfn(logps, labels)\n", |
| 86 | + "loss.backward()\n", |
| 87 | + "\n", |
| 88 | + "optimize = tch.optim.SGD(model.parameters(), lr=0.003, momentum=0.9)\n", |
| 89 | + "time_start = time()\n", |
| 90 | + "epochs = 18\n", |
| 91 | + "for num in range(epochs):\n", |
| 92 | + " run=0\n", |
| 93 | + " for images, labels in train_batch:\n", |
| 94 | + " images = images.view(images.shape[0], -1)\n", |
| 95 | + " optimize.zero_grad()\n", |
| 96 | + " output = model(images)\n", |
| 97 | + " loss = lossfn(output, labels)\n", |
| 98 | + " writer.add_scalar(\"Loss\", loss, num)\n", |
| 99 | + " loss.backward()\n", |
| 100 | + " optimize.step()\n", |
| 101 | + " run += loss.item()\n", |
| 102 | + " else:\n", |
| 103 | + " print(\"Epoch Number : {} = Loss : {}\".format(num, run/len(train_batch)))\n", |
| 104 | + "Elapsed=(time()-time_start)/60\n", |
| 105 | + "print(\"\\nTraining Time (in minutes) : \",Elapsed)\n", |
| 106 | + "writer.flush()\n", |
| 107 | + "writer.close()" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": 4, |
| 113 | + "id": "99b1476b", |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [ |
| 116 | + { |
| 117 | + "name": "stdout", |
| 118 | + "output_type": "stream", |
| 119 | + "text": [ |
| 120 | + "Number Of Images Tested : 10000\n", |
| 121 | + "Model Accuracy : 0.9787\n" |
| 122 | + ] |
| 123 | + } |
| 124 | + ], |
| 125 | + "source": [ |
| 126 | + "correct=0\n", |
| 127 | + "all = 0\n", |
| 128 | + "for images,labels in test:\n", |
| 129 | + " img = images.view(1, 784)\n", |
| 130 | + " with tch.no_grad():\n", |
| 131 | + " logps = model(img) \n", |
| 132 | + " ps = tch.exp(logps)\n", |
| 133 | + " probab = list(ps.numpy()[0])\n", |
| 134 | + " prediction = probab.index(max(probab))\n", |
| 135 | + " truth = labels\n", |
| 136 | + " if(truth == prediction):\n", |
| 137 | + " correct += 1\n", |
| 138 | + " all += 1\n", |
| 139 | + "\n", |
| 140 | + "print(\"Number Of Images Tested : \", all)\n", |
| 141 | + "print(\"Model Accuracy : \", (correct/all))" |
| 142 | + ] |
| 143 | + } |
| 144 | + ], |
| 145 | + "metadata": { |
| 146 | + "kernelspec": { |
| 147 | + "display_name": "Python 3 (ipykernel)", |
| 148 | + "language": "python", |
| 149 | + "name": "python3" |
| 150 | + }, |
| 151 | + "language_info": { |
| 152 | + "codemirror_mode": { |
| 153 | + "name": "ipython", |
| 154 | + "version": 3 |
| 155 | + }, |
| 156 | + "file_extension": ".py", |
| 157 | + "mimetype": "text/x-python", |
| 158 | + "name": "python", |
| 159 | + "nbconvert_exporter": "python", |
| 160 | + "pygments_lexer": "ipython3", |
| 161 | + "version": "3.9.13" |
| 162 | + } |
| 163 | + }, |
| 164 | + "nbformat": 4, |
| 165 | + "nbformat_minor": 5 |
| 166 | +} |
0 commit comments