CS6140 Machine Learning — Fall 2026

HW6 — Convolutional and Recurrent Networks

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

Where this HW sits. HW5's networks were stacks of fully-connected layers that ignore the structure of their input. HW6 adds the two architectures built around structure. Convolutional networks (Problems 1–2) exploit the fact that images are local and that the same pattern can appear anywhere. Recurrent networks (Problems 3–4) exploit the fact that sequences unfold in order, carrying a hidden state from step to step. As in every HW of this course, each key piece is built from scratch and then compared with the PyTorch library version. Problem 4's translator is the baseline that HW7's attention models and Transformers are compared against.

Setup. Same environment as HW5; see the NN setup page. The notebook gets every dataset itself.

Dev and full runs. The starter has one switch, MODE; both settings are given and printed by the notebook, so you never choose sizes.

Expected full-run time. Measured on an Apple M1 Max (32-core GPU): about 25 minutes for the whole notebook, the longest single training run (Problem 4's translator) about 5 minutes, plus the first-run downloads. Estimates for other machines: base M1/M2/M3 laptops (8–10-core GPU) about 2.5–3.5× longer; Colab's free T4 GPU about the same as the M1 Max; CPU only: use Colab, or run full mode overnight.

Instructions. Submit the completed notebook with its outputs, from full runs. Libraries (torch, torchvision, d2l, numpy, matplotlib) are allowed for data handling, plotting and standard layers. In this HW, "from scratch" means built from torch tensor operations, with autograd allowed, but without the built-in being replaced (no nn.Conv2d/F.conv2d inside your convolution, no nn.BatchNorm2d inside your batch norm, no nn.RNN inside your RNN). You may read the d2l book's code, but inside a TODO_STUDENT blank you may not call the d2l package's version of the piece you are building (nor the version in the shared course files lm_common.py / seq2seq_common.py).

Requirements.

Datasets. Fashion-MNIST (Problem 1; the same data as HW5, so HW5's MLP is your baseline). CIFAR-10 (Problem 2; 32×32 colour photos, 10 classes; 170 MB). TinyStories (Problem 3; short children's stories written with a small vocabulary, so a small language model can produce fluent text; we use the official 19 MB validation file, about 22,000 stories and 4.4 million words). Spanish–English sentence pairs (Problem 4; Tatoeba, sentence_pairs_large.tsv, filtered to pairs of at most 12 tokens per side: 100,000 training pairs, and 2,000 test sentences never seen in training, each scored against all its translations), the same data HW7 uses. The notebook downloads all of it on first run into the shared data/ folder (see the setup page). Problems 3–4 load their data through two shared files that come with the starter, lm_common.py and seq2seq_common.py, which HW7 uses too.

Starter code: hw6_26F_starter.ipynb with lm_common.py and seq2seq_common.py (download all three into one folder, e.g. cs6140/hw6/). The starter provides the device cell, data loading, training loops, plotting, and a checkpoint cell after each piece you implement: it copies your weights into the matching PyTorch module and checks that both give the same output, so you know a piece is right before you train with it. Written answers go in the notebook's markdown cells, each with a short How to think about it hint.


PROBLEM 1 — A CNN from Its Building Blocks (Fashion-MNIST)   [50 points]

Prerequisites: HW5 Problem 2 (an MLP classifier in PyTorch, softmax + cross-entropy, training loop); matrix products. Lecture notes: CNN note (Week 11) — cross-correlation, channels, padding and stride, pooling, LeNet, batch normalization.

What to see: the 1998 LeNet (sigmoid) trails the MLP; modernized, it matches the MLP with less than a third of its parameters and holds up far better on shifted images, because convolution builds in locality and weight sharing; batch norm makes training faster and more forgiving of the learning rate, and the batch-normalized LeNet beats the MLP outright.

Library calls: torch.nn.functional.conv2d, torch.nn.functional.max_pool2d, torch.nn.Conv2d, torch.nn.BatchNorm2d.


PROBLEM 2 — Deeper CNNs on Natural Images (CIFAR-10)   [50 points]

Prerequisites: Problem 1; HW5 Problem 2(F) (vanishing gradients through many layers). Lecture notes: CNN note (Week 11) — residual networks, data augmentation, transfer learning.

What to see: the 11-layer ResNet beats the batch-normalized LeNet by a wide margin; the explanation from lecture is that its shortcuts give the signal and the gradient a direct path around every block (this comparison changes depth and shortcuts together; optional Problem 7 separates them). Augmentation shrinks the train/test gap; pretrained features win by a wide margin when labelled data is scarce.

Library calls: torchvision.models.resnet18, torchvision.transforms.v2.


PROBLEM 3 — A Recurrent Language Model (TinyStories)   [60 points]

Prerequisites: HW5 (training loop, softmax + cross-entropy); the starter's opening walkthrough of tokens → IDs → embedding lookup → next-token targets. Lecture notes: RNN note (Week 12) — language models and perplexity, the recurrent cell, backpropagation through time, exploding and vanishing gradients, GRU and LSTM.

What to see: perplexity drops from the RNN to the GRU and LSTM, and the RNN's gradient fades with distance much faster than the gated models'. In the generated stories, look for which characters and scenes carry over from sentence to sentence and where a story contradicts itself (one sample per model is noisy; the perplexities are the firmer evidence).

Library calls: torch.nn.RNN, torch.nn.GRU, torch.nn.LSTM, torch.nn.utils.clip_grad_norm_.


PROBLEM 4 — Sequence-to-Sequence Translation, Spanish → English   [50 points]

Prerequisites: Problem 3 (an RNN that predicts the next word); padding a batch of different-length sentences. Lecture notes: RNN note (Week 12) — the encoder–decoder, teacher forcing, padding masks, greedy decoding, BLEU.

What to see: short everyday sentences come out right; longer sentences tend to score worse. One suspect is that the whole source sentence must fit into one fixed-size vector; HW7 tests that idea with attention.

Library calls: torch.nn.GRU, torch.nn.CrossEntropyLoss (with ignore_index); the d2l book's d2l.Seq2Seq as the package baseline.


PROBLEM 5 (THEORY) — Why an LSTM Reaches Further Back Than a Plain RNN   [25 points]

Prerequisites: Problem 3(C).

Problem 3(C) described what the curves show; here explain why. Using your Problem 3(C) plot: why does the plain RNN's gradient shrink with distance, and what in the LSTM changes that? Why does gradient clipping fix exploding gradients but not vanishing ones?


PROBLEM 6 (THEORY) — What Convolution Is (and Isn't) Robust To   [25 points]

Prerequisites: Problem 2(B).

A CNN trained on centred, upright objects fails on objects moved to a corner or rotated. Which changes does convolution + pooling handle on its own, which does it not, and which of these did your Problem 2(B) augmentation teach it?



OPTIONAL PROBLEMS [no credit]

Background: the CNN and RNN advanced notes.


References