Neural-Engine · a field guide

How a neural network learns

Every part of a neural network, explained by letting you move it. Drag the sliders, step through the maths and watch a real network learn in your browser. Each chapter matches a file in this repo.

repeat thousands of times
Chapter 1

Why we need neural networks

Normal programming means you write the rules. That works for tax calculations, and breaks down for things like "is there a cat in this photo?". Nobody can write down the rules for a cat.

A neural network flips this around. You give it examples with the right answers, and it works out the rules itself by adjusting thousands of numbers called weights.

Traditional code

rules + input → answer

You think of the logic. Breaks when the logic is too fuzzy to write down.

Neural network

examples + answers → rules (weights)

The machine finds the logic. You only need enough good examples.

Chapter 2 · nn.py → Neuron

One neuron, the building block

A neuron does three small things:

1. Weigh the inputs. Multiply each input by its weight. A big weight means "this input matters a lot". A negative weight means "this input pushes me down".
2. Add them up, plus a bias, which shifts how easy it is to switch on.
3. Squash the result with an activation function such as tanh.

Move the sliders. Line thickness shows how strong each weight is, and the colour shows its sign: blue is positive and orange is negative.

Inputs (the data)
Knobs (what training changes)
Activation function

Why squash at all? Without an activation, a neuron is a straight line. Stacking straight lines only gives you another straight line, however many layers you add. The bend in tanh or ReLU lets a network draw curved boundaries. You'll see this fail on purpose in Chapter 5.

Chapter 3 · lessons/01_what_is_a_gradient.py

Learning means rolling downhill

To learn, the network needs to know how wrong it is. That number is the loss. A common one is (prediction − target)²: zero when perfect, and bigger the more wrong it is.

Picture the loss as a valley. The weight is a ball sitting on the slope. The gradient is the steepness under the ball. To go down, step against the slope:

new weight = weight − learning rate × gradient

That one rule is gradient descent. Here the best weight is 3, but the ball doesn't know that. It only feels the slope. Press Step a few times, then try a learning rate of 0.95 and then 1.05.

weight
loss
slope

Click anywhere on the curve to drop the ball somewhere new.

Chapter 4 · engine.py → backward()

Backpropagation: finding every slope at once

In Chapter 3 there was one weight. A real network has thousands or billions, and we need the slope for each one. Nudging them one at a time would mean re-running the whole network once per weight, which is far too slow.

Backpropagation gets all of them in a single pass backwards through the calculation. It relies on the chain rule: the effect of a number on the output is its local effect multiplied by the effect of whatever comes after it.

Below is the neuron from Chapter 2, drawn as a graph of small operations. Run the forward pass, then step backwards and watch the gradients flow from right to left.

Chapter 5 · train.py

Watch a network learn XOR

XOR means "one or the other, but not both". Its four answers sit on the corners of a square, and no single straight line can separate the 1s from the 0s. One neuron can't do it. A network with a hidden layer can.

Press Train. The coloured map shows what the network currently answers for every point. Watch it bend to fit the corners as the loss drops. Then try these:

• Set hidden neurons to None and train on XOR. It gets stuck, because a single neuron can only draw a straight line.
• Switch the dataset to AND or OR. A single neuron solves those easily.
• Push the learning rate up to 2 and see what happens.

step
0
loss
right
What the network answers
answers 1answers 0
The network Loss over time
x1x2TargetNetwork saysRaw output
Chapter 6

Where each idea lives in the code

Everything on this page runs on the same tiny engine as the Python files, translated to JavaScript. Once the pictures make sense, read the code in this order:

IdeaFileLook for
Gradient and gradient descentlessons/01_what_is_a_gradient.pyw = w - lr * slope
Chain rule on one neuronlessons/02_backprop_by_hand.pyout.backward()
Each operation's local slopeengine.pythe _backward functions
Backward pass over the graphengine.pyValue.backward (topological sort)
Neuron, layer, networknn.pyNeuron.__call__, MLP
Forward, loss, backward, updatetrain.pythe 4 numbered steps in the loop
Proof that backprop is righttest_engine.pybackprop vs numeric slope

Glossary

Weight
A knob on each connection. Training changes these. It decides how much one input matters.
Bias
A knob on each neuron that shifts how easily it switches on.
Activation
The squashing function (tanh, ReLU, sigmoid) that lets networks bend instead of only drawing straight lines.
Layer
A group of neurons that all look at the same inputs.
Hidden layer
A layer between the inputs and the output. It builds the features the output neuron needs.
Forward pass
Running inputs through the network to get a prediction.
Loss
One number saying how wrong the predictions are. Training tries to make it small.
Gradient
For each weight: if I nudge it up, how much does the loss change, and in which direction?
Backpropagation
Computing every gradient in one backward pass using the chain rule.
Learning rate
How big a step to take downhill. Too small is slow. Too big overshoots or blows up.
Gradient descent
The update rule: weight −= learning_rate × gradient, repeated many times.