1 / 8

Overview of Neural Network Architecture Assignment Code

This assignment code demonstrates the implementation of a simple neural network architecture using PyTorch for image classification. It covers the process of defining the network parts, loading the data into tensors, creating the model, and fitting the model. Important concepts such as convolutional layers, pooling layers, batch normalization, and data augmentation are also explored.

lynni
Download Presentation

Overview of Neural Network Architecture Assignment Code

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overview of Neural Network Architecture Assignment Code Geoff Hulten

  2. PyTorch • Deep learning platform – easy to use • Install from: https://pytorch.org/get-started/locally/ • CUDA enables GPU training – 100s of times faster • Docs: • https://pytorch.org/docs/stable/index.html • https://pytorch.org/docs/stable/nn.html <- important

  3. Define the Neural Network Parts importtorch classSimpleBlinkNeuralNetwork(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), torch.nn.Sigmoid() )

  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 fromPILimportImage importtorchvision.transformsastransforms importtorch # Load the images and then convert them into tensors (no normalization) xTrainImages = [ Image.open(path) forpathinxTrainRaw ] xTrain = torch.stack([ transforms.ToTensor()(image) forimageinxTrainImages ]) xTestImages = [ Image.open(path) forpathinxTestRaw ] xTest = torch.stack([ transforms.ToTensor()(image) forimageinxTestImages ]) yTrain = torch.Tensor([ [ yValue ] foryValueinyTrainRaw ]) yTest = torch.Tensor([ [ yValue ] foryValueinyTestRaw ])

  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 foriinrange(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)

More Related