diff --git a/CNN - MNIST.ipynb b/CNN - MNIST.ipynb index 805d96e..be2af07 100644 --- a/CNN - MNIST.ipynb +++ b/CNN - MNIST.ipynb @@ -44,7 +44,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 3, @@ -76,14 +76,11 @@ "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] } ], "source": [ @@ -109,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -118,7 +115,7 @@ "100" ] }, - "execution_count": 10, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -132,7 +129,47 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class CNN(nn.Module):\n", + " def __init__(self):\n", + " super(CNN,self).__init__()\n", + " # Same padding means that input_size = output_size\n", + " # same_padding = (filter_size - 1) / 2\n", + " self.cnn1 = nn.Conv2d(in_channels=1, out_channels=8, kernel_size=(3,3), stride=1, padding=1)\n", + " # The output from this layer has size:\n", + " # [(input_size - filter_size + 2*(padding)) / stride] + 1\n", + " self.batchnorm1 = nn.BatchNorm2d(8)\n", + " self.relu = nn.ReLU()\n", + " self.maxpool = nn.MaxPool2d(kernel_size=(2,2))\n", + " # Pooling output size is 28/2 = 14 (downsampling)\n", + " # Same padding size is (5 - 1)/2 = 2\n", + " self.cnn2 = nn.Conv2d(in_channels=8, out_channels=32, kernel_size=(5,5), stride=1, padding=2)\n", + " self.batchnorm2 = nn.BatchNorm2d(32)\n", + " # Pooling output size is 14/2 = 7 (downsampling)\n", + " # We have to flatten the output channels 32*7*7 = 1568\n", + " self.fc1 = nn.Linear(1568,600)\n", + " self.dropout = nn.Dropout(p=0.5)\n", + " self.fc2 = nn.Linear(600,10)\n", + " \n", + " def forward(self,x):\n", + " out = self.cnn1(x)\n", + " out = self.batchnorm1(out)\n", + " out = self.relu(out)\n", + " out = self.maxpool(out)\n", + " out = self.cnn2(out)\n", + " out = self.batchnorm2(out)\n", + " out = self.relu(out)\n", + " out = self.maxpool(out)\n", + " # we have to flatten the 32 feature maps, output of our last maxpool (100, 1568)\n", + " out = out.view(batch_size, 1568)\n", + " out = self.fc1(out)\n", + " out = self.relu(out)\n", + " out = self.dropout(out)\n", + " out = self.fc2(out)\n", + " \n", + " return out\n", + " " + ] } ], "metadata": { diff --git a/Convolutional_NN.ipynb b/Convolutional_NN.ipynb deleted file mode 100644 index 35eac67..0000000 --- a/Convolutional_NN.ipynb +++ /dev/null @@ -1,32 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -}