Homework #8: PyTorch Classifiers on MNIST, Fashion-MNIST, and CIFAR-10
EE 541: Fall 2026
Assigned: 26 October
Due: Sunday, 1 November at 23:59
Gradescope: Homework 8 | Setup | How to Submit
Getting Started Guide: View Guide
Data: External Link
Overview
Three PyTorch classifiers: the MNIST softmax classifier from Homework 6 rebuilt with nn.Sequential and CrossEntropyLoss, a regularization and dropout comparison on Fashion-MNIST, and an MLP on CIFAR-10 with confusion-matrix analysis.
Getting Started
Download the starter code: hw8-starter.zip
unzip hw8-starter.zip
cd hw8-starter
python generate_datasets.py --seed 541This creates data/ with placeholder copies of mnist_traindata.hdf5 and mnist_testdata.hdf5 that have the right layout but random content. Download the real files from the Data link into data/ to replace them (they are the Homework 5 files). Fashion-MNIST and CIFAR-10 are downloaded into data/ by torchvision the first time Problems 2 and 3 run.
Each problem directory contains the script named in the problem, with a docstring stating how it is run and what it prints or writes. q1/ also contains a test_interfaces.py that runs the script and checks its output files. Run it from inside q1/:
python -m pytest test_interfaces.pyProblem 1: PyTorch Logistic Classifier (MNIST)
Use PyTorch to train a logistic-classifier for the MNIST dataset of handwritten digits. Use the Dataset class from the Getting Started Guide to read from the earlier MNIST data files — mnist_traindata.hdf5 and mnist_testdata.hdf5. Create a nn.Sequential network with a single fully-connected (i.e. linear) layer. Choose dimensions to match the multi-class classifier from Homework 6. Omit the final soft-max activation (PyTorch implicitly computes this when calculating the loss).
Train your model using a multi-category cross-entropy loss. PyTorch provides the CrossEntropyLoss to numerically stabilize the combined SoftMax-NLLikelihood calculation. Optimize with (standard) stochastic gradient descent (SGD) and with a mini-batch size of 100. Experiment with \(\ell_1\)- and/or \(\ell_2\)-regularization to stabilize training and improve generalization. You may need to experiment the learning rate to improve performance.
Record the log-loss and accuracy of your model on the training set and test set at the end of each epoch. Plot log-loss (i.e. learning curve) of the training set and test set on the same figure. On a separate figure plot the accuracy of your model on the training set and test set. Plot each as a function of the epoch number.
Evaluate the fully trained model on the test data and generate a confusion matrix – i.e. find the classification rate conditioned on the true class. Element \((i,j)\) of the confusion matrix is the rate at which the network decides class \(j\) when class \(i\) is the correct label (ground truth). Use seaborn or similar python package to generate a heatmap showing the confusion matrix.
Deliverables
See Submission. logistic_torch.py is your implementation. q1.pdf contains the learning curves, the accuracy plots, and the confusion matrix heatmap.
Problem 2: Regularization and Dropout (Fashion MNIST)
Train two models on Fashion MNIST for 40 epochs:
One hidden layer with ReLU activation, 128 nodes. No regularization, no dropout.
One hidden layer with ReLU activation, 48 nodes. L2 regularization with coefficient \(\lambda = 0.0001\) and dropout with rate 0.2 at the hidden layer.
You may use the demo scripts from lecture and discussion. Produce histograms for the weights of these two networks – a separate histogram for the two layers (the input and hidden layers) in each case. Describe the qualitative differences between these histograms. What effect does regularization have on the distribution of weights?
Deliverables
See Submission. fashion_mnist.py is your implementation. q2.pdf contains the four weight histograms and your discussion.
Problem 3: CIFAR-10 Classification
Train an MLP for CIFAR-10 (https://www.cs.toronto.edu/~kriz/cifar.html). The CIFAR-10 dataset consists of 60000 \(32 \times 32\) colour images in 10 classes. The 10 different classes represent airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks. Each \(32 \times 32\) image consists of three color channels (Red, Green, and Blue) for a total of \(3 \times 32 \times 32 = 3072\) pixels. There are 50000 training images and 10000 test images.
Use two hidden layers and ReLU activations. Use 256 nodes in the first hidden layer and 128 nodes in the second hidden layer. Use a dropout layer for each hidden layer output with 30% dropout rate and use an L2 regularizer with coefficient \(\lambda = 0.0001\). You may use the demo scripts from lecture and discussion.
Evaluate this trained model on the test data and compute a confusion matrix. Use seaborn or similar python package to generate a heatmap showing the confusion matrix.
Consider class \(m\). List the class most likely confused for class \(m\) for each object type.
Which two classes (object types) are most likely to be confused overall?
Deliverables
See Submission. cifar10.py is your implementation. q3.pdf contains the confusion matrix heatmap and your answers to (a) and (b).
README.md
.gitignore
requirements.txt
generate_datasets.py
q1/
├── logistic_torch.py
├── test_interfaces.py
└── q1.pdf
q2/
├── fashion_mnist.py
└── q2.pdf
q3/
├── cifar10.py
└── q3.pdf
Do not commit data/ — the starter’s .gitignore excludes it.