Setup for the Neural-Network Homeworks (HW5–HW7)

HW5–HW7 use PyTorch plus the helper package of the Dive into Deep Learning (d2l) book. You set up one environment before HW5 (about 20 minutes) and add three packages for HW7. You do not need a GPU: every notebook has a small "dev" mode that runs on any laptop; a GPU (or Colab) matters only for HW6–HW7's full runs.

You never download or prepare data yourself: each notebook fetches what it needs the first time it runs.

1. Install (once, before HW5)

Use Python 3.13. Every package below installs from a ready-made wheel on 3.13, gensim included; on 3.14 gensim has none yet. Check with python3.13 --version. If you don't have it, get it from python.org (or brew install python@3.13 on a Mac).

In a terminal (macOS/Linux):

python3.13 -m venv ~/cs6140_env
source ~/cs6140_env/bin/activate        # repeat this line in every new terminal
pip install --upgrade pip
pip install torch torchvision numpy scipy pandas matplotlib scikit-learn jupyterlab requests tqdm
pip install d2l==1.0.3 --no-deps        # keep --no-deps (see below)
jupyter lab

Windows (Command Prompt): the same, except create and activate the environment with py -3.13 -m venv %USERPROFILE%\cs6140_env and %USERPROFILE%\cs6140_env\Scripts\activate. If you have an NVIDIA GPU, see "NVIDIA GPU" below before installing torch.

Why --no-deps? The d2l package pins very old versions of numpy/matplotlib that won't install on current Python. d2l itself works fine with current versions, so we install it without its pins.

2. Check that it works

Paste into a notebook cell and run:

import torch
from d2l import torch as d2l
device = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
print('torch', torch.__version__, '| device:', device)
data = d2l.FashionMNIST(batch_size=256)          # downloads ~30 MB the first time
print(len(data.train), 'training images')

Expected: no errors, a device name (cuda = NVIDIA GPU, mps = Apple Silicon GPU, cpu = no GPU, which is fine), and 60000 training images. Each starter notebook begins with a cell that picks your device automatically.

3. Folders

Keep the homeworks as sibling folders inside one course folder, each one exactly as you downloaded it (the notebook plus any .py files that come with it):

cs6140/
  hw5/   hw6/   hw7/
  data/          ← created by the notebooks; all downloads land here and are shared (about 0.5 GB in all)

A notebook first looks for its data in ../data; anything missing it downloads from the course's data folder or from the original source.

4. Per homework

HWExtra installsWhat the notebook downloads (first run)Run time
HW5none Fashion-MNIST and MNIST (~30 MB each); the Wine and 2-D Gaussian files a few minutes on any laptop
HW6none CIFAR-10 (~170 MB), pretrained ResNet-18 weights (~45 MB), TinyStories (~19 MB), Spanish–English sentence pairs (~24 MB) dev: about 4 min on a laptop CPU; full: about 25 min on an Apple M1 Max (see below)
HW7pip install transformers tokenizers gensim GPT-2's tokenizer files (~3 MB); everything else is HW6's data, reused dev: about 3 min on a laptop CPU; full: about 30 min on an Apple M1 Max

Dev and full runs (HW6–HW7). Each notebook has one switch at the top, MODE, and both settings are already chosen for you. Write and debug in "dev" (small data, small models, minutes on a CPU; its numbers are labelled not for answers). Then switch to "full", problem by problem. Your answers, and anything you show in office hours, must come from full runs.

HW6 → HW7. HW7 compares against your own HW6 full results. HW6's last cell writes results_hw6.json and hw6_seq2seq.pt into your HW6 folder. Keep both: HW7 finds them there if hw6/ is a sibling of hw7/, or next to the HW7 notebook. Files from a dev run are not accepted.

5. Which machine do I have?

Your machineWhat happensFull runs of HW6 / HW7 take about
Mac with Apple Silicon (M1–M4) Uses the Mac's GPU (mps) automatically. 25–30 min each on an M1 Max (measured); roughly 2.5–3.5× that on a base M1/M2/M3 (estimate)
Linux or Windows with an NVIDIA GPU Uses the GPU (cuda). On Windows this needs a special torch install (below). similar to the M1 Max or faster (estimate)
No GPU (Intel Mac, laptop without NVIDIA GPU) Runs on the CPU: fine for HW5 and for dev mode. do the full runs on Colab (below), or run them overnight

Optional details

Open these only if they apply to you.

Google Colab (free GPU in the browser)
NVIDIA GPU (Windows, Linux, WSL2)
Apple Silicon notes
Using plain PyTorch instead of d2l's Trainer

The starters use d2l's Module / Trainer classes, which match the book. You may use a plain PyTorch training loop instead (the most common style in practice), as long as you evaluate exactly as the starter does. This is what d2l.Trainer does for you:

model = model.to(device)
opt = torch.optim.SGD(model.parameters(), lr=0.1)
loss_fn = torch.nn.CrossEntropyLoss()
for epoch in range(num_epochs):
    model.train()                                  # dropout / batch norm in training mode
    for X, y in train_loader:
        X, y = X.to(device), y.to(device)
        loss = loss_fn(model(X), y)
        opt.zero_grad(); loss.backward(); opt.step()
    model.eval()                                   # evaluation mode
    with torch.no_grad():
        val_acc = sum((model(X.to(device)).argmax(1) == y.to(device)).float().mean().item()
                      for X, y in val_loader) / len(val_loader)
    print(f'epoch {epoch}: val acc {val_acc:.3f}')

Apart from HW7's Hugging Face packages (used as the reference to compare against), no other frameworks (Lightning, Keras, JAX) are needed.

conda users, and picking the right Jupyter kernel
Troubleshooting
SymptomFix
Installing d2l tries to build old numpy and fails You left out --no-deps. Run pip install --upgrade numpy matplotlib pandas scipy, then the d2l line again.
pip install gensim tries to compile and fails Your Python is not 3.13 (check python --version inside the environment). If you can't switch, skip gensim: HW7 then uses the course's saved gensim results.
ModuleNotFoundError: torch in Jupyter Wrong kernel; see the section above.
A notebook can't find lm_common.py / seq2seq_common.py, or HW7 can't find your HW6 results Keep each HW folder as downloaded, with the folders side by side (section 3). The error message lists the places it looked.
Out of memory, or the machine freezes during training Restart the kernel to free old models; work in "dev" mode until the full run.
DataLoader worker ... exited unexpectedly (usually in .py scripts) Put the script's code under if __name__ == "__main__":, or use num_workers=0.
OMP: Error #15 ... libomp already initialized (conda on Mac) Use the plain venv from Step 1 instead of conda.
Numbers differ slightly from the expected ranges, or between runs Normal (random initialization, GPU arithmetic). The notebooks compare with tolerances and give ranges, not exact values.

Still stuck? Post on Piazza with your OS, the output of the Step 2 check, and the full error message (as text, not a screenshot).