# Import the libraries we need to use in this lab
# Using the following line code to install the torchvision library
# !mamba install -y torchvision
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
0) torch.manual_seed(
Test Uniform, Default and He Initialization on MNIST Dataset with Relu Activation
Objective for this Notebook
- Learn how to Define Several Neural Network, Criterion function, Optimizer.
- Test Uniform, Default and He Initialization
Table of Contents
In this lab, you will test the Uniform Initialization, Default Initialization and He Initialization on the MNIST dataset with Relu Activation
- Neural Network Module and Training Function
- Make Some Data
- Define Several Neural Network, Criterion function, Optimizer
- Test Uniform, Default and He Initialization
- Analyze Results
Estimated Time Needed: 25 min
Preparation
We’ll need the following libraries:
Neural Network Module and Training Function
Define the neural network module or class with He Initialization
# Define the class for neural network model with He Initialization
class Net_He(nn.Module):
# Constructor
def __init__(self, Layers):
super(Net_He, self).__init__()
self.hidden = nn.ModuleList()
for input_size, output_size in zip(Layers, Layers[1:]):
= nn.Linear(input_size, output_size)
linear ='relu')
torch.nn.init.kaiming_uniform_(linear.weight, nonlinearityself.hidden.append(linear)
# Prediction
def forward(self, x):
= len(self.hidden)
L for (l, linear_transform) in zip(range(L), self.hidden):
if l < L - 1:
= F.relu(linear_transform(x))
x else:
= linear_transform(x)
x return x
Define the class or neural network with Uniform Initialization
# Define the class for neural network model with Uniform Initialization
class Net_Uniform(nn.Module):
# Constructor
def __init__(self, Layers):
super(Net_Uniform, self).__init__()
self.hidden = nn.ModuleList()
for input_size, output_size in zip(Layers, Layers[1:]):
= nn.Linear(input_size,output_size)
linear 0, 1)
linear.weight.data.uniform_(self.hidden.append(linear)
# Prediction
def forward(self, x):
= len(self.hidden)
L for (l, linear_transform) in zip(range(L), self.hidden):
if l < L - 1:
= F.relu(linear_transform(x))
x else:
= linear_transform(x)
x
return x
Class or Neural Network with PyTorch Default Initialization
# Define the class for neural network model with PyTorch Default Initialization
class Net(nn.Module):
# Constructor
def __init__(self, Layers):
super(Net, self).__init__()
self.hidden = nn.ModuleList()
for input_size, output_size in zip(Layers, Layers[1:]):
= nn.Linear(input_size, output_size)
linear self.hidden.append(linear)
def forward(self, x):
=len(self.hidden)
Lfor (l, linear_transform) in zip(range(L), self.hidden):
if l < L - 1:
= F.relu(linear_transform(x))
x else:
= linear_transform(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
# Define function to train model
def train(model, criterion, train_loader, validation_loader, optimizer, epochs = 100):
= 0
i = {'training_loss': [], 'validation_accuracy': []}
loss_accuracy
#n_epochs
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())
loss_accuracy[
= 0
correct for x, y in validation_loader:
= model(x.view(-1, 28 * 28))
yhat = torch.max(yhat, 1)
_, label += (label == y).sum().item()
correct = 100 * (correct / len(validation_dataset))
accuracy 'validation_accuracy'].append(accuracy)
loss_accuracy[
return loss_accuracy
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 False
and convert it to a tensor by placing a transform object int the argument transform
# Create the validation dataset
= dsets.MNIST(root='./data', train=False, download=True, transform=transforms.ToTensor()) validation_dataset
Create the training-data loader and the validation-data loader object
# Create the data loader for training and validation
= 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 criterion function
# Create the criterion function
= nn.CrossEntropyLoss() criterion
Create a list that contains layer size
# Create the parameters
= 28 * 28
input_dim = 10
output_dim = [input_dim, 100, 200, 100, output_dim] layers
Test PyTorch Default Initialization, Xavier Initialization and Uniform Initialization
Train the network using PyTorch Default Initialization
# Train the model with the default initialization
= Net(layers)
model = 0.01
learning_rate = torch.optim.SGD(model.parameters(), lr=learning_rate)
optimizer = train(model, criterion, train_loader,validation_loader, optimizer, epochs=30) training_results
Train the network using He Initialization function
# Train the model with the He initialization
= Net_He(layers)
model_He = torch.optim.SGD(model_He.parameters(), lr=learning_rate)
optimizer = train(model_He, criterion, train_loader, validation_loader, optimizer, epochs=30) training_results_He
Train the network using Uniform Initialization function
# Train the model with the Uniform initialization
= Net_Uniform(layers)
model_Uniform = torch.optim.SGD(model_Uniform.parameters(), lr=learning_rate)
optimizer = train(model_Uniform, criterion, train_loader, validation_loader, optimizer, epochs=30) training_results_Uniform
Analyze Results
Compare the training loss for each activation
# Plot the loss
'training_loss'], label='He')
plt.plot(training_results_He['training_loss'], label='Default')
plt.plot(training_results['training_loss'], label='Uniform')
plt.plot(training_results_Uniform['loss')
plt.ylabel('iteration ')
plt.xlabel('training loss iterations')
plt.title( plt.legend()
Compare the validation loss for each model
# Plot the accuracy
'validation_accuracy'], label='He')
plt.plot(training_results_He['validation_accuracy'], label='Default')
plt.plot(training_results['validation_accuracy'], label='Uniform')
plt.plot(training_results_Uniform['validation accuracy')
plt.ylabel('epochs ')
plt.xlabel(
plt.legend() plt.show()