Lab

Watch a neural network learn

Train a real network in your browser, then take it apart: one neuron, one backpropagation step, one learning rate at a time.

People say a neural network “learns from data”. What does that actually mean? Nobody writes the rules. The network starts as a pile of random numbers, and a simple procedure nudges those numbers, thousands of times, until the network’s answers match the examples.

This page builds that up one piece at a time. First you move a single neuron by hand, then let gradient descent move it for you. Next a whole network, written from scratch for this page, trains in your browser while you change its shape. Then you open it up: one backpropagation step with every number shown, and the learning rate on a landscape you can see. Last comes what all this means for the memory a model needs.

By the end you will know what a single neuron computes, how backpropagation works out which way to nudge each weight, why the size of the nudge matters so much, and why training a model needs far more memory than running one.

Before this: How a model picks the next token shows how a model’s raw outputs, its logits, are turned into a choice. This page shows where those numbers come from: a network whose weights were trained.

One neuron, by hand

A neuron does very little. It multiplies each input by a weight, adds them up with a bias, and squashes the result:

p=σ(w1x1+w2x2+b),σ(z)=11+e−z

The weighted sum is zero along a straight line, so one neuron can only ever split the plane with a straight line. The weights set the line’s angle, the bias shifts it, and the squash turns “how far over the line” into a probability between 0 and 1.

To say how wrong the neuron is, we use cross-entropy loss: for each point, minus the log of the probability it gave to the right answer. A confident right answer costs almost nothing. A confident wrong answer costs a lot. The widget averages it over 40 points.

One neuron, by hand

0 gradient steps
−1.20
0.60
1.00

Loss
1.200
Correct
16 of 40
Slope of the loss (the gradient):
∂L/∂w₁ = −0.851
∂L/∂w₂ = −0.893
∂L/∂b  = 0.184
The neuron outputs σ(w₁x₁ + w₂x₂ + b). The solid line is where that sum is zero (output 0.5); the arrow shows which way the output rises. Loss is mean cross-entropy over the 40 points, the same loss the playground uses.

It starts badly placed: a loss of 1.200 with 16 of 40 points right. Try to fix it with the sliders before pressing the button. Then press Let gradient descent do it. After 120 steps the loss is 0.009 and all 40 points are right.

The three numbers under the sliders are the gradient: how much the loss would change if you nudged each slider up a little. At the start, ∂L/∂w₁ is −0.851, which says “increase w₁ and the loss falls”. Gradient descent just follows that advice: move every weight a small step against its gradient, then measure again.

A network learns

One neuron draws one straight line. Put a few in a layer, feed their outputs to another layer, and the lines combine into curves. Below, a small network learns to tell blue dots from pink dots using only their position, with every weight trained by the same gradient descent you just watched. Each lesson sets it up to show one idea.

Neural network playground

31 parameters

Two hidden layers bend a boundary around the inner cluster. The test loss follows the training loss down because the pattern is real.

Speed

Prediction over the plane
Class A Class B Test point
The network2 → 4 → 3 → 1

Hover over or tap a line or circle to read it

0.00.20.40.60.80246810Epoch
Training lossTest loss

Data

0.15
50% (150 points)
Inputs

Network

Hidden layers
Layer 1 neurons4
Layer 2 neurons3

Training

0.030
10
0.00
Regularisation
0.0010
The square plot shows what the network predicts at every point: blue for class A, pink for class B, fading towards the background where it is unsure. The thin line is its decision boundary. Filled dots train it; hollow dots are held back to test it. In the network diagram, each circle shows what that neuron responds to across the same plane, and each line is a weight: blue positive, pink negative, thicker for larger. Hover over or tap one to read it.

Backpropagation: blame, passed backwards

With one neuron the gradient is easy. With layers of them, how does a weight deep inside know its share of the blame? Backpropagation is the answer. It is the chain rule from calculus, applied neatly from the loss back to every weight.

Step through one example below. The hidden neurons’ weighted sums are called z₁ and z₂, the output neuron’s sum is u, and its squashed value p = σ(u) is the prediction. Nothing is skipped. Each number is rounded to four decimal places before the next line uses it, so you can check every line with a calculator.

Backpropagation, one number at a time

Round 1 · step 1 of 12
1.0
0.5
Target y
w₁₁ 0.500w₁₂ −0.400w₂₁ 0.300w₂₂ 0.800v₁ 0.700v₂ −0.500biases: b₁ = 0.100, b₂ = −0.200, c = 0.050x₁1.000x₂0.500h₁h₂ploss

The example

One training example goes in: two numbers, and the answer we want. Naming: wⱼₖ is the weight into hidden neuron j from input k, so w₂₁ carries x₁ into h₂; v₁ and v₂ carry the hidden values into the output. Each neuron also adds its own constant, a bias: b₁ and b₂ for the hidden neurons and c for the output, listed at the top of the diagram. Nothing has been learned yet.

x₁ = 1.0000, x₂ = 0.5000
target y = 1
A 2-2-1 network with sigmoid units and cross-entropy loss. Each number is rounded to four decimal places before the next line uses it, so every line checks on a calculator. The unit tests confirm these gradients agree with the playground’s own full-precision backpropagation to within 0.001.

With the starting weights and the example x = (1.0, 0.5), target 1, the network predicts p = 0.5394 and the loss is 0.6173. Going backwards, the slope of the loss with respect to the output sum u collapses to p − y = −0.4606. That tidy shortcut is why sigmoid outputs and cross-entropy are usually paired.

Notice how the blame shrinks as it flows back. The output’s δ is −0.4606, but the hidden neurons get only −0.0775 and 0.0541, because each pass through a sigmoid multiplies by h(1 − h), which is at most 0.25. Stack many sigmoid layers and the early ones barely learn. This is the vanishing gradient problem, and it is one reason modern networks prefer ReLU-like activations (large language models use relatives such as GELU and SwiGLU).

After one update with learning rate 0.5, the same example gets p = 0.6416 and a loss of 0.4437. Press Apply the update and go again a few times and watch it climb towards 1.

What you just did, in general: if a weight connects input value a to a neuron whose sum z has blame δ = ∂L/∂z, then

∂L∂w=δ×a δhidden=(∑nextδnext×wnext)×σ′(z)

Blame at the output end times the signal at the input end. Each hidden neuron collects blame from the neurons it feeds, weighted by the connecting weights, and scales it by the slope of its own activation.

The playground runs exactly this procedure on every point in a mini-batch, adds up the gradients, and takes a step. The unit tests check that this widget’s gradients agree with the playground’s full-precision code to within 0.001, and check that code against a brute-force estimate: nudge each weight by a millionth, measure the change in loss, and compare.

The step size

The learning rate decides how far each step goes. Too small and training crawls. Too large and every step overshoots the valley floor, landing higher up the opposite wall.

Real loss surfaces have millions of dimensions, but near a minimum they look like a bowl. A bowl is enough to see all four behaviours. This one is ten times steeper in one direction than in the other.

Learning rate on a loss landscape

Step 0 of 60
Learning rate
0.180

Verdict
Oscillating
Loss after 0 steps
7.561
Steep direction, each step
× 0.800
flips side each step
Shallow direction, each step
× 0.820
shrinks

Along each direction, the distance to the minimum is multiplied by |1 − ηλ| every step. Above η = 2/λ₁ = 0.200 the steep direction grows instead of shrinking.

0.0010.010.11101001,0000102030405060Step
A bowl-shaped loss over two weights, steep in one direction (curvature λ₁ = 10) and shallow in the other (λ₂ = 1). Contours join points of equal loss; the cross marks the minimum. Each step moves against the gradient by the learning rate times its size.

The maths of a bowl gives an exact rule. Along a direction with curvature λ, each step multiplies the distance to the minimum by |1 − ηλ|, where η is the learning rate. Here the steep direction has λ = 10, so anything above η = 2/10 = 0.2 makes that factor bigger than 1, and the path flies off. Between 0.1 and 0.2 the factor is still below 1, but 1 − ηλ is negative: the path zigzags across the valley, shrinking each time.

The catch is that the shallow direction (λ = 1) wants a much bigger step than the steep one can tolerate. At η = 0.09 the steep direction settles quickly, but the shallow one only shrinks by × 0.91 per step. Compare it with Oscillates (η = 0.18): the path zigzags, yet it reaches the minimum first, because the shallow direction now shrinks by × 0.82 per step. For a bowl the fastest rate is 2/(λ₁ + λ₂), about 0.18 here, just short of the edge where everything blows up. That is why tuning learning rates is fiddly. This mismatch between directions is also why plain gradient descent is slow on real networks. Momentum, which the playground offers, and optimisers such as Adam exist largely to deal with it.

What this means for local AI

Inference is the forward pass only. No gradients, no optimiser state, no stored activations for a backward pass. When you run a model locally, you are doing the first half of what this page shows, billions of times over. A language model’s output layer is this page’s output neuron scaled up: instead of one number it produces one logit for every token in its vocabulary, and the sampler turns those logits into a choice.

The large language models you can run at home are the same machine, scaled up. A model described as “8B” has about eight billion weights where this page has a few dozen. It is trained by the same loop: forward pass, loss, backpropagation, update. It just uses far more data, and the layers are more elaborate.

The playground’s default network has 31 parameters. Training it needs, for each weight, its value, its gradient and the optimiser’s running state. That is trivial here and enormous at scale. The standard recipe for large models trains in mixed precision with the Adam optimiser. It keeps 16-bit weights and gradients (2 bytes each), plus a 32-bit master copy of the weights and two 32-bit running averages (4 bytes each). That is 16 bytes per parameter, before counting activations. Running the same model needs only the weights: 2 bytes per parameter at 16-bit, or roughly 0.6 bytes at 4-bit (a Q4_K_M file of Llama 3.1 8B is 4.58 GiB). (For what running a model really takes on your hardware, including the context cache, see Will it run on my machine?)

Memory to train a model vs. to run it

Weights and optimiser state only

Training it

NF4 (QLoRA) fine-tuneabout 5.0 GiB · fits
LoRA fine-tuneabout 16 GiB · fits
Full training, Adam119 GiB · too big

For comparison: running it at 16-bit

Run, 16-bit15 GiB · fits
Full training vs. running at 16-bit
8×
the memory, before activations
Training options that fit in 24 GiB
2 of 3
full training needs 119 GiB
Bytes per parameter: full training with Adam in mixed precision 16 (16-bit weights and gradients plus 32-bit master weights, momentum and variance); 16-bit weights 2; NF4, the 4-bit format QLoRA keeps the frozen model in, about 0.52. The LoRA rows assume adapters of 1% of the model, trained with Adam. Activations and the context cache come on top of every row. For an accurate check of whether a model runs on your machine, see Will it run on my machine?

So on a 24 GB graphics card, an 8B model runs even at 16-bit (about 15 GiB of weights for Llama 3.1 8B, leaving some room for context). Fully training it would need about 120 GiB just for weights and optimiser state, and activations come on top. That is why almost nobody fully fine-tunes at home. Instead people use LoRA, which freezes the base model and trains small adapter matrices, or QLoRA, which also stores the frozen base in NF4, a 4-bit format. Backpropagation still runs through the whole network, but gradients and Adam state are kept only for the adapters. That is what makes fine-tuning an 8B model fit on a single home GPU.

What we simplified

  • Tiny and two-dimensional. Real networks take thousands of inputs and have millions to billions of weights. We use two coordinates and at most 289 parameters so you can see every one.
  • Plain SGD. The playground uses stochastic gradient descent with optional momentum. Large models almost always use Adam or a relative of it, which adapts the step size per weight.
  • One output, two classes. Language models choose among tens of thousands of tokens with a softmax output. The sigmoid-plus-cross-entropy pair here is its two-class special case.
  • No transformer parts. Attention, layer normalisation, residual connections and embeddings are absent. Each changes how signals flow, but not the train-by-gradient loop.
  • Fixed, tiny datasets. 300 points, regenerated deterministically. Real training streams billions of examples and rarely sees one twice.
  • A bowl, not a real landscape. The learning-rate figure uses an exact quadratic so the maths is clean. Real loss surfaces have plateaus, saddle points and cliffs, and the safe learning rate changes during training.
  • Memory is weights and optimiser state only. The memory figure leaves out activations, the context (KV) cache and framework overhead. The 4-bit and LoRA rows are estimates; the real numbers depend on the format and adapter rank.

Next: How attention decides what matters looks at the part a transformer adds to this kind of network.

Inspired by, and worth your time