NeuronCanvas
Neural Networks
Training a Network

Loss Functions

How a network measures how wrong a prediction is, covering squared error and cross-entropy loss.

Step 01 - The break

A correct network with a terrible score

This network is right about the answer and its score is terrible.

the network said 0.574 the right answer was 1 it was RIGHT
the network said 0.310 the right answer was 0 it was RIGHT
score for the first one 0.554355
score for the second one 0.371101
the one it was more confident about scored better.
the one it barely got right scored worst of the batch, worse
than an example it got WRONG less confidently.

Something is grading this network, and it is not grading correctness. This page is about what it is grading instead.

Step 02 - Before this page
Step 03 - The stage

Drag the prediction. Then switch the loss with the three buttons and drag it again, without changing anything else. The three losses disagree about the same mistake, and by more than you would guess.

Preview frame from the "Loss Functions" animation0:13

What this animation shows

A red "prediction" dot sits far from a green target star while a large loss number reads 9.00 at the top of the frame. As the prediction glides toward the target, the loss number counts down and shifts from red toward green, reaching a small number as the dot lands on the target and turns green itself. This visualizes exactly what a loss function does: measure how wrong a prediction is, as a single shrinking number.

correct answerprediction
loss = 0.5 × (prediction − answer)² = 0.5 × (0.201 = 0.320
Correct answer
Loss function

Drag the prediction toward the correct answer and watch the loss shrink toward zero.

A different loss for classification: cross-entropy

Here log always means the natural log (ln), not log base 10. Cross-entropy is defined as L = −[y·log(ŷ) + (1−y)·log(1−ŷ)]. Compare it against squared error for the same confidently wrong prediction - the network predicted ŷ=0.02 when the correct answer was y=1:

squared error = 0.5 × (0.02 − 1)² = 0.480
cross-entropy = −ln(0.02) = 3.912

Cross-entropy scores this exact same mistake as roughly 8× worse. Why that difference matters for how a network learns, not just how it's scored, depends on backpropagation, which we haven't covered yet. We'll come back to this exact number on the output activations page, once the machinery for computing it is in place.

Number line showing the gap between the network's prediction and the correct answer.
Step 04 - One question first

The network predicts 0.02 when the right answer is 1. Squared error scores that mistake at 0.4802. What does cross-entropy score it at?

  • aAbout the same, near 0.5
  • bRoughly double, near 1
  • cRoughly eight times more, near 4
  • dInfinite - it was almost completely wrong
Commit to a guess, then open this

3.9120, about eight times more. And there is no ceiling: the same prediction at 0.002 instead of 0.02 scores 6.2146, and at 0.0002 it scores 8.5172. Squared error, over that same range, only ever climbs from 0.4802 to 0.5000. One of these two losses can go on punishing a confident mistake forever and one of them cannot, and that difference is the whole page.

Step 05 - Plain explanation

Everything a network learns, it learns from one number. The network makes a guess, the loss function compares that guess to the right answer, and returns a single number saying how bad the guess was. Small number, good guess. Large number, bad guess. That is the entire job.

It matters that it is one number and not a description of what went wrong. The network is never told “your answer was too high” or “you missed the cat”. It is told “0.36”, and everything else - which weight to change, in which direction, by how much - gets worked out afterwards from that single number, by the machinery the next two pages are about. Choosing a loss function is therefore choosing what the network will consider a mistake, and how strongly it will care about mistakes of different sizes. Two losses looking at the same wrong answer can disagree by a factor of eight about how wrong it was: a prediction of 0.02 when the answer was 1 scores 0.4802 under squared error and 3.9120 under cross-entropy, and both of those numbers appear below. The network trains differently as a result. This page is about those disagreements.

One piece of vocabulary before anything else, because it is used loosely everywhere else: loss usually means the score for a single example, and cost usually means the average loss over a batch of examples. Most people, including the rest of this site, use “loss” for both. Where the distinction matters below, it is spelled out.

Speeding fines

Squared error is a fine that quadruples every time you double the speed you were over by: 10 over costs a hundred, 20 over costs four hundred. MAE is a flat penalty per unit over, no matter how fast. Cross-entropy is a different court entirely: it fines you for how confidently you swore you were under the limit. Swear blind you were doing 30 in a 30 and get clocked at 60, and the fine is enormous. Say you might have been a bit over, and it is small.

Where it breaks downFines are capped by law and cross-entropy is not - it really does go to infinity.

Step 06 - The depth ladder
WordsWhat one number has to carryTwo losses, in words, before either has a symbol.Rung 01

Squared error is the default loss for regression, meaning any task where the network has to produce a number rather than pick a category: a house price, a temperature, a position. You take the gap between the prediction and the right answer, and you square it.

Squaring does two things at once, and both are on purpose. It makes the score positive, so being 2 too high and 2 too low both cost the same, and it makes the cost grow faster than the mistake does. Being twice as wrong is four times as expensive. That second property is what makes squared error push hard on the worst predictions first.

Squared error is the wrong tool the moment the network’s job changes from “produce a number” to “pick a category”. For classification the network outputs a probability, a number between 0 and 1 saying how confident it is, and what you want to punish is not distance, it is misplaced confidence. Being 90% sure of the wrong answer should hurt much more than being 55% sure of the wrong answer, and it should hurt more than twice as much.

Cross-entropy does exactly that, and the mechanism is one idea: score the probability the network assigned to the answer that actually happened, and take the negative natural log of it. That is all −ln(ŷ) means. The log is there because it has one useful shape: it is 0 when the probability is 1, small when the probability is high, and it grows without limit as the probability approaches 0. There is no ceiling on the punishment for confidently ruling out the truth.

NumbersEvery loss on this page, workedSix losses, every one of them arithmetic you can check by hand.Rung 02

Here is a full trace, batch and all. The network is as small as it can be while still having something to learn: one input x, one weight w = 0.60, one bias b = 0.10, and no activation function on the output, so the prediction is just ŷ = w·x + b. The weight is positive, so it would be drawn blue on any diagram on this site. Three training examples go through it.

Worked example - forward pass, three examples
x=1.0 yhat = 0.60 * 1.0 + 0.10 = 0.7000 y = 1.0 e = -0.3000 e^2 = 0.0900
x=2.0 yhat = 0.60 * 2.0 + 0.10 = 1.3000 y = 1.6 e = -0.3000 e^2 = 0.0900
x=3.0 yhat = 0.60 * 3.0 + 0.10 = 1.9000 y = 2.5 e = -0.6000 e^2 = 0.3600
sum of squared errors = 0.0900 + 0.0900 + 0.3600 = 0.5400
MSE = 0.5400 / 3 = 0.1800
half-MSE = 0.5400 / 6 = 0.0900 (this site’s 1/2 convention)

All three errors are negative, so the network is guessing low on every example. Look at which example the loss is mostly made of. The third one contributed 0.3600 of the 0.5400 total, two thirds of the whole score, off an error only twice the size of the other two. That is the squaring at work, and it is the reason this loss will end up caring about that example more than about the other two combined.

What happens next is that this one number, 0.0900, gets turned into a change to w and a change to b. That is not a property of squared error, it is the same procedure for every loss on this page, and it is the subject of the next page, which picks up this exact kind of network and walks the arithmetic.

−ln(p) at a few probabilities
p = 1.00 -> -ln(1.00) = 0.000000 perfectly confident and right, no cost
p = 0.90 -> -ln(0.90) = 0.105361
p = 0.50 -> -ln(0.50) = 0.693147 a coin flip
p = 0.10 -> -ln(0.10) = 2.302585
p = 0.02 -> -ln(0.02) = 3.912023 confidently wrong, and climbing
p = 0.00 -> -ln(0) = infinity (real code clamps p away from 0 and 1)

That p = 0.02 row is the factor of eight from the top of this page. Squared error scores the same prediction at 0.4802; cross-entropy scores it at 3.9120, 8.15 times higher, for being confidently wrong about the same example.

Here is the full path from a raw network score to a loss, which is the part that usually gets skipped. The network’s last layer produces a raw, unbounded score called a logit, written z. Sigmoid squashes that logit into a probability. Cross-entropy scores the probability.

Worked example - logit to loss, three examples
z = +1.20, y = 1 p = 1/(1+e^-1.20) = 0.768525 L = -ln(0.768525) = 0.263282
z = -0.80, y = 0 p = 1/(1+e^+0.80) = 0.310026 L = -ln(1 - 0.310026) = 0.371101
z = +0.30, y = 1 p = 1/(1+e^-0.30) = 0.574443 L = -ln(0.574443) = 0.554355
mean cross-entropy over the batch = (0.263282 + 0.371101 + 0.554355) / 3
= 0.396246

The third example is the interesting one. The network said 0.574, barely better than a coin flip, and got it “right” in the sense that 0.574 rounds to 1. Accuracy would score that as a win. Cross-entropy scores it as the worst of the three, worse than the example the network got wrong less confidently. Cross-entropy grades confidence, not correctness.

Now the outlier comparison. To see how far apart squared error and MAE are, compare each average against what the three well-behaved errors would have produced on their own. Three errors of 0.1 give a squared-error average of 0.01 and an MAE average of 0.1. Adding the single outlier takes those to 6.2575 and 1.325 respectively.

Worked example - what one outlier does to each average
three clean errors only, [0.1, 0.1, 0.1]
average squared error = 0.010000
average MAE = 0.100000
with the outlier, [0.1, 0.1, 0.1, 5.0]
average squared error = 6.257500 -> multiplied by 625.75x
average MAE = 1.325000 -> multiplied by 13.25x
the same single bad example inflates the squared-error average
625.75 / 13.25 = 47.2x harder than it inflates the MAE average

The two averages are not directly comparable to each other, one is in squared units and one is not, which is exactly why the comparison has to be made against each loss’s own baseline. Measured that way, one outlier out of four moves squared error roughly 47 times more than it moves MAE.

Mean absolute error itself is the plain distance between prediction and answer, |ŷ−y|. Take the same wrong prediction the demo above used, ŷ=0.02 and y=1:

MAE = |0.02 - 1| = 0.98
squared error = 0.5 * (0.02 - 1)^2 = 0.4802

That squared-error figure uses this page’s ½(ŷ−y)² convention. If you check the arithmetic yourself with the plain (ŷ−y)² form instead, you’ll land on 0.9604 for this same example, not a mistake, just a different convention.

Huber loss behaves like squared error for small mistakes and like MAE for large ones. The switch happens at a threshold you choose, δ. Worked with δ=1.0:

Worked example
small error, e = 0.5 (|e| <= d, quadratic branch)
L = 0.5 * 0.5^2 = 0.125 (same as squared error here)
large error, e = 5.0 (|e| > d, linear branch)
L = 1 * (5 - 0.5) = 4.5
squared error alone: 0.5 * 5^2 = 12.5

Huber caps how much a single outlier can dominate training, while still behaving exactly like squared error close to the target. It’s the loss behind the original DQN reinforcement-learning agent, among other places you’ll run into it.

Hinge loss, for a label y ∈ {−1, +1} and a raw score f(x), asks a coarser question than any of the above: is your raw score on the correct side of the boundary, by a comfortable margin?

y=1, f(x)=1.5 (correct, comfortably past the margin)
1 - 1 * 1.5 = -0.5 -> loss = max(0, -0.5) = 0
y=1, f(x)=0.8 (correct, but inside the margin)
1 - 1 * 0.8 = 0.2 -> loss = 0.2
y=1, f(x)=-0.3 (wrong side of the boundary entirely)
1 - 1 * (-0.3) = 1.3 -> loss = 1.3
PictureThe same mistake, three losses, on the number lineOne diagram, three switch positions, and the moment two losses swap places.Rung 03

Set the prediction to 0.50 with the answer at +1 in Figure 02 and read the three losses off the switch: squared error 0.1250, MAE 0.5000, Huber 0.1250. Huber and squared error agree exactly, because an error of 0.5 is inside Huber’s δ=1 threshold and Huber is squared error in there. Now drag the prediction to −1.00, an error of 2.0: squared error 2.0000, MAE 2.0000, Huber 1.5000. The two that agreed have separated, and the two that disagreed have crossed.

The red bar on the number line is the same length in every one of those readings - the bar is the error, the number underneath is what each loss thinks that error is worth.

The widget’s own static cross-entropy panel is the fourth reading the number line cannot draw: 0.480 against 3.912 for the identical prediction of 0.02. There is no bar length that shows a difference of eight times, which is exactly why that panel is separate.

EquationThe formulas, and where each symbol came fromThe same six losses as rung 2, now in symbols, with every symbol named.Rung 04

The full binary form of cross-entropy, L = −[y·ln(ŷ) + (1−y)·ln(1−ŷ)], looks like two terms but only ever uses one of them. When the true label y is 1, (1−y) is 0 and the second term vanishes, leaving −ln(ŷ). When y is 0, the first term vanishes, leaving −ln(1−ŷ). It is an if-statement written as arithmetic, which matters because the training procedure needs a loss that is differentiable, meaning it has a well-defined slope everywhere rather than jumping between two branches.

Huber, both branches
e = yhat - y
L_d(e) = 0.5 * e^2 if |e| <= d
L_d(e) = d * (|e| - 0.5 * d) if |e| > d
Hinge
L = max(0, 1 - y * f(x))

MAE’s derivative is dL/dŷ = sign(ŷ−y): always exactly +1 or −1, no matter how far off the prediction is. Every mistake, big or small, pushes the weights by the same amount. Squared error’s derivative grows with the size of the mistake instead, so a huge outlier produces a huge gradient and one bad example can dominate an entire weight update.

General caseMore than two classes, and why cross-entropy is paired with sigmoidN classes instead of two, and the one fact that decides which loss you actually use.Rung 05

With more than two categories the network produces one logit per class, softmax turns the whole set of logits into probabilities that sum to 1, and cross-entropy scores the one belonging to the correct class. Nothing about the idea changes; there are just more numbers.

Two pieces of vocabulary arrive with them. A distribution here means a list of probabilities covering every possible outcome and adding up to exactly 1, so [0.659, 0.242, 0.099] is one. And one-hot is how the correct answer gets written in the same shape: one number per class, a 1 in the position of the right answer and 0 everywhere else. “The answer is class 1, out of three” becomes [1, 0, 0].

Worked example - 3 classes, true class is class 1
logits z = [2.0, 1.0, 0.1]
exponentiate: e^2.0 = 7.389056
e^1.0 = 2.718282
e^0.1 = 1.105171
sum = 11.212509
softmax p = [7.389056/11.212509, 2.718282/11.212509, 1.105171/11.212509]
= [0.659001, 0.242433, 0.098566] sums to 1.000000
one-hot target y = [1, 0, 0]
L = -ln(p_correct) = -ln(0.659001) = 0.417030
how hard each logit is pushed: p - y = [-0.340999, +0.242433, +0.098566]
these sum to exactly 0, which they must: softmax outputs are tied
together, so pushing one class up necessarily pushes the others down

If the true class had been class 3 instead, the same forward pass would have scored −ln(0.098566) = 2.317030 instead of 0.417030, five and a half times the loss, from identical logits. The loss depends on which answer was correct, not on how spread out the predictions were. Softmax gets its own page, Softmax for Multi-Class Output.

Now the reason cross-entropy is the standard partner for a sigmoid or softmax output, and squared error is not. The question to ask is: when the raw logit z moves a little, how much does the loss move? That quantity is written dL/dz, and for now it can be read exactly as those words - how much the loss changes when z changes - with no more machinery than that. Where such numbers come from, and what training does with them, are the next page’s subject. All that matters here is that a loss which barely moves when the network is badly wrong is a loss that will barely teach it anything.

Worked example - how hard the loss pushes on the raw logit z, target y = 1
cross-entropy: dL/dz = p - y (the whole thing, no extra factors)
half-squared: dL/dz = (p - y) * p * (1 - p) (carries sigmoid’s own slope)
z = -4.0 p = 0.017986 cross-entropy -0.982014 squared -0.01734502
z = 0.0 p = 0.500000 cross-entropy -0.500000 squared -0.12500000
z = +4.0 p = 0.982014 cross-entropy -0.017986 squared -0.00031769
at z = -4 (confidently wrong): cross-entropy’s push is 56.6x
larger than squared error’s, and that factor is exactly
1 / (p * (1-p)), sigmoid’s own slope

At z = −4 the network is confidently, badly wrong: it says 1.8% for something that is true. Cross-entropy responds with −0.982, almost the largest response it can give. Squared error responds with −0.0173, 56.6 times smaller, because it multiplies the error by sigmoid’s own slope, and sigmoid’s slope is nearly zero out there. Push the logit out to −8 and the gap widens to over 2,900 times; the further into the wrong answer the network goes, the quieter squared error gets. Squared error plus sigmoid produces almost no learning signal precisely when the network is most wrong. Cross-entropy’s p − y cancels that slope term away exactly. That cancellation is the whole reason the pairing exists, and it is derived properly on the output activations page.

A precision note: p − y is the response with respect to the logit, after sigmoid has been folded in. The response with respect to the probability itself, dL/dp, is (p − y) / (p(1−p)), which does blow up near 0 and 1. Frameworks combine the sigmoid and the cross-entropy into one operation (BCEWithLogitsLoss in PyTorch) so that the clean p − y form is what actually gets computed and the unstable intermediate never appears.

The last one is not really a loss you would pick off a menu, it is a way of understanding the one you already picked. KL-divergence answers “how much worse is my predicted distribution than the true one?”, using “distribution” in the sense defined above: a list of probabilities over every outcome, adding to 1. Cross-entropy answers “how expensive is my predicted distribution?”, and part of that expense is unavoidable: even a perfect prediction costs something, because the true distribution has its own built-in uncertainty. KL-divergence is cross-entropy with that unavoidable part subtracted off, so a perfect prediction scores exactly 0.

Cross-entropy decomposes as H(P,Q) = H(P) + D_KL(P‖Q), so minimizing cross-entropy and minimizing KL-divergence are the same optimization problem in practice - H(P), the true data’s own uncertainty, doesn’t depend on the model at all. Quick worked check: true distribution P=[0.7, 0.3], predicted Q=[0.5, 0.5] gives H(P)=0.610864 and H(P,Q)=−ln(0.5)=0.693147, so D_KL=0.082283, matching a direct KL calculation, 0.7×ln(1.4) + 0.3×ln(0.6) = 0.082283. You’ll see this term again if you go on to study generative models.

Six losses is a lot to hold at once. The choice is usually made by the shape of the output, not by taste:

Which loss for which job
regression, clean data ................ squared error (MSE)
regression, outliers present .......... Huber, or MAE if extreme
binary classification ................. binary cross-entropy + sigmoid
multi-class, one label per example .... categorical cross-entropy + softmax
multi-label, several labels at once ... binary cross-entropy, per class
support vector machines ............... hinge loss (not for networks)
comparing two distributions ........... KL-divergence

If you are unsure, the shape of your output layer has already decided for you. That is not a coincidence; the page on output activations is the other half of this one.

Step 07 - Why this and not that

Why this and not that

Why not just train on accuracy? That is what I actually care about.

Accuracy is a count, and counts do not have slopes. Move a prediction from 0.51 to 0.62 and the accuracy is identical, so there is nothing to tell the network which way to move next. Every loss on this page is chosen partly because it changes smoothly when the prediction changes slightly. Accuracy is what you report; the loss is what you train.

Why square the error rather than take the absolute value? Squaring feels arbitrary.

Two reasons, and one of them is not the one usually given. It makes the cost grow faster than the mistake, which is a real design choice, and it also makes the loss smooth at zero, where the absolute value has a corner. MAE’s slope is +1 on one side of the answer and −1 on the other and never anything in between, which is why it never settles down close to the target.

Why the natural log and not log base 10?

Base 10 works identically; every value is just divided by ln(10) = 2.302585, a constant, so it rescales every score without changing which prediction scores best. The natural log is the convention because its slope is exactly 1/p, with no constant hanging off it, which keeps the derivative clean.

Is cross-entropy just a fancier squared error?

No, and the dL/dz block in rung 5 is the reason. When the network is confidently wrong, squared error’s push on the raw score is 56.6 times weaker than cross-entropy’s, and past z = −8 it is over 2,900 times weaker. They are not the same loss with different constants; one of them stops teaching precisely when the network most needs teaching.

The page says ‘loss’ and my textbook says ‘cost’ and ‘objective’ and ‘criterion’.

Loss is normally one example, cost is normally the batch average, objective is whatever you are optimising including any regularization terms, and criterion is PyTorch’s variable name for the loss object. In practice everyone says loss for all four, including the rest of this site.

Name origins
Entropy
Borrowed from thermodynamics, where Clausius coined it in 1865 for the unusable part of a system’s energy. Shannon reused it in 1948 for the average surprise in a stream of messages, which is the sense every use on this page has.
Cross-
In cross-entropy: you are scoring one distribution using the code that would have been optimal for a different one. Your predictions are the code, the truth is the data, and the cross is where the two meet.
Hinge
The graph of max(0, 1 − y·f(x)) is a straight slope that stops dead at zero and stays flat. It looks like a hinge, and the flat part is where the loss no longer cares.
Huber
Peter Huber, the Swiss statistician who published it in 1964 in a paper about making estimates that survive contaminated data. It was designed for outliers before neural networks existed.
KL
Solomon Kullback and Richard Leibler, 1951. Their own word for it was “divergence” rather than “distance”, deliberately, because it is not symmetric: D(P‖Q) and D(Q‖P) are different numbers.
MSE, MAE
Mean squared error, mean absolute error. The only difference in the names is the only difference in the formulas.
Step 08 - Where people go wrong
correct answerprediction
loss = 0.5 × (prediction − answer)² = 0.5 × (0.201 = 0.320
Correct answer
Loss function

Drag the prediction toward the correct answer and watch the loss shrink toward zero.

A different loss for classification: cross-entropy

Here log always means the natural log (ln), not log base 10. Cross-entropy is defined as L = −[y·log(ŷ) + (1−y)·log(1−ŷ)]. Compare it against squared error for the same confidently wrong prediction - the network predicted ŷ=0.02 when the correct answer was y=1:

squared error = 0.5 × (0.02 − 1)² = 0.480
cross-entropy = −ln(0.02) = 3.912

Cross-entropy scores this exact same mistake as roughly 8× worse. Why that difference matters for how a network learns, not just how it's scored, depends on backpropagation, which we haven't covered yet. We'll come back to this exact number on the output activations page, once the machinery for computing it is in place.

Number line showing the gap between the network's prediction and the correct answer.
Step 09 - Practice
  1. 01
    Get the squared-error readout to exactly 0.0000 twice, using two different target settings.
    Hint

    The loss is zero only when the prediction equals the answer, and the answer can be either +1 or −1.

    Answer

    Prediction +1.00 with target +1, and prediction −1.00 with target −1. Both ends of the slider are reachable. The point is that zero loss is not a property of the prediction, it is a property of the pair.

  2. 02
    Find a prediction where squared error and MAE give the same number, and say why that particular value.
    Hint

    Squared error here is 0.5·e² and MAE is |e|. Set them equal.

    Answer

    0.5·e² = |e| when |e| = 2, so prediction −1.00 with target +1: both read 2.0000. Below an error of 2 the ½ makes squared error the smaller of the two; above it, squared error would be larger. On this slider, 2.0 is the largest error available, which is why the two curves meet exactly at the end of the track and nowhere else.

  3. 03
    With the answer at +1, find the prediction where Huber stops agreeing with squared error, to the nearest 0.05.
    Hint

    Huber’s two branches swap at |e| = δ, and this widget’s δ is 1.0.

    Answer

    Prediction 0.00, where the error is exactly 1.0 and both read 0.5000. At prediction 0.05 (error 0.95) they still agree at 0.4513. At prediction −0.05 (error 1.05) squared error reads 0.5513 and Huber reads 0.5500, and from there they separate for good. The gap widens with every further step, which is the whole design.

  4. 04
    Predict, before dragging: as the prediction moves from 0.00 to −1.00, does the gap between squared error and Huber grow steadily, or accelerate? Check it.
    Hint

    Write down the three readings at 0.00, −0.50 and −1.00 for both losses and take the differences.

    Answer

    It accelerates. Squared 0.5000 / 1.1250 / 2.0000 against Huber 0.5000 / 1.0000 / 1.5000, so the gaps are 0.0000, 0.1250, 0.5000. The gap quadruples over the second half of the drag. That is squared error’s quadratic growth pulling away from Huber’s linear branch, and it is precisely how much influence one outlier is buying itself.

  5. 05
    Using only the widget’s static cross-entropy panel and the switch, argue that the loss you pick changes which example the network works hardest on.
    Hint

    Compare the ratio between two examples’ scores, not the scores themselves.

    Answer

    Take an error of 0.5 and an error of 2.0. Under MAE they score 0.5000 and 2.0000, a ratio of 4. Under squared error, 0.1250 and 2.0000, a ratio of 16. Under the panel’s cross-entropy, a prediction of 0.02 against 1 scores 3.912 while squared error scores 0.480, a ratio of 8.15 for the same single example. The bad example is worth four times more under squared error than under MAE, and the confidently wrong example is worth eight times more under cross-entropy than under squared error. The loss is a weighting over your dataset, and you chose it.

Step 10 - Seen in the wild
  • PyTorchnn.CrossEntropyLoss is one line, and it is the line that trains almost every image classifier you have ever used. It takes raw logits, not probabilities, precisely because of the p − y cancellation in rung 5.
  • DeepMind’s DQNThe 2015 Atari agent used Huber loss rather than squared error, for the reason on this page: a single freak reward would otherwise have produced a gradient large enough to wreck the network.
Step 11 - Memory anchor

Squared error asks how far the arrow landed from the gold. Cross-entropy asks how much you bet before you shot.

Which question you ask decides which mistakes your network works hardest to never make again.

Step 12 - The next break

A loss is a single number saying how bad a guess was. It says nothing about which of the network’s thousands of weights was responsible, or which way to move any of them. Nothing on this page can turn 0.0900 into a change to w. That conversion is a separate machine, and building it takes the next two pages: one to define what “which way to move” even means, and one to compute it for every weight at once.

Gradients and Gradient Descent