Download presentation
Presentation is loading. Please wait.
Published byAgus Darmali Modified over 5 years ago
1
Overview of Neural Network Architecture Assignment Code
Geoff Hulten
2
PyTorch Deep learning platform – easy to use
Install from: CUDA enables GPU training – 100s of times faster Docs: <- important
3
Define the Neural Network Parts
import torch class SimpleBlinkNeuralNetwork(torch.nn.Module): def __init__(self, hiddenNodes = 20): super(SimpleBlinkNeuralNetwork, self).__init__() # Down sample the image to 12x12 self.avgPooling = torch.nn.AvgPool2d(kernel_size = 2, stride = 2) # Fully connected layer to all the down-sampled pixels to all the hidden nodes self.fullyConnectedOne = torch.nn.Sequential( torch.nn.Linear(12*12, hiddenNodes), torch.nn.Sigmoid() ) # Fully connected layer from the hidden layer to a single output node self.outputLayer = torch.nn.Sequential( torch.nn.Linear(hiddenNodes, 1),
4
Run Forward Pass def forward(self, x):
# Apply the layers created at initialization time in order out = self.avgPooling(x) out = out.reshape(out.size(0), -1) out = self.fullyConnectedOne(out) out = self.outputLayer(out) return out
5
Load the Data into Tensors
from PIL import Image import torchvision.transforms as transforms import torch # Load the images and then convert them into tensors (no normalization) xTrainImages = [ Image.open(path) for path in xTrainRaw ] xTrain = torch.stack([ transforms.ToTensor()(image) for image in xTrainImages ]) xTestImages = [ Image.open(path) for path in xTestRaw ] xTest = torch.stack([ transforms.ToTensor()(image) for image in xTestImages ]) yTrain = torch.Tensor([ [ yValue ] for yValue in yTrainRaw ]) yTest = torch.Tensor([ [ yValue ] for yValue in yTestRaw ])
6
Create the Model and Support
# Create the model and set up: # the loss function to use (Mean Square Error) # the optimization method (Stochastic Gradient Descent) and the step size model = SimpleBlinkNeuralNetwork.SimpleBlinkNeuralNetwork(hiddenNodes = 5) lossFunction = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)
7
Fit the Model for i in range(500): # Do the forward pass
yTrainPredicted = model(xTrain) # Compute the training set loss loss = lossFunction(yTrainPredicted, yTrain) print(i, loss.item()) # Reset the gradients in the network to zero optimizer.zero_grad() # Backprop the errors from the loss on this iteration loss.backward() # Do a weight update step optimizer.step()
8
Things to look into to succeed at this assignment:
torch.nn.Conv2d torch.nn.ReLu torch.nn.MaxPool2d torch.nn.BatchNorm2d torch.nn.Dropout Tuning the number of filters/nodes, the optimizer, and the training iterations torchvision.transforms – Data augmentation (e.g. mirroring training data)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.