Assignment Goals
• Get Pytorch set up for your environment.
• Familiarize yourself with the tools.
• Implementing and training a basic neural network using Pytorch.
• Happy deep learning :)
Summary
Home-brewing every machine learning solution is not only time-consuming but potentially error-prone. One of
the reasons we’re using Python in this course is because it has some very powerful machine learning tools. Besides
common scientific computing packages such as SciPy and NumPy, it’s very helpful in practice to use frameworks
such as Scikit-Learn, TensorFlow, PyTorch, and MXNet to support your projects. The utilities of these frame works have been developed by a team of professionals and undergo rigorous testing and verification.
In this homework, we’ll be exploring the PyTorch framework. You will complete the functions in the starter code
provided, intro pytorch.py, following the instructions below.
Part 1: Setting up the Python Virtual Environment
In this assignment, you will familiarize yourself with the Python Virtual Environment. Working in a virtual envi ronment is an important part of working with modern ML platforms, so we want you to get a flavor of that through
this assignment. Why do we prefer virtual environments? Virtual environments allow us to install packages within
the virtual environment without affecting the host system setup. So you can maintain project-specific packages in
respective virtual environments.
You can work on your own machine but remember to test on Gradescope. The following are the installation steps
for Linux. If you don’t have a Linux computer, you can use the CS lab computers for this homework. Find more
instructions: How to access CSL Machines Remotely. For example, you can connect to the CSL Linux computers
by using ssh along with your CS account username and password. In your terminal simply type:
ssh {csUserName}@best-linux.cs.wisc.edu
You can use scp to transfer files: scp source destination. For example, to upload a file to the CSL
machine:
scp Desktop/intro_pytorch.py {csUserName}@best-linux.cs.wisc.edu:/home/{csUserName}
You will be working on Python 3 (instead of Python 2 which is no longer supported) with Python version >= 3.8.
Read more about PyTorch and Python version here. To check your Python version use:
python -V or python3 -V
If you have an alias set for python=python3 then both should show the same version (3.x.x)
Step 1: For simplicity, we use the venv module (feel free to use other virtual envs such as Conda).
To set up a Python Virtual Environment, use the following:
python3 -m venv /path/to/new/virtual/environment
1
Homework 6
For example, if you want to set up a virtual environment named Pytorch in your working directory:
python3 -m venv Pytorch
(Optional: If you want to learn more about Python virtual environments, a very good tutorial can be found here.)
Step 2: Activate the virtual environment:
Suppose the name of our virtual environment is Pytorch (you can use any other name if you want). You can
activate the environment by the following command:
source Pytorch/bin/activate
Step3: From your virtual environment shell, run the following commands to upgrade pip (the Python package
installer) and install the CPU version of PyTorch. (It may take some time.)
pip install --upgrade pip
pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0
pip install numpy==1.26.4
You can check the versions of the packages installed using the following command:
pip freeze
Note: to deactivate the virtual environment, just type
deactivate
Part 2: Build Your First Neural Network
In this section, we will guide you step by step to build a simple deep learning model for predicting labels of hand written images. You will learn how to build, train, evaluate the model, and to make predictions on test data using
this model.
You will implement the following functions in Python.
• get data loader(training=True)
– Input: an optional boolean argument (default value is True for training dataset)
– Return: Dataloader for the training set (if training = True) or the test set (if training = False)
• build model()
– Input: none
– Return: an untrained neural network model
• train model(model, train loader, criterion, T)
– Input: the model produced by the previous function, the train DataLoader produced by the first func tion, the criterion for measuring model performance, and the total number of epochs T for training
– Return: none
• evaluate model(model, test loader, criterion, show loss=True)
– Input: the trained model produced by the previous function, the test DataLoader, and the criterion.
– It prints the evaluation statistics as described below (displaying the loss metric value if and only if the
optional parameter has not been set to False)
– Return: none
• predict label(model, test images, index)
– Input: the trained model, test images (tensor of dimension N × 1 × 28 × 28), and an index
– It prints the top 3 most likely labels for the image at the given index, along with their probabilities
– Return: none
You are free to implement any other utility function. But we will only be testing the functionality using the above
5 APIs, so make sure that each of them follows the exact function signature and returns. You can also use helper
methods to visualize the images from the FashionMNIST dataset for a better understanding of the dataset and the
labels. But it is entirely optional and does not carry any points.
2
Homework 6
Import necessary packages
Here are some of the useful modules that may help us save a ton of effort in the project:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
torch, torchvision and the Python standard packages are the only imports allowed on this assignment. The
autograder will likely not handle any other packages.
The following 5 sections explain the details for each of the above functions you are required to implement.
Get the DataLoader
We will use the Fashion-MNIST dataset, each example is a 28 × 28 grayscale image, associated with a label from
10 classes.
Hint 1: Note that PyTorch already contains various datasets for you to use, so there is no need to manually
download from the Internet. Specifically, the function
torchvision.datasets.FashionMNIST()
can be used to retrieve and return a Dataset object torchvision.datasets.FashionMNIST, which is a wrapper that
contains image inputs (as 2D arrays) and labels (’T-shirt/top’, ’ Trouser’, ’Pullover’, ’Dress’, ’Coat’, ’Sandal’,
’Shirt’,’Sneaker’, ’Bag’, ’Ankle Boot’):
train_set=datasets.FashionMNIST(’./data’,train=True,
download=True,transform=custom_transform)
test_set=datasets.FashionMNIST(’./data’, train=False,
transform=custom_transform)
The train set contains images and labels we’ll be using to train our neural network; the test set contains
images and labels for model evaluation. Here we set the location where the dataset is downloaded as the data
folder in the current directory.
Note that input preprocessing can be done by specifying transform as our custom transform (you don’t need to
change this part)
custom_transform= transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
• In the above, transforms.To Tensor() converts a PIL Image or numpy.ndarray to tensor.
3
Homework 6
• transforms.Normalize() normalizes the tensor with a mean and standard deviation which goes as
the two parameters respectively. Feel free to check the official doc for more details.
Hint 2: After obtaining the dataset object, you may wonder how to retrieve images and labels during training and
testing. Luckily, PytTorch provides such a class called torch.utils.data.DataLoader that implements the iterator
protocol. It also provides useful features such as:
• Batching the data
• Shuffling the data
• Load the data in parallel using multiprocessing.
• ...
Below is the full signature of the DataLoader class (for more details, check here):
DataLoader(dataset, batch_size=1, shuffle=False, sampler=None,
batch_sampler=None, num_workers=0, collate_fn=None,
pin_memory=False, drop_last=False, timeout=0,
worker_init_fn=None, *, prefetch_factor=2,
persistent_workers=False)
As an introductory project, we won’t use complicated features. We ask you to set the batch size = 64 for both
train loader and test loader. Besides, set shuffle=False for the test loader. Given a Dataset object data set, we can
obtain its DataLoader as follows:
loader = torch.utils.data.DataLoader(data_set, batch_size = 64)
Putting it all together, you should be ready to implement the get data loader() function. Note that when the
optional argument is unspecified, the function should return the Dataloader for the training set. If the optional
argument is set to False, the Dataloader for the test set is returned. The expected output is as follows:
>>> train_loader = get_data_loader()
>>> print(type(train_loader))
<class ’torch.utils.data.dataloader.DataLoader’>
>>> print(train_loader.dataset)
Dataset FashionMNIST
Number of datapoints: 60000
Root location: ./data
Split: Train
StandardTransform
Transform: Compose(
ToTensor()
Normalize(mean=(0.1307,), std=(0.3081,))
)
>>> test_loader = get_data_loader(False)