# Import the libraries we need for this lab
# Using the following line code to install the torchvision library
# !mamba install -y torchvision
!pip install torchvision==0.9.1 torch==1.8.1
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.datasets as dsets
import torch.nn.functional as F
import matplotlib.pylab as plt
import numpy as np
2) torch.manual_seed(
Multiple Linear Regression
Training Two Parameter, Mini-Batch Gradient Decent, Training Two Parameter Mini-Batch Gradient Decent
Hidden Layer Deep Network: Sigmoid, Tanh and Relu Activations Functions MNIST Dataset
Objective for this Notebook
- Define Several Neural Network, Criterion function, Optimizer.
- Test Sigmoid ,Tanh and Relu.
- Analyse Results.
Table of Contents
In this lab, you will test Sigmoid, Tanh and Relu activation functions on the MNIST dataset with two hidden Layers.
- Neural Network Module and Training Function
- Make Some Data
- Define Several Neural Network, Criterion function, Optimizer
- Test Sigmoid ,Tanh and Relu
- Analyse Results
Estimated Time Needed: 25 min
We’ll need the following libraries
Neural Network Module and Training Function
Define the neural network module or class, with two hidden Layers
# Create the model class using sigmoid as the activation function
class Net(nn.Module):
# Constructor
def __init__(self, D_in, H1, H2, D_out):
super(Net, self).__init__()
self.linear1 = nn.Linear(D_in, H1)
self.linear2 = nn.Linear(H1, H2)
self.linear3 = nn.Linear(H2, D_out)
# Prediction
def forward(self,x):
= torch.sigmoid(self.linear1(x))
x = torch.sigmoid(self.linear2(x))
x = self.linear3(x)
x return x
Define the class with the Tanh activation function
# Create the model class using Tanh as a activation function
class NetTanh(nn.Module):
# Constructor
def __init__(self, D_in, H1, H2, D_out):
super(NetTanh, self).__init__()
self.linear1 = nn.Linear(D_in, H1)
self.linear2 = nn.Linear(H1, H2)
self.linear3 = nn.Linear(H2, D_out)
# Prediction
def forward(self, x):
= torch.tanh(self.linear1(x))
x = torch.tanh(self.linear2(x))
x = self.linear3(x)
x return x
Define the class for the Relu activation function
# Create the model class using Relu as a activation function
class NetRelu(nn.Module):
# Constructor
def __init__(self, D_in, H1, H2, D_out):
super(NetRelu, self).__init__()
self.linear1 = nn.Linear(D_in, H1)
self.linear2 = nn.Linear(H1, H2)
self.linear3 = nn.Linear(H2, D_out)
# Prediction
def forward(self, x):
= torch.relu(self.linear1(x))
x = torch.relu(self.linear2(x))
x = self.linear3(x)
x return x
Define a function to train the model, in this case the function returns a Python dictionary to store the training loss and accuracy on the validation data
# Train the model
def train(model, criterion, train_loader, validation_loader, optimizer, epochs=100):
= 0
i = {'training_loss': [], 'validation_accuracy': []}
useful_stuff
for epoch in range(epochs):
for i, (x, y) in enumerate(train_loader):
optimizer.zero_grad()= model(x.view(-1, 28 * 28))
z = criterion(z, y)
loss
loss.backward()
optimizer.step()'training_loss'].append(loss.data.item())
useful_stuff[
= 0
correct for x, y in validation_loader:
= model(x.view(-1, 28 * 28))
z = torch.max(z, 1)
_, label += (label == y).sum().item()
correct
= 100 * (correct / len(validation_dataset))
accuracy 'validation_accuracy'].append(accuracy)
useful_stuff[
return useful_stuff
Make Some Data
Load the training dataset by setting the parameters train
to True
and convert it to a tensor by placing a transform object int the argument transform
# Create the training dataset
= dsets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor()) train_dataset
Load the testing dataset by setting the parameters train
to False
and convert it to a tensor by placing a transform object int the argument transform
# Create the validating dataset
= dsets.MNIST(root='./data', train=False, download=True, transform=transforms.ToTensor()) validation_dataset
Create the criterion function
# Create the criterion function
= nn.CrossEntropyLoss() criterion
Create the training-data loader and the validation-data loader object
# Create the training data loader and validation data loader object
= torch.utils.data.DataLoader(dataset=train_dataset, batch_size=2000, shuffle=True)
train_loader = torch.utils.data.DataLoader(dataset=validation_dataset, batch_size=5000, shuffle=False) validation_loader
Define Neural Network, Criterion function, Optimizer and Train the Model
Create the model with 100 hidden layers
# Set the parameters for create the model
= 28 * 28
input_dim = 50
hidden_dim1 = 50
hidden_dim2 = 10 output_dim
The epoch number in the video is 35. You can try 10 for now. If you try 35, it may take a long time.
# Set the number of iterations
= 10 cust_epochs
Test Sigmoid ,Tanh and Relu
Train the network using the Sigmoid activation function
# Train the model with sigmoid function
= 0.01
learning_rate = Net(input_dim, hidden_dim1, hidden_dim2, output_dim)
model = torch.optim.SGD(model.parameters(), lr=learning_rate)
optimizer = train(model, criterion, train_loader, validation_loader, optimizer, epochs=cust_epochs) training_results
Train the network using the Tanh activation function
# Train the model with tanh function
= 0.01
learning_rate = NetTanh(input_dim, hidden_dim1, hidden_dim2, output_dim)
model_Tanh = torch.optim.SGD(model_Tanh.parameters(), lr=learning_rate)
optimizer = train(model_Tanh, criterion, train_loader, validation_loader, optimizer, epochs=cust_epochs) training_results_tanch
Train the network using the Relu activation function
# Train the model with relu function
= 0.01
learning_rate = NetRelu(input_dim, hidden_dim1, hidden_dim2, output_dim)
modelRelu = torch.optim.SGD(modelRelu.parameters(), lr=learning_rate)
optimizer = train(modelRelu, criterion, train_loader, validation_loader, optimizer, epochs=cust_epochs) training_results_relu
Analyze Results
Compare the training loss for each activation
# Compare the training loss
'training_loss'], label='tanh')
plt.plot(training_results_tanch['training_loss'], label='sigmoid')
plt.plot(training_results['training_loss'], label='relu')
plt.plot(training_results_relu['loss')
plt.ylabel('training loss iterations')
plt.title( plt.legend()
Compare the validation loss for each model
# Compare the validation loss
'validation_accuracy'], label = 'tanh')
plt.plot(training_results_tanch['validation_accuracy'], label = 'sigmoid')
plt.plot(training_results['validation_accuracy'], label = 'relu')
plt.plot(training_results_relu['validation accuracy')
plt.ylabel('Iteration')
plt.xlabel( plt.legend()