| Assignment 7a: Backprop and Baselines |
|
CSE 415: Introduction to Artificial Intelligence The University of Washington, Seattle, Autumn 2026 |
|
Due Friday, December 4, at 11:59 PM, via
Gradescope (assignment "A7a").
There are no grace days for A7. Late work is accepted for three days, with a penalty, as
in A6: submissions on Saturday, December 5 earn 80% credit, on Sunday, December 6 60%, and
on Monday, December 7 40%. A7a closes at 11:59 PM on Monday, December 7.
A7 has two parts with separate deadlines. A7a (this page, 60 points) builds the tools: backpropagation, n-gram language models, and sampling. A7b (85 points, due Wednesday, December 9) uses them to build, train, and study BabyGPT, a small GPT that learns to write like the author of your choice. Together they are worth 145 points. You may do this assignment either individually or in a partnership of two; keep the same partner for A7a and A7b. If you work in a partnership, you'll be turning in one set of files for the partnership (see What to Turn In). You are permitted to use AI as a coding assistant in this assignment; however, we recommend that you read the guidelines for prompting (see the ED post about prompting guidelines under "General" here) and keep your prompting records from the start: the A7b report covers both parts, including the models and tools you used for agentic programming (see the Assignment Reports page). |
| Contents |
|
Introduction
Reading (before you write your predictions in Part 3). Aatish Bhatia, “Let Us Show You How GPT Works — Using Jane Austen,” The New York Times, April 2023 (free through the UW Libraries' NYT access). The article trains a small model, BabyGPT, on Jane Austen's novels and shows what it writes after 250, 500, … training steps: first random letters, then word-like strings, then something that reads, briefly, like Austen. In A7 you will do the same experiment yourself, with code you understand line by line. A language model is a probability distribution over sequences of tokens (for us, characters). By the chain rule of probability, the joint distribution factors into one next-token distribution per position: P(t1, t2, ..., tN) = P(t1) · P(t2 | t1) · P(t3 | t1, t2) · ... · P(tN | t1, ..., tN-1) So a language model needs just one ability: given a context, produce a distribution over the next token. Generating text is then repeated sampling. A6 asked you to do these steps by hand on tiny examples (backpropagation in Exercise 11; joint distributions of tokens in Exercise 12; softmax, temperature and sampling in Exercise 13). A7a has you write code that does them at scale:
A7a needs only standard Python: no NumPy and no PyTorch. (A7b uses PyTorch; installation instructions will be in A7b, along with a way to run it on Google Colab instead of your laptop.) The starter code contains these files:
|
|
Learning with an AI Coding Assistant
An AI assistant can write every function in A7a in a few seconds; this code is famous, and the assistant has seen many versions of it. That is fine, and you may use it. But the code is not the point. The point is that after A7 you can explain, without help, how a network computes its gradients and how a language model writes text, because the final exam will ask you to (on paper, like A6). Some ways to get there with an assistant:
|
|
Getting Started
Download and unzip the
A7a starter code.
It unpacks into a folder
Each function you write contains a line python autograder.py # all of A7a, including the predictions check python autograder.py -q q1 # one question python autograder.py -v # more detail about failures The autograder checks what your functions do (values, gradients, probabilities), not how they are written. Gradescope runs the same tests plus a few hidden ones of the same kinds, so passing every local test is a good sign but not a guarantee of full credit. None of the tests uses the numbers from A6. |
|
Part 1: Backpropagation by Hand (35 points)
A neural network computes its loss L through a long chain of small operations.
Each
a --\
(*) --> a*b --\
b --/ (+) --> a*b+1 --> (tanh) --> c
1 ----/
Backpropagation computes dL/dx for every a.grad += (d out / d a) * out.grad b.grad += (d out / d b) * out.grad
This is the same calculation as A6 Exercise 11, organized so that a computer can do it for
any network. In A7b, PyTorch's Q1: The Value Class (15 points)
In
Two things to get right. (1) Use The tests check values and derivatives of each operation, gradient accumulation (including a “diamond” graph), random expressions against numerical derivatives, and a graph 5,000 operations deep. Our solution adds about 50 lines, most of them in the seven operations. Q2: Softmax, Cross-Entropy, and a Neuron (10 points)
Write Q3: XOR, Two Ways (10 points)
Write python tinygrad.py to see the perceptron fail and the network succeed. The tests check one epoch of the perceptron rule step by step, that the perceptron learns AND but never classifies more than 3 of the 4 XOR points, and that your network classifies XOR perfectly for three random initializations. About 25 lines. |
|
Part 2: n-gram Language Models and Sampling (20 points)
Characters are represented by integer ids (the provided Q4: n-gram Models and Perplexity (10 points) An n-gram model predicts the next character from the previous n−1 characters only, using counts from the training text, with add-k smoothing: P(x | ctx) = (count(ctx, x) + k) / (count(ctx) + k V) V = vocabulary size
Without smoothing, any combination not seen in training would get probability 0, and so
would any text containing it. A context never seen in training gets the uniform distribution
1/V. In perplexity = exp( −(1/N) · ln P(text) ) N = number of predicted characters A model that chose uniformly among m characters at every step would have perplexity m; so perplexity is the model's “effective number of choices” per character. Its logarithm, the average negative log probability in nats, is the loss that BabyGPT reports during training, so the two kinds of model can be compared directly. When your code passes the tests, run python lm_basics.py data/austen.txt # or your corpus to see validation perplexity and a sample for n = 1 to 5 (about 15 seconds). About 15 lines. Q5: Temperature and Sampling (10 points)
Write |
|
Part 3: Predictions (5 points)
First choose your corpus for A7b from
The 5 points are awarded by the autograder, which checks only that P1–P4 are filled
in, with numbers where we ask for numbers (run |
|
What to Turn In
Turn in the following three files to Gradescope, in the assignment “A7a”: tinygrad.py lm_basics.py a7a_predictions_firstname_lastname.txt
Before submitting, run the whole local test suite with
Partnerships.
If you are in a partnership, (1) fill in the partnership comment at the top of
# Partnership? YES # Submitting partner: John Doe # Other partner: Jane Smith
(2) when you submit to Gradescope, indicate that this is a group submission and add your
partner, and (3) list both names and both corpora at the top of the predictions file.
If you are working alone, write |
| Updates and Corrections
If needed, updates and corrections will be posted here, and/or in ED. |
| Last updated 2026-11-29. |