...

/

Solution Review: Define the Neural Network Architecture

Solution Review: Define the Neural Network Architecture

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
# classifier class
class Classifier(nn.Module):
def __init__(self):
# initialise parent pytorch class
super().__init__()
# define neural network layers
self.model = nn.Sequential(
nn.Linear(784, 200),
nn.LeakyReLU(0.02),
nn.LayerNorm(200),
nn.Linear(200, 10),
nn.Sigmoid()
)
# create loss function
self.loss_function = nn.BCELoss()
# create optimiser, using simple adam optimiser
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

  • Lines 14-22 ...