From cef04aeb63f5c50af0226e077727c6eb399c6b27 Mon Sep 17 00:00:00 2001 From: elias Date: Sun, 20 Oct 2019 00:38:10 +0200 Subject: [PATCH 1/2] Added Basic Neural Network This is a basic implementation of a neural network using pytorch. The network accepts an input of 784 and generate a final output of 10. eg(classifying 28x28 -> 784 images of 10 digits) --- ML Cookbook/BasicNeuralNet.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ML Cookbook/BasicNeuralNet.py diff --git a/ML Cookbook/BasicNeuralNet.py b/ML Cookbook/BasicNeuralNet.py new file mode 100644 index 0000000..92e6d2f --- /dev/null +++ b/ML Cookbook/BasicNeuralNet.py @@ -0,0 +1,23 @@ +from torch import nn + +class BasicNeuralNet(nn.Module): + def __init__(self): + super().__init__() + + # Inputs to hidden layer linear transformation + self.hidden = nn.Linear(784, 256) + # Output layer, 10 units + self.output = nn.Linear(256, 10) + + # Define sigmoid activation and softmax output + self.sigmoid = nn.Sigmoid() + self.softmax = nn.Softmax(dim=1) + + def forward(self, x): + # Pass the input tensor through each of the operations + x = self.hidden(x) + x = self.sigmoid(x) + x = self.output(x) + x = self.softmax(x) + + return x \ No newline at end of file From 2b127f14863bd89d114353c8321f5e3ad2c2f336 Mon Sep 17 00:00:00 2001 From: elias Date: Sun, 20 Oct 2019 00:58:48 +0200 Subject: [PATCH 2/2] Added basic CNN Basic implimentation of a CNN using pytorch. 1 converlutional layer: -> input depth 3 -> output depth 16 -> kernel size 3 -> padding 1 1 maxpool layer --- ML Cookbook/CNNs/BasicCNN.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ML Cookbook/CNNs/BasicCNN.py diff --git a/ML Cookbook/CNNs/BasicCNN.py b/ML Cookbook/CNNs/BasicCNN.py new file mode 100644 index 0000000..178ba57 --- /dev/null +++ b/ML Cookbook/CNNs/BasicCNN.py @@ -0,0 +1,16 @@ +import torch.nn as nn +import torch.nn.functional as F + +# CNN architecture definition +class Net(nn.Module): + def __init__(self): + super(Net, self).__init__() + # convolutional layer + self.conv1 = nn.Conv2d(3, 16, 3, padding=1) + # max pooling layer + self.pool = nn.MaxPool2d(2, 2) + + def forward(self, x): + # add sequence of convolutional and max pooling layers + x = self.pool(F.relu(self.conv1(x))) + return x \ No newline at end of file