CS6140 Machine Learning — Fall 2026

HW5 — Neural Networks, Backprop, Autoencoders, VAE, GAN

Make sure you check the syllabus for the due date. Please use the notations adopted in class.

Instructions. Submit your code (Jupyter notebook encouraged) together with a short report of results (tables + plots). You may use libraries (numpy, matplotlib) for data handling, plotting, and basic math. PROBLEM 2 has a from-scratch part (A) and a PyTorch part (B): implement the from-scratch part first, then confirm it with a PyTorch reimplementation, evaluated the same way — same pattern as HW1–4's library-baseline checks, just implemented the other way around. PROBLEMS 3 and 4 are PyTorch-only generative models (extending PROBLEM 1's autoencoder, and HW4 Problem 3's toy data, respectively) and don't have a from-scratch counterpart. (HW5's Word Vectors problem has moved to HW7, alongside Attention/Transformers, which consume word/token representations the same way.)

Requirements.

Datasets. Links below are relative to the course data folder (../../data/...) unless noted otherwise. Autoencoder (Problem 1): a small self-contained 8-input/8-output identity task (no external file — the 8 training points are given inline in Problem 1). Wine (Problem 2, and Problem 5): train / test (3 labels, 13 features). Digits (Problem 3, required; Problem 6, optional): sklearn.datasets.load_digits() for Problem 3, and additionally the larger (train images, train labels, test images, test labels) MNIST-style set for Problem 6. 2-D Gaussian mixture (Problem 4): the same 2gaussian.txt / 3gaussian.txt toy data from HW4 Problem 3.

Starter code. No wired-up starter notebook exists yet for this HW (unlike HW1–4's _26F_starter.ipynb) — build your own data-loading/training harness following the per-problem specs below, and compare your from-scratch result against the matching library/PyTorch problem the same way HW1–4 compare against a library baseline. Answers to the (THEORY) problems, and to written parts embedded in other problems, may be written directly in the notebook (e.g. a markdown cell) rather than a separate document, as long as they include any equation, plot, or illustration the answer depends on — not prose alone.


PROBLEM 1 — Autoencoder Neural Network, from scratch   [40 points]

Consider a neural network with 8 input units, 3 hidden units, and 8 output units, all sigmoid activations:

picture

(A) The 8 training inputs are identical to the outputs, as shown in the table above. Implement this network and the backpropagation algorithm (square loss, sigmoid activation) from scratch to compute all the network weights; initialize the weights with nontrivial values (not values that already minimize the error).

Hint: on the trained network, you should obtain hidden-unit values similar to the ones shown in the table (up to symmetry). Feel free to make implementation changes that suit your approach, but briefly document them.

(B) Since the outputs and inputs are identical for each datapoint, this network can be viewed as an encoder-decoder mechanism (one of the standard uses of neural networks). In a short, non-technical written answer, explain the purpose of the training algorithm in this context.

References: a worked algorithm sketch; Mitchell's backprop chapter; Brilliant's backpropagation article.


PROBLEM 2 — Classifier Neural Network   [60 points]

(A — 40 points, from scratch) Implement a multi-class supervised neural network from scratch. Train and test on the Wine dataset (3 labels, 13 features). Layers should look like 13-input, k-hidden, 3-output; use square loss and sigmoid activations throughout.

(B — 20 points, using PyTorch) Train and test the same architecture using PyTorch, and compare train/test accuracy against your from-scratch result.

Library calls: torch.nn.



PROBLEM 3 — Variational Autoencoder (VAE), using PyTorch   [50 points]

PROBLEM 1's autoencoder learns to compress and reconstruct, but its latent codes have no reason to be organized in any particular way, and there's no principled way to generate a new point — you'd have to guess a latent vector and hope the decoder produces something meaningful. A Variational Autoencoder (VAE) fixes this by making the latent space itself a probability distribution shaped like a simple prior, which is exactly what makes sampling and interpolation meaningful. This problem extends PROBLEM 1's encoder-decoder idea into a real (small-scale) generative model, in PyTorch, using sklearn.datasets.load_digits() (8×8 digit images, 10 classes).

Structurally, this is less new than it sounds: the decoder is the exact same "stack of Linear + activation" recipe as PROBLEM 1's decoder half. The only new architectural shape is the encoder having two output heads (one for $\mu$, one for $\log\sigma^2$) sharing one trunk, instead of one. See the VAE lecture note's "How new is this, structurally?" section for the full comparison (and a code-reuse hint).

Library calls: torch.nn.


PROBLEM 4 — Generative Adversarial Network (GAN), using PyTorch   [30 points]

HW4 Problem 3 fit a Gaussian mixture to 2gaussian.txt by explicitly modeling and maximizing a likelihood. PROBLEM 3 above optimizes an explicit lower bound on a likelihood. A GAN takes a third approach: it never writes down a density at all.

Structurally, neither network here is new either: $G$ is the same "stack of Linear + activation" recipe as PROBLEM 1's decoder or PROBLEM 3's decoder (a vector in, a vector out) — just fed noise instead of a code, and usually with no final squashing activation (it outputs raw data values, not a probability). $D$ has the same shape as PROBLEM 2's classifier (a vector in, a score out), usually left as a raw linear score with no sigmoid, letting the loss function apply it internally. See the GAN lecture note's "How new is this, structurally?" section for the full comparison (and a code-reuse hint).

Library calls: torch.nn.


PROBLEM 5 (THEORY) — Why Dropout Lowers Train Accuracy but Raises Test Accuracy   [25 points]

Add dropout (dropping each hidden unit independently with probability $p=0.5$ during training) to your PROBLEM 1 or PROBLEM 2 network. Compared to the same network trained without dropout: training accuracy is now lower, but test accuracy is higher, and the train/test gap shrinks substantially.

In a short written answer, explain why dropout produces this pattern, and explain (conceptually) why dropout is turned off at test time rather than left on.


PROBLEM 6 (THEORY) — Diagnosing Learning Rate from a Loss Curve   [25 points]

Suppose two students train the same network with gradient descent and plot training loss vs. iteration. Student A's curve decreases for a few iterations, then starts oscillating wildly and trending upward. Student B's curve decreases smoothly but extremely slowly, flattening out after many iterations at a loss value still far above what a properly-tuned run achieves.

In a short written answer, diagnose each student's problem in terms of the learning rate, and explain the mechanism (not just "it's too big/small") behind each pattern.



PROBLEM 7 — Max-Likelihood Classifier Neural Network   [optional, no credit]

(A, from scratch) Implement a multi-class supervised neural network using a maximum-likelihood (cross-entropy) objective instead of PROBLEM 2's square loss. Train and test on the Wine dataset. Use cross-entropy loss with a softmax output layer, and either sigmoid or ReLU hidden-layer activations.

(B, using PyTorch) Train and test the same network using PyTorch.


PROBLEM 8 — Max-Likelihood Network on Digits   [optional, no credit]

Run your PROBLEM 7 max-likelihood neural network on a medium-size dataset (Digits_small) and on a larger one (the Digits/MNIST-style set linked above, about 60,000 28x28 images, 10 classes).