S14 · Lesson 37 · Neural network lab
A softmax cross-entropy that can't overflow, backpropagation through time for a recurrent unit checked against finite differences, scaled dot-product attention with a causal mask, and the Adam optimizer on a badly conditioned bowl
Deep-learning libraries hide four pieces of code that every model depends on: a loss that stays finite for any logits, gradients through time for recurrent networks, attention, and an optimizer that copes with curvatures differing by orders of magnitude. Lessons 24 to 29 explained each. In this lab you'll write them in plain Python, small enough to check every number by hand or by finite differences, and see where each one would go wrong if written naively.
- Gradient descent with step size η on a quadratic bowl whose curvatures are 1 and 100. The curves show how the error along each direction evolves, .
- The steep direction limits the step: above η = 0.02 it diverges, while the shallow direction still crawls.
- Adam (task 4) rescales each coordinate by its own gradient history, so both directions take steps of about the same size, which removes most of this conflict.
Predict
Adam (learning rate 0.1) starts at (1, 1) on , whose gradient there is (1, 100). How far does its first step move each coordinate?
After bias correction, Adam's first step is : the learning rate times the sign of each gradient component, so both coordinates move by 0.1 whatever their gradients' sizes. Plain gradient descent would move y a hundred times further than x and, at this rate, overshoot wildly.
Name it
What the lab computes
- Stable softmax cross-entropy: with m the largest logit, , so no exponential overflows; the loss is that minus , and its gradient is the softmax minus the one-hot target.
- Backpropagation through time: for with and loss , run backward from : at each step adds to and to , gives , and passes back to .
- Attention: for each query, scores , a stable softmax, and the weighted average of the values; with a causal mask, query i sees only keys 0 to i.
- Adam: , , then with and .
Check
Write softmax_xent(z, k): the cross-entropy loss for logits z and true class k, and its gradient with respect to z, without overflow for any logits.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rnn_forward(w, u, xs) returning the states , and rnn_grads(w, u, xs) returning , and the list of .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write attention(Q, K, V, causal=False) for lists of row vectors, returning one output row per query.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write adam(grad, x0, lr, steps, b1=0.9, b2=0.999, eps=1e-8), returning the final point.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does gradient descent with one learning rate struggle on a loss whose curvature differs greatly between directions, and how does Adam help?
A step of size η along a direction of curvature λ shrinks the error there by , so η must stay below 2/λ for the steepest direction or that direction diverges. With such a small step, the shallow directions shrink by only per step, and training takes about as many steps as the ratio of the two curvatures. Adam divides each coordinate's step by a running estimate of its gradient's size, so coordinates with large gradients take proportionally smaller steps and those with small gradients larger ones. When the curvature differences line up with the coordinates, this nearly equalizes them; it helps less when the steep directions are mixtures of many coordinates.