Professional Documents
Culture Documents
This assignment consists of two parts. Part-1 is based on the content you learned in the Deep
Learning lectures. You need to complete a few topics (weight initialization in neural networks,
batch normalization) as homework before solving question 4, 5, and 6. Feel free to headout to
forums for details. Part-2 is un-graded and mainly designed to help you flex the Deep Learning
muscles grown in Part-2.
Unlike the first part, you'll have to implement everything from scratch in Part-2. If you find
answers to questions in Part-2, feel free to head out to the forums and discuss them with your
classmates!
Instructions
1. Use Python 3.x to run this notebook
2. Write your code only in between the lines 'YOUR CODE STARTS HERE' and 'YOUR CODE
ENDS HERE'. you sould not change anything else code cells, if you do, the answers you
are supposed to get at the end of this assignment might be wrong.
3. Read documentation of each function carefully.
4. All the Best!
# Imports
import numpy as np
import cv2
import matplotlib.pyplot as plt
from PIL import Image
Part-1
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
import torch.nn.functional as F
import timeit
import unittest
Network Definition
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
#### YOUR CODE STARTS HERE ####
# define a conv layer with output channels as 16, kernel size
of 3 and stride of 1
Sanity Check
Make sure all the tests below pass without any errors, before you proceed with the training part.
import unittest
class TestImplementations(unittest.TestCase):
# Dataloading tests
def test_dataset(self):
self.dataset_classes = ['0 - zero',
'1 - one',
'2 - two',
'3 - three',
'4 - four',
'5 - five',
'6 - six',
'7 - seven',
'8 - eight',
'9 - nine']
self.assertTrue(train_dataset.classes == self.dataset_classes)
self.assertTrue(train_dataset.train == True)
def test_dataloader(self):
self.assertTrue(train_dataloader.batch_size == 32)
self.assertTrue(test_dataloader.batch_size == 32)
def test_total_parameters(self):
model = Net().to(device)
self.assertTrue(sum(p.numel() for p in model.parameters()) ==
1015946)
suite =
unittest.TestLoader().loadTestsFromModule(TestImplementations())
unittest.TextTestRunner().run(suite)
...
----------------------------------------------------------------------
Ran 3 tests in 8.293s
OK
test_loss /= len(test_loader.dataset)
Question 1
Run the code cell below and report the final test accuracy (If you are not getting the exact
number shown in options, please report the closest number).
1. 64%
2. 79%
3. 97%
4. 90%
model = Net().to(device)
start = timeit.default_timer()
for epoch in range(1, 11):
train(model, device, train_dataloader, optimizer, epoch)
test(model, device, test_dataloader)
stop = timeit.default_timer()
print('Total time taken: {} seconds'.format(int(stop - start)) )
Question 2
Modify the network to replace ReLU activations with Sigmoid and report the final test accuracy
by running the cell below. (If you are not getting the exact number shown in options, please
report the closest number).
1. 95%
2. 54%
3. 20%
4. 9%
class NetSigmoid(nn.Module):
def __init__(self):
super(NetSigmoid, self).__init__()
#### YOUR CODE STARTS HERE ####
# define a conv layer with output channels as 16, kernel size
of 3 and stride of 1
model = NetSigmoid().to(device)
start = timeit.default_timer()
stop = timeit.default_timer()
print('Total time taken: {} seconds'.format(int(stop - start)) )
Question 3
Train the network defined in Question-1 with Adagrad optimizer with the same learning rate
mentioned in the question. Report the final test accuracy by running the cell below. (If you are
not getting the exact number shown in options, please report the closest number).
1. 80%
2. 99%
3. 92%
4. 85%
model = Net().to(device)
start = timeit.default_timer()
stop = timeit.default_timer()
print('Total time taken: {} seconds'.format(int(stop - start)) )
Question 4
Initialize the Conv2d layers in the network defined in Question-1 (Net) with all ones (both
weights and bias). Train the network with Adam optimizer and report the final test accuracy by
running the cell below. (If you are not getting the exact number shown in options, please report
the closest number).
1. 11%
2. 18%
3. 97%
4. 6%
model = Net().to(device)
def init_weights(m):
if isinstance(m, nn.Conv2d):
torch.nn.init.ones_(m.weight.data)
torch.nn.init.ones_(m.bias.data)
model.apply(init_weights)
## Define Adam Optimiser with a learning rate of 0.01
optimizer = optim.Adam(model.parameters(), lr=0.01)
start = timeit.default_timer()
stop = timeit.default_timer()
print('Total time taken: {} seconds'.format(int(stop - start)) )
Question 5
Initialize the network defined in Question-1 (Net) with Xavier's initialization
(torch.nn.init.xavier_normal)(for bias use zero). Train the network with Adam optimizer and
report the final test accuracy by running the cell below. (If you are not getting the exact number
shown in options, please report the closest number).
1. 88%
2. 74%
3. 97%
4. 80%
model = Net().to(device)
def init_weights(m):
if isinstance(m, nn.Conv2d):
torch.nn.init.xavier_normal_(m.weight.data)
torch.nn.init.zeros_(m.bias.data)
model.apply(init_weights)
## Define Adam Optimiser with a learning rate of 0.01
optimizer = optim.Adam(model.parameters(), lr=0.01)
start = timeit.default_timer()
for epoch in range(1, 5):
train(model, device, train_dataloader, optimizer, epoch)
test(model, device, test_dataloader)
stop = timeit.default_timer()
print('Total time taken: {} seconds'.format(int(stop - start)) )
Question 6
Add three batch-norm layers to the network defined in Question-1 and report the final test
accuracy by running the cell below.
1. 92%
2. 89%
3. 98%
4. 74%
class NetBatchNorm(nn.Module):
def __init__(self):
super(NetBatchNorm, self).__init__()
#### YOUR CODE STARTS HERE ####
# define a conv layer with output channels as 16, kernel size
of 3 and stride of 1
True
model = NetBatchNorm().to(device)
start = timeit.default_timer()
for epoch in range(1, 11):
train(model, device, train_dataloader, optimizer, epoch)
test(model, device, test_dataloader)
stop = timeit.default_timer()
print('Total time taken: {} seconds'.format(int(stop - start)) )
Part-2
This section is un-graded and purely for practice.
Main focus of this part is to help you flex the deep learning muscles built in the above part. You
should build a network on the SVHN dataset. This dataset is similar to MNIST but unlike MNIST,
the images are colored and more complex.
As of writing this, the state-of-the-art(SoTA) performance on this dataset is 98.98%. You can try
to start with the simple network we defined above for the MNSIT dataset(with some
modification for dealing with different sized colored images unlike MNIST). But to achive the
SoTA performance you need to do a lot of hackery. These are list of few things, we would
encourage you to try:
• Use data augmentation wisely. Read and understand how to perform the
augmentations listed below.
– RandomFlips, Color Jittering
– Cutout, Cutmix
– Mixup
– Auto-augment
• Try to increase the image size using standard image interpolation techniques. Try
using tricks like Progressive resizing of images and see if it helps.
• After certain number of layers, adding more layer might not be of much help, run
experiments on SVHN and see if you observe this.
• To understand the difficulties in training deeper networks read this paper: Deep
Residual Learning for Image Recognition