...

/

Solution Review: Define the Convolution Neural Network

Solution Review: Define the Convolution Neural Network

Learn to define the network architecture, loss function, and optimiser for the Classifier class.

We'll cover the following...

Solution

Press + to interact
main.py
NeuralNetwork.py
Dataset.py
fashion-mnist_train.csv
import torch
import torch.nn as nn
from torch.utils.data import Dataset
class View(nn.Module):
def __init__(self, shape):
super().__init__()
self.shape = shape,
def forward(self, x):
return x.view(*self.shape)
# classifier class
class Classifier(nn.Module):
def __init__(self):
# initialise parent pytorch class
super().__init__()
# define neural network layers
self.model = nn.Sequential(
# expand 1 to 30 filters
nn.Conv2d(1, 30, kernel_size=5, stride=2),
nn.LeakyReLU(0.02),
nn.BatchNorm2d(30),
# 30 filters to 40 filters
nn.Conv2d(30, 40, kernel_size=3, stride=2),
nn.LeakyReLU(0.02),
nn.BatchNorm2d(40),
View(1000),
nn.Linear(1000, 10),
nn.Sigmoid()
)
# create loss function
self.loss_function = nn.BCELoss()
# create optimiser, using simple stochastic gradient descent
self.optimiser = torch.optim.Adam(self.parameters())
# counter and accumulator for progress
self.counter = 0
self.progress = []
pass
def forward(self, inputs):
# simply run model
return self.model(inputs)
def train(self, inputs, targets):
# calculate the output of the network
outputs = self.forward(inputs)
# calculate loss
loss = self.loss_function(outputs, targets)
# increase counter and accumulate error every 10
self.counter += 1
if (self.counter % 10 == 0):
self.progress.append(loss.item())
pass
if (self.counter % 10000 == 0):
print("counter = ", self.counter)
pass
# zero gradients, perform a backward pass, and update the weights
self.optimiser.zero_grad()
loss.backward()
self.optimiser.step()
pass
def plot_progress(self):
df = pandas.DataFrame(self.progress, columns=['loss'])
df.plot(ylim=(0, 1.0), figsize=(16,8), alpha=0.1, marker='.', grid=True, yticks=(0, 0.25, 0.5))
pass
pass

Explanation

  • The first element of this neural network is a convolution layer nn.Conv2d (line 25).

    • The first parameter is the number of input channels, which is 1 for our monochrome images.

    • The second parameter is the number of output channels. What this means is that we’ve created 30 convolution kernels ...