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 torchimport torch.nn as nnfrom torch.utils.data import Datasetclass View(nn.Module):def __init__(self, shape):super().__init__()self.shape = shape,def forward(self, x):return x.view(*self.shape)# classifier classclass Classifier(nn.Module):def __init__(self):# initialise parent pytorch classsuper().__init__()# define neural network layersself.model = nn.Sequential(# expand 1 to 30 filtersnn.Conv2d(1, 30, kernel_size=5, stride=2),nn.LeakyReLU(0.02),nn.BatchNorm2d(30),# 30 filters to 40 filtersnn.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 functionself.loss_function = nn.BCELoss()# create optimiser, using simple stochastic gradient descentself.optimiser = torch.optim.Adam(self.parameters())# counter and accumulator for progressself.counter = 0self.progress = []passdef forward(self, inputs):# simply run modelreturn self.model(inputs)def train(self, inputs, targets):# calculate the output of the networkoutputs = self.forward(inputs)# calculate lossloss = self.loss_function(outputs, targets)# increase counter and accumulate error every 10self.counter += 1if (self.counter % 10 == 0):self.progress.append(loss.item())passif (self.counter % 10000 == 0):print("counter = ", self.counter)pass# zero gradients, perform a backward pass, and update the weightsself.optimiser.zero_grad()loss.backward()self.optimiser.step()passdef 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))passpass
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 ...
-