S1437 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, |1−ηλ|k.
  • 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 f=12(x2+100y2), whose gradient there is (1, 100). How far does its first step move each coordinate?

Name it

What the lab computes

  • Stable softmax cross-entropy: with m the largest logit, ln⁡∑jezj=m+ln⁡∑jezj−m, so no exponential overflows; the loss is that minus zk, and its gradient is the softmax minus the one-hot target.
  • Backpropagation through time: for ht=tanh⁡(wht−1+uxt) with h0=0 and loss L=hT, run backward from ∂L/∂hT=1: at each step δt=(∂L/∂ht)(1−ht2) adds δtht−1 to ∂L/∂w and δtxt to ∂L/∂u, gives ∂L/∂xt=δtu, and passes δtw back to ht−1.
  • Attention: for each query, scores 𝐪⋅𝐤j/d, a stable softmax, and the weighted average of the values; with a causal mask, query i sees only keys 0 to i.
  • Adam: 𝐦←β1𝐦+(1−β1)𝐠, 𝐯←β2𝐯+(1−β2)𝐠2, then 𝐱←𝐱−η𝐦^/(𝐯^+ϵ) with 𝐦^=𝐦/(1−β1t) and 𝐯^=𝐯/(1−β2t).

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 h0=0,h1,…,hT, and rnn_grads(w, u, xs) returning ∂hT/∂w, ∂hT/∂u and the list of ∂hT/∂xt.

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?