How Neural Networks Learn


How Neural Networks Learn

Anna
Picture a ball dropping through a Plinko board. It bounces unpredictably from peg to peg, and you don’t know which exit it will reach. Now imagine adjusting the pegs so a ball, a cube, a prism, and even a toy dinosaur each land in a bin chosen for them.

Michael
We’d try a setting, send a shape through, compare its bin with the one we wanted, and adjust if we missed. Then we’d repeat that process with different shapes. That try, check, and adjust loop is a useful picture of learning: a trained neural network takes an input and produces an intended output.

Anna
A real network doesn’t have pegs or bouncing objects. It has numbers and connected calculations. But the core idea is similar: tune many settings so the network’s answers get closer to the answers we want.

Michael
Let’s start with the smallest possible example: one neuron controlling a sound-activated light. A microphone measures sound level as a number between zero and one, where zero means silence and one means very loud. The neuron turns that input into a score, and the light should be either off or on.

Anna
The neuron multiplies the sound level by a weight. That weight determines how strongly the sound affects the score. For this simple light, we round the score: anything below point five becomes zero, or off; point five and above becomes one, or on.

Michael
That rounding is just a finishing step chosen for this job. Different networks can use different output steps. Our goal here is to switch the light on at sound level point three or higher, and leave it off below point three.

Anna
Start with a weight of one, which passes the sound level straight through. At point four, we want the light on, but the score is point four, which rounds down to zero. We got the wrong answer, so the weight needs adjusting.

Michael
We can practice on several sound levels with known right answers. At point four, the light should be on, but it is off, so nudge the weight up. At point two, it should be off, and it is; a correct answer needs no nudge.

Anna
At point three six, it should be on, but it is still off, so nudge up again. At point two eight, it should be off, but the light turns on, so the neuron is too sensitive and we nudge the weight down. Repeating this loop eventually settles the weight at about one point six seven.

Michael
Then we test on fresh sound levels the neuron never practiced on. In the example, point one five is off, point three is on, point two nine is off, and point eight is on—all correct. We used a deliberately simple problem, because a formula could have found the answer directly.

Anna
Real networks may have millions or even billions of weights, and there isn’t one formula that solves all of them at once. Trying, checking, and adjusting can scale: the machine can update many settings through repeated learning. One input, one weight, one answer is a tiny piece of the whole machine, but it is the seed of the larger idea.

Michael
Most real problems have more than one input. An image, for example, can be represented by many numbers, one for each pixel’s brightness. A neuron gives each input its own weight, adds all the weighted values into a score, and produces one response, called its activation.

Anna
A neuron usually adds a bias too. Without a bias, if every input is zero, the weighted sum must be zero. But zero can still carry information: silence tells us something about a room, and a dark patch tells us something about a picture.

Michael
The bias is the neuron’s own starting number, so its activation can be nonzero even when all its inputs are zero. Training adjusts both weights and biases. Bias can also shift the decision point: instead of using a weight of one point six seven to scale the sound, we could keep the weight at one and add a bias of minus point three.

Anna
Then the score is the sound level minus point three, and the light turns on when that score is zero or greater. That reaches the same threshold in a different way. So our growing recipe is: weigh the inputs, add them, add a bias, and produce an activation.

Michael
One neuron makes a simple call, but real recognition needs many small decisions working together. We can put several neurons side by side, all reading the same inputs but using their own weights. A group of neurons like that forms a layer.

Anna
We can also stack layers, so a later layer reads the earlier layer’s answers. That creates decisions built on decisions. But simply stacking the weigh-and-add calculations won’t give us the power we need, because those calculations are linear.

Michael
A linear equation can draw a line, and stacking linear equations still gives another line. A layer may tilt, shift, or flip the line, but even a hundred linear layers cannot draw anything beyond a line. To represent more complex patterns, the network needs nonlinear steps.

Anna
A common nonlinear step is ReLU. It turns a total of zero or less into zero, so the neuron stays silent, but lets a positive total pass through unchanged. In symbols, the activation is the maximum of zero and the total.

Michael
That rule bends the calculation: it is flat at zero and then climbs. Put bent responses together and you can make shapes a line can’t represent—a V, then peaks and waves, and with more layers, much more complex outlines and patterns.

Anna
Different inputs can wake different combinations of neurons. Layers can also build useful pieces in stages: dots into edges, edges into a circle, a circle into an eye, and eyes into a face. A deeper network can reuse features learned in earlier stages instead of trying to list every possible face in one enormous layer.

Michael
That is why nearly every neuron in a modern network ends with a nonlinear rule, commonly ReLU, though there are other choices. Modern networks add other techniques too, but weigh, add, bias, and bend remain the heart of the basic recipe.

Anna
When designing a network, the input and output sizes may be fixed by the task, while the hidden layers are choices. We might use three neurons in one hidden layer, five neurons in a wider one, or several layers of three neurons each in a deeper one.

Michael
Several processing layers are what people mean by deep learning. Wider layers mean more neurons working from the same inputs; deeper networks mean more stages working from earlier results. Neither choice automatically gives better answers. We have to measure.

Anna
Let’s measure on handwritten digits. We want a network to identify a digit from zero through nine, even though people write the same digit in many different ways. The image in this example is twenty-eight by twenty-eight pixels, or seven hundred eighty-four squares.

Michael
Each pixel becomes a number for brightness between zero and one. So the network has seven hundred eighty-four inputs. The example design uses thirty-two neurons in its hidden layer, each with seven hundred eighty-four weights and a bias, and each produces a response number.

Anna
The output has ten neurons, one for each possible digit. Each reads the thirty-two hidden responses and produces a score. A softmax finishing step converts the ten scores into probabilities that add up to one, and the digit with the highest probability is the network’s answer.

Michael
Training aims to raise the probability of the correct digit and lower the others by adjusting weights and biases. The machine repeats the try, check, adjust loop across the training examples. In the described run, it used sixty thousand images for five passes, and for one handwritten seven the seven’s probability climbed to ninety-eight point six percent.

Anna
A second network used two hidden layers, with sixty-four neurons and then thirty-two. On ten thousand images neither network had seen, the one-hidden-layer network was correct on ninety-five point five eight percent, or nine thousand five hundred fifty-eight images. The two-hidden-layer network reached ninety-six point three five percent, seventy-seven more.

Michael
The second network had about twice as many parameters—the weights and biases that can be adjusted. It won in that experiment, but it was both wider and deeper, so we can’t credit depth alone. One result on one task doesn’t mean bigger networks always win.

Anna
Now let’s zoom in on how a network learns. To see every number, imagine a tiny, untrained digit network with just three input numbers and two hidden neurons, still producing ten probabilities. For one example whose correct answer is seven, it happens to predict three.

Michael
Before training, weights and biases are starting values, commonly chosen at random, so the initial prediction has no meaningful reason behind it. In this example the network gives the correct digit seven only about nine percent probability. To improve it, we need a number measuring how wrong it is: the loss.

Anna
A good loss is zero when the correct answer gets all the probability, and grows as the correct answer’s probability shrinks. A common choice for this classification task is cross-entropy: the negative logarithm of the probability assigned to the correct answer.

Michael
That curve rises slowly at first and then sharply as the probability approaches zero, which gives the behavior we want. With seven at about nine percent, the example’s loss is two point three seven. Lower cross-entropy means the network’s output is closer to the expected answer.

Anna
How can we tell which way to change a weight? We could nudge it slightly and see what happens. In the example, raising one weight by point zero five changes a neuron by point zero four, shifts the seven’s score by point zero one six, and lowers the loss by point zero zero six five.

Michael
That experiment tells us both direction and strength: raising the weight helps, and we can measure how much the loss changes. Together, direction and strength are its slope, or derivative. We need that information for every weight and bias.

Anna
Changing each dial separately would be far too slow. The digit network had twenty-five thousand four hundred fifty adjustable numbers, and training involved two thousand three hundred forty-five updates. Testing one number at a time would require nearly sixty million full reruns, and modern networks may have billions of weights.

Michael
Backpropagation solves that by calculating all the slopes in one backward pass. The basic ingredient is a derivative: it tells us how much one quantity changes when another changes. For a neuron, output equals weight times input plus bias.

Anna
If the input is five, the weight is two, and the bias is one, the output is eleven. Raise the weight by one and the output becomes sixteen, a change of five—the input. So the output’s derivative with respect to the weight is the input.

Michael
Raise the bias from one to two and the output rises by one, so its derivative is one. Raise the input from five to six and the output rises by two, so its derivative with respect to the input is the weight. Those simple local derivatives are what backpropagation combines.

Anna
Imagine two neurons in a row. The first computes weight one times input one plus bias one, producing an intermediate value. The second takes that value, applies its own weight and bias, and produces the answer; a loss then scores that answer against the target.

Michael
We want the loss slope for each weight and bias. Start with the quantity directly connected to the loss: the final output. In the example, differentiating the loss gives a slope of plus two with respect to that output.

Anna
Then use the chain rule to find the slope for the second neuron’s weight: multiply the loss’s slope with respect to the output by the output’s local derivative with respect to that weight. The example gives fourteen. The same process gives a slope for the second bias, and a slope of four for the intermediate value.

Michael
Carry that intermediate slope backward to the first neuron. Multiply it by the first neuron’s local derivative with respect to its weight, and the weight’s loss slope is eight. Crucially, the carried slope already includes the downstream effects, so we don’t need to redo the second neuron’s calculation.

Anna
At each step, backpropagation does two things: multiply the slope that arrived by a local derivative, then keep the slopes for the current neuron’s weights and bias while passing the input slope backward. In a real network, a neuron may feed many neurons in the next layer, so it collects and adds the slopes coming back from all of them.

Michael
Working layer by layer from the output to the input gives every weight and bias its slope in a single backward pass. The whole list of slopes is called the gradient. Backpropagation measures how the loss responds to the settings; it doesn’t change them or tell a neuron what its output should be.

Anna
The adjustment step is gradient descent. It moves all the weights and biases at once in the direction that reduces loss, with the size of each move based on its slope. A large slope calls for a larger move than a tiny slope, but the moves must be scaled.

Michael
A slope measures sensitivity while every other setting is held fixed. If all the other settings move only a little, that measurement remains a useful guide. If they move a lot, the network changes so much that the old slope may no longer describe what happens.

Anna
But steps that are too tiny make learning slow and may leave the network stuck in a shallow dip, while very large steps can overshoot. A shared setting called the learning rate controls the step size: each move is the slope scaled by that rate.

Michael
In the example, the learning rate is point two, so each weight moves by one fifth of its slope. There isn’t a formula here that tells us the right rate; we try, check, and adjust. One step lowers the loss from two point three seven to two point zero eight, although the answer is still three.

Anna
A step that helps one training example may hurt another. So instead of updating after just one example, we can calculate the gradients for a group, average their suggested changes, and make one shared update. That group is a batch.

Michael
We work through batches until every training example has been used once. That complete pass is an epoch. In the digit experiment, each epoch involved hundreds of updates, and the loss eased down pass by pass.

Anna
The learning rate matters for batches too. In the two-example demonstration, a rate of point zero five barely reduced the loss, from two point zero zero to one point nine seven. A rate of two reduced it to point nine two, while a rate of twenty made it explode to eight point nine nine.

Michael
That huge step flew past what the measured slopes could guide. In practice, training means appropriately sized steps, repeated across batches and epochs. But there’s a danger: the network can improve on the examples it practiced on while getting worse on new ones.

Anna
That is overfitting: instead of learning the general pattern, the network starts memorizing the training images. We keep separate data for different jobs. Training data updates the network; validation data is never used for training and helps us check progress; test data is saved for a final evaluation.

Michael
In a deliberately small experiment with only about a thousand training images, a wide network’s training loss kept falling while its validation loss turned around and got worse. Validation showed that the best checkpoint was at epoch ten. Evaluated once on the test set, that checkpoint got eighty-eight point eight three percent correct.

Anna
That practice is called early stopping: save checkpoints during training and use the one the validation set liked best, rather than assuming the final one is best. More training isn’t always better. More data is a strong way to reduce memorization: the related network trained on sixty thousand images reached ninety-five point five eight percent.

Michael
Other tools can help too. We can choose a network shape that better fits the data, penalize extreme weights, randomly silence neurons during practice, or stretch, shift, and tilt training images to create extra variety. The aim is always to learn the pattern, not memorize the pages—and to judge success on data the network hasn’t seen.

Anna
There’s one more reason these networks can run fast: the arithmetic has a grid structure. Put the connections’ weights into a matrix, with a row for each input and a column for each neuron. For one neuron, multiply its inputs by the weights in its column and add them; for the whole layer, do that for every column.

Michael
That operation is matrix multiplication. The bias and nonlinear activation still follow for each neuron; the matrix does the weigh-and-add. The digit network’s first layer has a matrix with seven hundred eighty-four rows and thirty-two columns.

Anna
For a batch, stack examples as rows and one matrix operation can calculate weighted sums for every example and neuron at once. Each result cell involves its own multiply-and-add, so many can be calculated in parallel. Layers still depend on earlier layers, though: the next layer has to wait for the previous one’s answers.

Michael
That parallel structure suits graphics processors, or GPUs, which are built to carry out many similar calculations at once. CPUs are designed to handle a broader range of tasks, while GPUs can be a strong fit for large grids of neural-network calculations.

Anna
Modern language models have billions of weights, and their training follows the same broad loop—try, check, adjust—using gradient descent at enormous scale. Training can take days or weeks on thousands of processors, with architectures more elaborate than our small example.

Michael
Language models also need numbers for their inputs, just as image networks need pixel brightnesses. Text is split into tokens: whole words, word pieces, or punctuation, depending on the tokenizer. Each token has a learned list of numbers, tuned during training along with the weights and biases.

Anna
The model uses the earlier tokens as context, scores the tokens it could produce next, and uses softmax to turn those scores into probabilities. It can choose the highest-probability token or sample a less likely one, add it to the text, and repeat the process.

Michael
Training works in a similar way: show real text, ask the model to predict the next token, compare its probability for the real next token with the truth, calculate the loss, backpropagate the slopes, and update the weights, biases, and token numbers.

Anna
Next-token prediction isn’t the only task: networks can also be trained to translate, summarize, or answer questions. Modern language models use additional machinery, especially attention, which helps decide which earlier words matter most. But the basic bones are still predict, check, and nudge.

Michael
And that takes us back to the Plinko board. Try a setting, send a shape through, compare its landing bin with the goal, and adjust. Each peg is like a weight; each adjustment responds to error. The board doesn’t change itself—the training process measures and changes its settings.

Anna
The same backbone supports digit recognition, finding objects in pictures, speech, language, and systems that combine several tasks. A neural network is simple math, stacked into layers, bent by nonlinear steps, and tuned through repeated adjustments.

Michael
Not perfect on every answer, and not guaranteed to improve forever—but, with learning and careful measurement, better and better. That is the central idea: predict, check, and adjust.

Ask anything
We've read this whole study set, so questions about it get the best answers.