Eight worked examples · forward pass · backpropagation · update · generalization · images · text · attention

Neural Networks by Hand

Eight small networks, each solving a concrete problem. The first two can be checked with pencil and paper. The third adds a lookup table. The fourth is the shape of a real network, with the same three moves inside it: predict, measure the miss, nudge every weight. The fifth meets real data and learns the hardest lesson: a network can memorize instead of learn. The sixth reads real handwritten digits. The seventh reads text and writes some of its own. The eighth reads the same text with attention: the transformer inside today's language models, at pencil-and-paper size.

EXAMPLE 12 inputs · 1 neuron · 3 parameters

Fruit Stand Neuron

A stall sells apples and bananas and charges a bag fee. From receipts alone, one neuron works out the prices. Every number fits on a napkin.

  • weighted sum, ReLU, squared-error loss
  • the gradient: error signal × the input each weight multiplied
  • the update: step against the gradient, learning rate 0.1
pencil and paper
Open example 1
EXAMPLE 22 inputs · 3 neurons · 9 parameters

Hallway Light Network

Two switches, one light, on when exactly one switch is up. A single neuron can never learn this. A hidden layer of two can, and the first step is all whole numbers.

  • why a hidden layer is needed (XOR)
  • blame travels back along the same weight that carried the signal
  • a closed ReLU gate blocks the blame
pencil and paper
Open example 2
EXAMPLE 33 inputs · 3 neurons · 11 parameters

Will It Rain Tomorrow?

Three warning signs in, a probability of rain out. Sigmoid neurons, a lookup table for σ, and cross-entropy loss, which scores a probability by how surprised you are.

  • σ(z) and its shortcut derivative σ(1 − σ)
  • cross-entropy: −ln of the probability given to what happened
  • why sigmoid + cross-entropy leaves the error signal as p − y
lookup table
Open example 3
EXAMPLE 47 inputs · 8 neurons · 51 parameters

Rain Tomorrow, Two Hidden Layers

The same question with a real diary: temperature, rain today, the month. Two hidden layers, random starting weights, and twelve held-out days to check the network generalizes rather than memorizes.

  • preparing inputs: scaling temperature, encoding the month as sin and cos
  • a neuron that feeds several neurons gets the sum of their blame
  • train versus test loss, epoch by epoch
follow the page
Open example 4
EXAMPLE 56 inputs · up to 32 → 32 neurons · 1,313 parameters · 768 real patients

When the Network Memorizes

The real Pima Indians Diabetes dataset, no hidden rule anywhere. A big network trained on a hundred patients drives its training loss to zero while its answers for new patients get worse. Cause the overfitting, then try the four standard cures and see what each one really does.

  • cleaning real data: missing values recorded as zero, standardizing with training statistics only
  • training loss versus validation loss, and why only the held-out set is an honest judge
  • smaller network, more data, weight decay, early stopping
real data
Open example 5
EXAMPLE 628 × 28 MNIST digits · 4 filters of 3 × 3 · max pooling · softmax · 6,810 parameters

Reading Handwritten Digits

Real handwritten digits from MNIST, 2,000 to learn from and 500 never seen. A convolutional network slides four tiny filters across each picture, so a stroke learned in one corner is recognized in every corner, and reaches about 94% on new digits. Draw a digit yourself and watch the ten probabilities.

  • a filter is a neuron that moves: one 3 × 3 patch times nine weights, checkable by hand
  • feature maps, ReLU, max pooling, flatten, and softmax for ten answers
  • blame through pooling, and a shared weight that collects blame from every position
real data
Open example 6
EXAMPLE 7text, one character at a time · hidden state of 12 · 531 parameters

One Letter at a Time

Four sentences about a cat and a dog. A recurrent network reads them character by character, carrying a 12-number memory from one step to the next and guessing the next character each time. The first step of a snippet is a column lookup plus a tanh table, checkable by hand. Then type a few letters and let it continue.

  • memory as a hidden state, one-hot input as a column lookup, unrolling in time
  • backpropagation through time: blame from the future adds to blame from now
  • vanishing gradients seen step by step, and gradient clipping to stop explosions
text
Open example 7
EXAMPLE 8text, 16 characters at once · embeddings of 8 · one attention head · 855 parameters

Every Letter Looks Back

The same four sentences, read by a transformer: the architecture inside GPT-style language models. No memory passed step by step; instead every character looks directly at every character before it and decides how much to borrow from each. Starts with what a transformer is, in plain words, then steps through embeddings, attention, the feed-forward network and the backward pass with real numbers.

  • character and position embeddings; query, key and value; scores, softmax and the causal mask
  • residual connections and a feed-forward network applied to each position alone
  • blame through attention: one hop from any position to any earlier one, so nothing vanishes
text
Open example 8

How to use the pages

Every example has the same controls. Next advances one stage: read the data, forward pass stage by stage, loss, backward pass stage by stage, update. Each stage shows the formula, the same formula with the real numbers substituted, and the result, while the diagram highlights the part of the network being computed.

→ or space
next stage
←
previous stage, or undo the last update
Finish
apply the update for the current row and move on
Train N epochs
run whole passes over the data and watch the weights and loss
Ask the network
give it an input it may never have seen and read its answer

green is the forward pass, red is the backward pass, yellow is the thing being computed right now.

The vocabulary, in one place

weight
a number a neuron multiplies an input by; the thing that learns
bias
a weight on an input that is always 1; shifts the sum up or down
activation
the bend applied to the sum: ReLU keeps positives, sigmoid squashes to 0..1
loss
one number saying how wrong the prediction was
gradient
for each weight, how much the loss would change if that weight went up a little
error signal δ
the gradient at a neuron's sum; the blame that arrives at that neuron
backpropagation
computing δ for the output first, then passing it back one layer at a time
learning rate η
how big a step to take against the gradient
epoch
one pass through all the training rows
generalization
getting rows right that were never used for training

Each example also ships as a short Python script (nn.py to nn8.py) that prints the same numbers, so the pages can be checked independently. The real datasets for examples 5 and 6 are in the data folder with their provenance.