In the first Foundations post we built a tiny network, ran it forward, and used backpropagation to find how much each weight affected the output. That left two questions open. Affected what, exactly? And once we know, what do we do with it?
The answer to the first is the loss: one number that says how wrong the network is. The answer to the second is gradient descent: take a small step in the direction that makes the loss smaller, then repeat. Almost every model you have heard of, from image classifiers to the speech models in Pocket TTS, is trained this way.
Why we need one number
Imagine teaching a child to throw a ball into a bucket. After each throw you could say many things: “too far”, “a bit left”, “nice arc”. But if you had to give a single score, you would probably say how far from the bucket the ball landed. One number. Smaller is better.
A network is the same. It makes thousands of predictions, each a little right or wrong. To improve it, we need to squash all of that into one number called the loss (also called the cost or the objective). Why one? Because “better” only makes sense when you can compare. With one number, every change to the weights is either an improvement (loss went down) or not.
The loss has two jobs:
- Score. It says how bad the current predictions are.
- Steer. It must change smoothly as the weights change, so that we can ask “if I nudge this weight a little, does the loss go up or down?” That question is the gradient, and backpropagation from post 1 is how we answer it.
Choosing a loss is choosing what “wrong” means. The next two sections show that different choices really do lead to different answers.
Losses for numbers: squared and absolute error
Suppose the network predicts a number, like tomorrow’s temperature or the price of a house. We call the true answer the target and the network’s guess the prediction (read “y hat”).
Mean squared error
In one sentence: square each miss, then average.
- is how many examples we have.
- counts through them, from 1 to .
- is the prediction for example ; is its target.
- is the miss (positive if we guessed too high).
- means “add up”; turns the sum into an average.
MSE stands for mean squared error. Everyday picture: a fine that grows fast. Miss by 1, pay 1. Miss by 2, pay 4. Miss by 10, pay 100. Big mistakes are punished far more than small ones.
Worked example. Targets ; predictions . Misses are and . Squares are and . Average: 5.
If the second prediction moved from 2 to 4, its miss would shrink to , its square to , and the MSE to 1. Fixing the big miss helped a lot, because it was squared.
Mean absolute error
In one sentence: drop the sign of each miss, then average.
The bars mean “absolute value”: throw away the minus sign. MAE stands for mean absolute error. Everyday picture: a flat fee per unit of distance. Miss by 10, pay 10.
Same example: , , average 2.
Outliers: where the two disagree
Here is the clearest way to see the difference. Suppose the network is very simple: it must give the same guess for every example. The targets are . The 15 is an outlier, a value far from the rest (maybe a typo).
- The that makes MSE smallest is the mean: 6. The outlier drags the guess far from where most of the data sits.
- The that makes MAE smallest is the median, the middle value. With four values any from 3 to 4 ties; take 3.5. The outlier barely matters.
Check at : squared misses are , MSE 27.5. At : squared misses , MSE 33.75, worse, so MSE prefers 6. For MAE, at the misses are , MAE 3.5; at they are , MAE 4.5, so MAE prefers 3.5.
Neither is “right”. MSE says “never be very wrong about anything”; MAE says “be right about most things”. If the 15 is a typo, MAE is the safer choice. If it is real and costly to miss, MSE is.
The steering job matters too. The slope of the squared miss with respect to is : big misses push hard, small misses push gently, so steps naturally shrink near the answer. The slope of is just or : the same push whether you are off by 0.01 or by 100. We will see why that matters when we take steps.
Losses for choices: probabilities and cross-entropy
Many tasks are not “predict a number” but “pick one of several options”: is this picture a cat, a dog or a bird? This is called classification, and each option is a class.
Turning scores into probabilities with softmax
The last layer of a classifier outputs one raw score per class. These scores are called logits. They can be any number, negative or positive, and they do not add up to anything in particular. We want probabilities instead: numbers between 0 and 1 that add up to 1.
In one sentence: softmax makes every score positive with , then divides by the total so they add to 1.
- is the number of classes.
- is the logit for class .
- ; is always positive and grows fast as grows.
- The bottom adds up for every class, so the ‘s sum to 1.
- is the probability the network gives class .
Everyday picture: sharing sweets. Each class gets a pile of sweets whose size is , then we report each pile as a share of all the sweets. A score one higher means a pile about 2.7 times bigger.
Worked example. Cat, dog, bird with logits .
- , , .
- Total: .
- 0.665, 0.245, 0.090.
- Check: .
What changes if a number changes? Add the same amount to every logit, say , and the probabilities do not move at all: every pile is multiplied by the same . Only the differences between logits matter. Widen the gaps, say , and the top class takes almost everything: .
The name “softmax” comes from Bridle (1990): it is a soft version of “pick the maximum”.
Surprise, and why the loss is minus log
Now the true answer is “cat”. How should we score the probabilities ?
Think about surprise. If you were sure it would rain () and it rains, you are not surprised at all. If you gave rain a 1 in 100 chance and it rains, you are very surprised. A good measure of surprise should:
- be 0 when ;
- grow without limit as falls towards 0;
- add up for independent events: the surprise of two coin flips both landing heads should be twice the surprise of one.
The one function that does all three is . (The third rule is the key one: , so probabilities that multiply become surprises that add.) This idea comes from Claude Shannon’s 1948 paper that founded information theory. We use the natural log, .
| Probability given to what happened | Surprise |
|---|---|
| 1 | 0 |
| 0.5 | 0.693 |
| 0.1 | 2.303 |
| 0.01 | 4.605 |
Each time the probability shrinks by a factor of 10, the surprise grows by the same 2.303.
Cross-entropy loss is simply: the surprise the network feels at the right answer.
- is 1 for the true class and 0 for every other class. This list of one 1 and many 0s is called a one-hot vector.
- Because all the other are 0, the sum keeps only one term: .
Worked example. True class cat, : loss 0.408. If the true class were bird, : loss 2.408. Confidently wrong costs a lot.
Why not just use squared error on the probabilities? You could, but cross-entropy punishes confident mistakes much harder (it goes to infinity as , while squared error tops out at 1), and, as the next part shows, it gives a beautifully simple gradient.
The neat gradient: p minus y
We want to know: if I nudge logit up a little, how does the loss change? Working through the chain rule from post 1 for softmax followed by cross-entropy, almost everything cancels and leaves:
- reads “how much the loss changes per unit nudge of ”.
- is what the network said; is what it should have said.
In plain words: the push on each logit is “what you predicted minus what was true”.
Worked example with , true class cat, :
- cat: −0.335 (negative slope, so increasing the cat logit lowers the loss);
- dog: 0.245 (lower the dog logit);
- bird: 0.090 (lower the bird logit, a little).
The three numbers add to zero. And notice: when the network is already right and sure ( near 1), every push is near 0, so it stops changing what already works.
Here is the short reason, for two classes. With logits , . If class 1 is true, . The slope of with respect to is ; the slope of is . Together: . The same two lines work for any number of classes.
The loss landscape
Now put the network’s weights on the map. Every possible setting of the weights gives one loss value. If the network had just two weights, we could draw this as a hilly terrain: east-west is weight 1, north-south is weight 2, and the height of the ground is the loss. Training means finding a low valley.
Real networks have millions of weights, so the terrain has millions of directions we cannot draw. But the picture still works: at any spot, the ground slopes some way, and we want to walk downhill.
The catch is that we are walking in thick fog. We cannot see the whole terrain. We can only feel the slope right under our feet. That is exactly what backpropagation gives us.
The gradient: the steepest-uphill arrow
In one sentence: the gradient lists, for each weight, how fast the loss rises when you nudge that weight.
- is the list of all weights; are single weights.
- (read “nabla” or “grad”) means “the gradient of”.
- Each entry is a slope in one direction, holding the others fixed.
Taken together, these slopes form an arrow that points in the steepest uphill direction, and its length says how steep. So to go down, we walk the opposite way.
Worked example. On the bowl , the slopes are and . At the point the gradient is : the ground rises just as fast eastward as northward. At it is : much steeper northward, because the term makes the bowl narrow in that direction.
Gradient descent in one dimension
In one sentence: move each weight a small step against its slope, then repeat.
- is the weight now; is the weight after one step.
- is the slope of the loss at .
- (the Greek letter eta) is the learning rate: how big a step to take per unit of slope.
- The minus sign turns “uphill” into “downhill”.
Everyday picture: walking down a hill in fog by feel. Where it is steep, you take a bigger stride; where it is nearly flat, a small shuffle.
The method is old: Augustin-Louis Cauchy described it in 1847.
Worked example. The loss is , a bowl with its bottom at . Its slope is . Start at with .
| Step | Loss | Slope | Update | |
|---|---|---|---|---|
| 0 | 0 | 9 | −6 | |
| 1 | 0.6 | 5.76 | −4.8 | |
| 2 | 1.08 | 3.6864 | −3.84 | |
| 3 | 1.464 | 2.3593 | −3.072 |
Look at the distance to the bottom: Each step multiplies it by 0.8. Here is why. The update is , so the new distance is . In general, on this bowl, every step multiplies the distance by . Keep that number in mind; the whole learning-rate section follows from it.
Also notice the steps shrink on their own (0.6, then 0.48, then 0.384) even though is fixed, because the slope shrinks near the bottom. With absolute error, whose slope is always , the step would stay forever and hop back and forth across the bottom.
Gradient descent in two dimensions
With two weights we do exactly the same thing, once per weight, at the same time.
Take , start at , . The slopes are .
- Step 1: gradient . New point . Loss 10.4 (from 20).
- Step 2: gradient . New point . Loss 6.56.
- Step 3: gradient . New point . Loss 4.1946.
Per step, is multiplied by and by . The steep direction is fixed almost at once; the gentle direction crawls. That is the typical shape of real training: quick progress at first, then a long slow slide along a valley floor.
Now try . The factors become and . The minus sign means flips side each step: . The path zigzags across the narrow valley. Push to and ‘s factor is exactly : it bounces between and forever. The narrowest direction sets the speed limit for every direction.
The learning rate
The learning rate is the most important number you choose in training. On our 1-D bowl , each step multiplies the distance to the bottom by . That single factor sorts every learning rate into one of five cases. Start at (distance 3) each time.
| Factor | after steps 1, 2, 3 | What happens | |
|---|---|---|---|
| 0.01 | 0.98 | 0.06, 0.1188, 0.1764 | Too small: crawls; after 100 steps still 0.4 away |
| 0.4 | 0.2 | 2.4, 2.88, 2.976 | Just right: within 0.024 after 3 steps |
| 0.5 | 0 | 3, 3, 3 | Lands exactly in one step (only on a perfect bowl) |
| 1.0 | −1 | 6, 0, 6 | Bounces forever between 0 and 6 |
| 1.1 | −1.2 | 6.6, −1.32, 8.184 | Too large: diverges, distance 3, 3.6, 4.32, 5.18, … |
Check the “after 100 steps” claim: 0.40.
And the divergence: at , losses go , each step 1.44 times worse (). In real training this is the dreaded loss that shoots to “NaN” (not a number) after a few hundred steps.
The rule: as long as the factor’s size is below 1, that is here, we get closer. In general the limit depends on how curved the steepest direction is, which is why the 2-D bowl’s narrow direction set a tighter limit of . Real networks do not tell you their curvature, so people try a few rates and watch the loss. Post 3 covers methods (momentum, Adam, schedules) that make this choice much less fragile.
Full-batch, stochastic and mini-batch
So far the loss was one formula. In real training it is an average over the training data: the loss on example 1, plus on example 2, and so on, divided by . The true gradient is therefore an average of per-example gradients, and computing it means running every example through the network. With a million examples, that is a million forward and backward passes for one step.
There are three ways to handle this.
- Full-batch gradient descent: use every example for each step. Exact, but slow.
- Stochastic gradient descent (SGD): use one random example per step. “Stochastic” means “random”. Each step is cheap but noisy, since one example’s opinion is not everyone’s. This idea goes back to Robbins and Monro (1951).
- Mini-batch gradient descent: use a small random group, say 32 or 256 examples, per step. This is what nearly everyone does; people often still call it SGD.
A toy you can follow. We want one point that is close to four data points: A , B , C , D . The loss for one point is its squared distance ; the full loss is the average over four. The best is the centre, , where the loss is 4 (every point is 2 away, squared is 4). It cannot reach 0, because no single spot sits on all four points.
The gradient of one point’s loss is . With , the update simply moves halfway towards the target. That makes every step easy:
- Full batch (target is the average of all four, ), from : , , . A straight line.
- Stochastic, points in the shuffled order B, D, A, C: halfway to B gives ; halfway to D gives ; halfway to A gives ; halfway to C gives . A wobbly path that ends near the centre after one pass through the data, using the same total work as one full-batch step.
- Mini-batch of two, batches B and A (average ) then D and C (average ): , then . Less wobbly than one-at-a-time.
The noise is not only a cost. It can shake the weights out of small dents in the terrain, and it means the weights never settle perfectly; with a fixed learning rate, SGD keeps jittering around the bottom. That is one reason learning rates are usually lowered as training goes on.
Valleys, saddles and plateaus
Our bowls had one bottom. Real loss landscapes are messier.
- A local minimum is a dip that is lower than everything nearby but not the lowest point overall. Gradient descent, feeling only the slope underfoot, stops there happily.
- A saddle point is flat in every direction but is a dip in some directions and a hump in others, like the middle of a horse’s saddle or a mountain pass. The gradient there is zero, so a ball placed exactly on it does not move.
- A plateau is a large, nearly flat region. The slope is tiny, so steps are tiny, and training looks stuck for a long time before it suddenly finds a slope again.
For big networks, saddles and plateaus are thought to be a bigger nuisance than bad local minima: Dauphin and colleagues (2014) argued that in very many dimensions, most zero-slope points are saddles, since it is unlikely that the surface curves upward in every one of millions of directions at once.
Worked saddle. : a valley along , a hump along . Slopes are . With , each step multiplies by and by .
- Start at : , , , … The ball slides into the saddle and stays. Exactly zero in means it never feels the downhill side.
- Start at : , , , , , … For several steps it looks stuck near the centre, losses barely changing (, , , , , ), then grows by half each step and the ball rolls away down the hump. By step 12, .
That tiny 0.01 is exactly what the noise from mini-batches provides for free.
Convexity in simple terms
Some landscapes are guaranteed to be easy. A function is convex if, whenever you stretch a straight string between any two points on its surface, the string never dips below the surface. A bowl is convex; an egg carton is not.
Why care? On a convex landscape, every dip is the lowest point. There are no false valleys and no saddles, so gradient descent with a small enough learning rate always reaches the bottom.
Quick check with the string. On , take (height 9) and (height 9). The string’s midpoint is at height 9 above , and the curve there is at 0. String above curve: fine. On , along the direction take and (both height ). The string’s middle is at , but the surface at is at , above the string. Not convex.
Mean squared error on a model that is a straight line in its weights (linear regression) is convex, and so is cross-entropy on a single softmax layer (logistic regression). As soon as we stack layers with nonlinear activations, as in post 1, convexity is lost. Deep learning trains on non-convex landscapes anyway, and it works far better in practice than the theory promises.
What to remember
- The loss is one number that scores the whole network. MSE punishes big misses hard and is pulled by outliers (it chooses the mean); MAE treats every unit of miss the same (it chooses the median).
- Softmax turns logits into probabilities; only differences between logits matter. Cross-entropy is the surprise at the right answer, and its gradient with respect to the logits is simply .
- The gradient points steepest uphill. Gradient descent steps against it: .
- On a simple bowl, each step multiplies the distance to the bottom by : tiny crawls, a factor between −1 and 1 converges, a factor beyond −1 diverges. The steepest direction sets the limit.
- Mini-batches trade exact gradients for many cheap, noisy steps. The noise helps escape saddles and flat plateaus.
- Convex landscapes have one bottom; deep networks are not convex, and train well anyway.
Try it yourself
- Figure 1. Set target 3 and prediction 5: squared error shows 4 with slope 4, absolute error 2 with slope 1. Set the probability of the true class to 0.1: cross-entropy shows 2.303. Switch to “Outlier data” and set c to 6: MSE 27.5, MAE 4.5; at 3.5: MSE 33.75, MAE 3.5.
- Figure 2. Set logits cat 2, dog 1, bird 0, true class cat. You should see probabilities 0.665, 0.245, 0.090, loss 0.408, gradient −0.335, 0.245, 0.090. Now set all three logits 10 higher (12, 11, 10): nothing changes.
- Figure 3. Start 0, learning rate 0.1. After three steps should read 1.464 and the loss 2.359.
- Figure 4. Click (4, 1), learning rate 0.1. After three steps: (2.048, 0.008), loss 4.194. Change to 0.2 and confirm the zigzag , .
- Figure 5. After three steps: 0.1764, 2.976, 8.184.
- Figure 6. After four steps: full batch at (1.875, 1.875), stochastic at (1.75, 2.375), mini-batch (two steps per pass, so after four steps it has seen the data twice) at (1.5625, 2.1875).
- Figure 7. After five steps from the ball is at (0.0625, 0.0759); the other is at (0.0625, 0).
Sources
- A.-L. Cauchy (1847). “Méthode générale pour la résolution des systèmes d’équations simultanées.” Comptes Rendus de l’Académie des Sciences, 25, 536–538. The first description of gradient descent.
- H. Robbins and S. Monro (1951). “A Stochastic Approximation Method.” The Annals of Mathematical Statistics, 22(3), 400–407. The root of stochastic gradient descent.
- C. E. Shannon (1948). “A Mathematical Theory of Communication.” Bell System Technical Journal, 27, 379–423 and 623–656. Information, surprise and entropy.
- J. S. Bridle (1990). “Probabilistic Interpretation of Feedforward Classification Network Outputs, with Relationships to Statistical Pattern Recognition.” In Neurocomputing (NATO ASI Series F68), Springer, 227–236. The name “softmax”.
- Y. Dauphin, R. Pascanu, C. Gulcehre, K. Cho, S. Ganguli and Y. Bengio (2014). “Identifying and attacking the saddle point problem in high-dimensional non-convex optimization.” NeurIPS 2014. arXiv:1406.2572.
- I. Goodfellow, Y. Bengio and A. Courville (2016). Deep Learning, MIT Press, chapters 4, 6.2 and 8. Textbook treatment of all of the above.