1/32
Part 1 of 6 · The chain ruleLearn

Backprop by hand, for a two-layer network

One OpenAI ML engineer candidate reports an ML coding round on a matrix multiplication's forward pass and backprop, written in PyTorch from a code skeleton (1 report). The same candidate says NumPy fluency was essential (1 report). Backprop by hand prepares you for both, and the practice problem asks for it on the smallest network that has every part. Our version reads like this:

Here's a two-layer network in NumPy: a linear layer, a ReLU, a second linear layer, and the mean softmax cross-entropy over the batch. Write forward_backward(params, x, y). Return the loss and a gradient for each of W1, b1, W2 and b2, with no autograd. Then show me it's right.

ArrayShape
x(N, D): N examples of D numbers
W1 and b1(D, H) and (H,)
W2 and b2(H, K) and (K,)
y(N,): one label per example, from 0 to K - 1

The practice comes in four levels. Level 1 checks only the loss. Level 2 checks every gradient against nudging each weight, then trains the network on three blobs of points. Level 3 multiplies W2 by 10,000, so the logits reach the tens of thousands and the loss must stay finite. Level 4 writes one training step that clips all the gradients together by their total length.

You don't write it yet. The lesson starts with what backprop computes and the one rule behind every step, then builds each gradient in the order the backward pass needs them. At the end you solve the whole problem, level by level, in an interview simulation with a 45-minute timer.

This lesson builds on the NumPy lesson's shapes and broadcasting, and on the softmax from the attention lessons. The matmul backward lesson comes next and writes a backward pass of your own in PyTorch, the round the report describes.