Loss Functions
Before a network can improve, we need to tell it what counts as an error. A loss function assigns a numerical cost to a prediction, and that choice affects which mistakes the model tries hardest to reduce.
14 sections · 10 questions · 4 coding challenges
The arrow keys move between sections too.
What Should Count as a Worse Answer?
BOTIE
Least squares established one influential answer by squaring discrepancies. But unusually large observations can then dominate the fit. Huber's 1964 work on robust estimation studied procedures that remain useful when the assumed distribution is slightly wrong, motivating a loss that is quadratic near the centre and grows more gently in the tails.
The choice of loss therefore expresses a judgement about the problem. Squared error strongly emphasises large numerical misses. Absolute error treats their growth differently. A probability loss judges the probability assigned to the observed outcome. Before differentiating a loss, we should understand what behaviour minimising it rewards and which kind of bad answer it considers expensive.
Sources for this history
How Wrong Was That Prediction?
A prediction can miss in several ways. A small numerical error, one extreme mistake, and a confident wrong category may deserve different treatment.
A loss function turns those outcomes into a fitting objective. We need to decide what should be costly before choosing its formula.
Keep guesses beside known answers
A loss starts from a prediction and a target. The difference describes what went wrong on one example. Across a batch, those individual outcomes must be combined under a stated rule.
Prediction
- A little too high
- Far too low
Known answer
- The target for each example
Choose the error meaning before the formula
The full lesson calculates regression and classification losses and their derivatives separately, with the input conventions made explicit.
The objective used for fitting and the metric used to judge practical performance need not be identical.
Explore the full example
Move a prediction away from its target and compare the loss curves. Read the loss value and its slope separately, especially for large errors.
| loss | reads the raw output as | probability | cost | gradient | slope by nudging |
|---|---|---|---|---|---|
| Squared error | the prediction itself | … | … | … | |
| Absolute error | the prediction itself | … | … | … | |
| Huber | the prediction itself | … | … | … | |
| Binary cross-entropy | the prediction itself | … | … | … | |
| Softmax cross-entropy | the prediction itself | … | … | … |
The last column nudges the raw output by … either side, measures the cost at both, and divides the difference by the gap. The largest disagreement with the gradient across the rows shown is ….
Part 1. What a Loss Is For
1. A guess, a truth, and a price
Take the four people the PCA page measured, 180, 160, 175 and 165 centimetres, weighing 78, 58, 63 and 73 kilograms, and draw a line through their mean point with a slope of 0.6 kilograms per centimetre. The line guesses a weight for each of them, 74, 62, 71 and 65, and each guess misses. The question a loss answers is how much those four misses should cost, as one number, so that a better line is one with a smaller number and the search for it can begin.
| person | guessed | measured | miss, guess less truth |
|---|---|---|---|
| 180 cm | 74 | 78 | −4 |
| 160 cm | 62 | 58 | 4 |
| 175 cm | 71 | 63 | 8 |
| 165 cm | 65 | 73 | −8 |
The measured four under the slope-0.6 line. Every batch number on this page is worked from these four misses.
A miss on its own has a sign, and the sign is the wrong thing to add up, since minus four and four would cancel to nothing while both guesses were wrong. So every loss here first turns a miss into something that cannot be negative, and the five differ in how they do it.
Squaring, taking the size, squaring up close and taking the size far out, and for the two losses that price a probability rather than a weight, taking the negative logarithm of the probability the model gave to what was true.
Keep in mind
2. One number for the whole batch
A network is scored on many people at once, and the loss has to say what the whole batch costs. Every loss on this page does the same two things with the individual prices, adds them up and divides by the number of rows. The division is what keeps a batch of forty comparable with a batch of four, since without it the loss would double whenever the batch did, and so would the slope the network steps against.
loss = ( price of miss 1 + price of miss 2 + … + price of miss n ) / nThe four, and the four twice over
A mean loss stays the same when the entire batch is duplicated. Here are the original four costs and the duplicated total.
original mean = (8 + 8 + 32 + 32) / 4 = 80/4 = 20
duplicated mean = (2 × 80) / 8 = 20Each duplicate receives half the original per-row gradient, but there are twice as many contributions. Their sum, and therefore the parameter gradient, is unchanged. Absolute error and Huber preserve their respective means in the same way.
Keep in mind
3. The value and the slope are different things
The box at the top of the page has one slider, the raw output of a network’s last layer for one person, and it draws two charts. The upper one is what each loss charges at every raw output, and the lower one is the slope of that charge, which is the thing a network actually uses, because the slope says which way to move the output and how hard.
The value is the number we plot as a training curve and quote when a run is over, and the slope is the number the next step is taken from, so a loss that reported only its value would give a network nothing to do with it.
Both come back from one call, since computing either alone would mean running the squash twice, and the table under the charts checks that they agree with each other. The last column nudges the raw output by a hundred-thousandth either side, measures the cost at both, and divides the difference by the gap, which is the calculus primer’s definition of a slope.
At the halfway guess the largest disagreement between the slope reported alongside the value and the slope found by nudging is 2.3 parts in a trillion, and across the whole slider it never passes 2.1 parts in ten billion.
Where the check is allowed to disagree
Drag the raw output to the target, one half. Absolute error has a corner there, no slope exists, and the slope reported is zero; the nudge, straddling the corner symmetrically, also finds zero to within 3 parts in a trillion. Drag it to 1.5, which is exactly the Huber knee. The two pieces of the Huber loss meet at a value of 0.5 and a slope of 1, and the nudge, straddling a point where the curvature changes, disagrees by 2.5 parts in a million, a thousand times more than anywhere else on the chart and still agreement to five decimals.
Keep in mind
Part 2. Three Ways to Price a Miss in Kilograms
4. Squared error, the bowl
Square the miss, halve it, and that is the price. The half is bookkeeping and nothing more; it is there so the slope comes out as the miss itself rather than twice the miss, and a library that leaves it out reports values twice as large and gradients twice as steep, which the learning rate then has to absorb. Writing p for the guess and y for the truth, the price and its slope are these.
price = (p − y)² / 2
slope = p − yThe four under squared error
The prediction errors are minus four, four, eight and minus eight kilograms. Half-squared error magnifies the larger errors.
individual costs = ½ × (16, 16, 64, 64) = (8, 8, 32, 32)
mean loss = (8 + 8 + 32 + 32) / 4 = 20
output gradients = (−4, 4, 8, −8) / 4 = (−1, 1, 2, −2)
large-error share of cost = 64 / 80 = 0.8The two larger misses account for eighty percent of the loss. Their gradients are also twice as large in magnitude as those of the smaller misses.
That growth is the whole character of squared error. On the playground the bowl steepens without limit, so a raw output of 3 against a target of one half costs 3.125 and pulls by 2.5, and a raw output of −5 costs 15.125 and pulls by −5.5. Nothing caps it, and section 17 is what that does when one of the misses is not a miss at all.
Keep in mind
5. Absolute error, the vee
Take the size of the miss and stop. The price grows in a straight line, and its slope is only ever plus one, minus one, or nothing at all, because the size of a miss changes at the same rate however large the miss already is.
price = |p − y|
slope = sign(p − y)The four under absolute error
Absolute error keeps each miss’s magnitude without squaring it. Its derivative depends on the sign of the miss, not its size.
mean absolute loss = (4 + 4 + 8 + 8) / 4 = 6
output gradients = (−1, 1, 1, −1) / 4
= (−0.25, 0.25, 0.25, −0.25)The larger misses still contribute more to the loss. They no longer produce larger output gradients.
The corner at zero is the price of that. Where the guess equals the truth no slope exists, and the slope reported there is zero, which on the playground is the one point where the amber dot on the gradient chart is neither at 1 nor at −1. It also means the slope never shrinks as the guess gets close, so a walk under absolute error keeps stepping at full size right up to the answer and then past it, which section 19 measures.
Keep in mind
6. Huber, the bowl with a knee
Huber takes a threshold, written d here, and is squared error inside it and absolute error outside it, with the outer piece shifted so the two meet without a jump. Small misses are corrected smoothly and the corner at zero is gone, while a miss beyond the knee pulls by exactly d and no more.
|p − y| ≤ d price = (p − y)² / 2 slope = p − y
|p − y| > d price = d · (|p − y| − d/2) slope = d · sign(p − y)The four under Huber with a knee of 5 kilograms
With Huber threshold five, the four-kilogram misses use the quadratic branch. The eight-kilogram misses use the linear branch.
cost at magnitude 4 = ½ × 4² = 8
cost at magnitude 8 = 5 × (8 − 5/2) = 27.5
mean loss = (8 + 8 + 27.5 + 27.5) / 4 = 17.75
output gradients = (−4, 4, 5, −5) / 4
= (−1, 1, 1.25, −1.25)Large misses can still produce larger gradients here, but the threshold caps that growth.
The d/2 in the outer piece looks like decoration and is not. At a miss of exactly d the inner piece gives d squared over two and the outer gives d times d less d over two, the same number, and the slopes are d on both sides. Drop the d/2 and the price jumps at the knee, which leaves a step an optimiser can stop in. On the playground the knee is at 1 and the worked check in section 3 lands on it.
The knee is in kilograms
The threshold is a distance in the target’s own units, so a knee of 1 means something different on weights in kilograms from what it means on the playground’s targets near one half. Set the knee to 1 kilogram on the four and none of them is inside it, the batch costs 5.5, and every pull is a quarter in size, which is absolute error wearing a different name.
A knee has to be chosen against the size of the misses it will meet, and the Huber regression on its own page estimates a scale from the residuals and measures its knee in multiples of that; the network loss here takes a fixed number and leaves the choice to you.
Keep in mind
7. Where one person's miss goes
The three rules are easiest to compare on one batch at once. The widget below draws a line of your choosing through the people, scores every miss under all three losses, and shows each person’s own cost as a bar and their share of the batch’s pull in the table. Start with the measured four and the slope-0.6 line, then tilt the line and watch which bars grow fastest.
| person | miss, kg | squared error pull | absolute error pull | Huber pull | inside the knee |
|---|---|---|---|---|---|
| 180 cm, 78 kg | … | … | … | … | … |
| 160 cm, 58 kg | … | … | … | … | … |
| 175 cm, 63 kg | … | … | … | … | … |
| 165 cm, 73 kg | … | … | … | … | … |
On the four, the two eight-kilogram misses carry 80 percent of the cost under squared error, 67 percent under absolute error and 77 percent under Huber, and their share of the pull is 67, 50 and 56 percent. Squared error is the only one of the three whose pull share keeps growing with the miss, and the bars make the difference visible before any of the arithmetic is read.
Where the six percentages come from
A share of the cost is one person’s price over the sum of the four prices. The prices are the ones sections 4 to 6 worked, (8, 8, 32, 32) under squared error, (4, 4, 8, 8) under absolute error and (8, 8, 27.5, 27.5) under Huber with a knee of 5 kilograms. Add the two eight-kilogram people’s prices and divide by the total.
squared error (32 + 32) / 80 = 0.80
absolute error (8 + 8) / 24 ≈ 0.67
Huber, knee 5 (27.5 + 27.5) / 71 ≈ 0.77A share of the pull is built the same way from the gradients, with their signs dropped first. Two people pulling the line in opposite directions are both still pulling, so it is the sizes that are added. The gradients are (−1, 1, 2, −2), (−0.25, 0.25, 0.25, −0.25) and (−1, 1, 1.25, −1.25).
squared error (2 + 2) / (1 + 1 + 2 + 2) ≈ 0.67
absolute error (0.25 + 0.25) / (4 × 0.25) = 0.50
Huber, knee 5 (1.25 + 1.25) / (1 + 1 + 1.25 + 1.25) ≈ 0.56Half the people hold half the pull under absolute error because every gradient is the same size. Under squared error the same two hold two thirds of it, and Huber’s knee sits between the two.
Keep in mind
Part 3. Two Ways to Price a Wrong Probability
8. A score is not a probability until it is squashed
When the question is whether someone is an adult rather than how much they weigh, the last layer still hands over a plain number, a score that can be anything from far below zero to far above it, and a score cannot be compared to a yes or a no. The logistic page’s answer was to push the score through the sigmoid, which turns any number into a probability between zero and one, and that squash is the first thing both classification losses do, so the last layer stays straight and the activation function happens inside the loss, for a reason section 14 measures.
p = σ(z) = 1 / (1 + e^(−z))On the playground the raw output of zero squashes to exactly one half, which is the network saying it cannot tell, and that is why the page’s halfway guess is a raw output of zero rather than a probability of zero. A raw output of 3 squashes to 0.9526 and of −3 to 0.0474.
Keep in mind
9. Binary cross-entropy, the price of a probability
Once the score is a probability of yes, the price is the negative logarithm of the probability the model gave to whichever answer was actually true. A model that gave the truth a probability of one pays nothing, one that gave it a half pays the logarithm of two, and one that gave it almost nothing pays without limit. Writing y for the label, one for yes and zero for no, the two cases fold into one line.
price = −y · log(p) − (1 − y) · log(1 − p)
slope at the raw output = p − yThe halfway guess, then a confident one
For a positive label, a raw score of zero gives a probability of one half. Binary cross-entropy and its derivative with respect to that score are:
probability = sigmoid(0) = 0.5
loss = −ln(0.5) ≈ 0.6931
score gradient = 0.5 − 1 = −0.5A score of three raises the correct-label probability to about 0.9526 and reduces the loss to about 0.0486. Negative scores favour the wrong class: at minus three the loss is about 3.0486, and at minus five it is about 5.0067.
Two shapes are worth noticing on the playground. On the side where the squash agrees with the label the curve is flat and cheap, and on the side where it does not the curve rises like a straight line of slope one, since the logarithm of a sigmoid far out is the raw output itself. And the pull never exceeds one in size however wrong the model is, because a probability minus a label cannot.
Keep in mind
10. The value has a ceiling and the gradient does not
A sigmoid pushed far enough saturates to exactly zero or exactly one in double precision, and the logarithm of exactly zero is minus infinity, which would poison the average for a row that was merely very wrong. So the probability is clipped away from both ends before the logarithm is taken, by one machine epsilon, and the price stops growing at the negative logarithm of that, which is 36.0437.
Machine epsilon is the gap between one and the next number double precision can represent. A probability clipped to sit at least that far from zero and from one always has a finite logarithm, and the largest price the clip allows is the logarithm of that gap with its sign changed.
machine epsilon ε ≈ 2.22 × 10⁻¹⁶
ceiling −ln(ε) ≈ 36.0437Measured on one row
A score of 30 with the label no costs 30.0010 and pulls by 1.0 to thirteen decimals. A score of 40 costs 36.0437, the ceiling, and pulls by exactly 1.0. The gradient is a subtraction of two finite numbers and needs no clipping, so it is exact where the value is capped, and a network keeps being pushed the right way past the point where its reported loss has stopped rising.
Keep in mind
11. Softmax cross-entropy, one of many
With three classes, child, teenager and adult, the last layer hands over three scores per person, and the squash has to read across the row rather than down a column. The softmax raises e to each score and divides by the sum, so the three come out positive and summing to one, and the price is again the negative logarithm of the probability on the true class.
The slope at every score is the probability of that class minus the truth for that class, which is one on the true class and zero on the rest.
pₖ = e^(zₖ) / Σⱼ e^(zⱼ)
price = −log(p on the true class)
slope at zₖ = pₖ − yₖScores of 1, 2 and 3 with adult true
The probabilities are 0.0900, 0.2447 and 0.6652, the price is the negative logarithm of 0.6652, which is 0.4076, and the pulls are 0.0900, 0.2447 and −0.3348. The true class is pushed up by one less its probability and each wrong class is pushed down by its own probability, so the three pulls sum to zero, which the readout reports as −1.1 parts in ten quadrillion. Switch the true class to child and the price becomes 2.4076, the negative logarithm of 0.0900, with the child score pulled up by 0.9100.
Tick the box that adds a hundred to every score and nothing changes, the same probabilities, the same price, the same pulls to the last digit. The softmax only ever reads the differences between the scores, which is why the row maximum is subtracted before e is raised to anything; written literally, e to the hundred-and-three overflows nothing but e to a thousand does.
Keep in mind
12. The two-class softmax is the sigmoid
On the playground the rose and sky curves lie on top of one another, which is why the sky one is dashed. That is an identity rather than a drawing choice. With two classes, and the second class’s score held at zero, the softmax probability of the first class is e to the score over e to the score plus one, which is the sigmoid of the score.
The page’s softmax reading is built exactly that way, the raw output in the first column and a zero in the second, and the two losses agree on the value, the gradient and the probability at every raw output on the slider.
e^z / (e^z + e^0) = 1 / (1 + e^(−z)) = σ(z)Where they part, and why it does not matter
They part only where the value is clipped, because the two losses clip at different floors. Binary cross-entropy caps at 36.0437, as section 10 measured. The softmax loss floors its probabilities at the smallest positive double instead, so at scores of 40 and 0 with the second class true it reports 40.0 where the sigmoid reports 36.0437. The gradients agree exactly at both, 1 and −1 against 1, and training never reads the value.
Keep in mind
Check yourself
Questions on Parts 1 to 3
Question 1 of 5
Every loss on the page adds the individual prices and divides by the number of rows. What does the division buy?
Part 4. The Slope Every Network Starts From
13. Three losses, one subtraction
Now look at the gradient chart with the slider at the halfway guess. Four of the five dots are at exactly the same value, −0.5. One of the four is Huber, which sits there only because a miss of one half is inside its knee of 1, where it is squared error under another name. The other three are the fact the page exists to teach. Squared error with a plain output, binary cross-entropy with a sigmoid, and softmax cross-entropy with a softmax all have the same slope at the raw output, the prediction minus the truth over the row count, and it is the simplest expression on the page.
d loss / d raw output = (prediction − truth) / nFor squared error the prediction is the raw output itself, zero against a target of one half. For the other two it is the probability the squash produced, one half against a label of one. Both misses are minus one half, and the halfway guess was built so that they would be. Drag the slider to 3 and the dots part, 2.5 against −0.0474, because the prediction now means different things, a number that is two and a half too high and a probability that is nearly right.
What the three share is the form, and the numbers agree only at a raw output where a miss in weight and a miss in probability happen to be the same size.
Keep in mind
14. Why the squash belongs to the loss
Tick the playground’s last box and a grey dashed curve appears, a sigmoid output scored by squared error, which is the pairing a reader might reach for first, since it is a probability compared to a label by the loss that has been on every page. It is computed here through a one-neuron layer that applies the sigmoid, so the number is measured rather than argued. At the halfway guess it costs 0.125 and pulls by −0.125, a quarter of what cross-entropy pulls.
Confidently wrong under the wrong pairing
Drag the slider to −5 with the label yes. The probability is 0.0067, the model is as wrong as the slider allows, and cross-entropy pulls by −0.9933. The sigmoid scored by squared error pulls by −0.0066, a hundred and fifty times less, because its slope carries the sigmoid’s own slope as a factor, which is p times one less p and is nearly zero exactly where the model is most wrong, so the pairing pulls least on the rows where the model most needs pulling, and a network trained under it can spend a long time being confidently wrong about the same people.
The binary and multiclass cross-entropy implementations used on this site expect raw scores, also called logits. They apply sigmoid or softmax internally, so the preceding output layer uses identity.
Other APIs accept probabilities instead. A probability-based loss must receive the appropriate normalized probabilities. Check the loss interface: adding sigmoid before a loss that already expects logits applies it twice and changes the model being optimized.
Keep in mind
Match the output layer to the loss interface. The cross-entropy losses in this SDK take logits, so pass raw scores. A loss documented to take probabilities has a different contract. Squared error with a sigmoid output is another valid objective, but its gradient can become very small for confidently wrong predictions.
15. The canonical link
The coincidence has a mechanism. Each squash is the one whose own derivative cancels the loss’s, leaving the subtraction behind, and that is what Nelder and Wedderburn called a canonical link. Binary cross-entropy is where the cancellation is easiest to watch. With p the sigmoid of the raw output z and y the label, differentiate the price with respect to p, then multiply by the sigmoid’s own slope.
| step | what happened |
|---|---|
| d price / dp = −y/p + (1 − y)/(1 − p) | differentiate the two logarithms with respect to the probability |
| = (p − y) / (p (1 − p)) | put the two fractions over one denominator |
| dp / dz = p (1 − p) | the sigmoid's own slope, the logistic page's result |
| d price / dz = (p − y) / (p (1 − p)) · p (1 − p) = p − y | the chain rule; the denominator the price produced is the factor the squash produced, and they cancel |
›The softmax case, and the regression case
The softmax’s own slope is a whole matrix, every output depending on every score, and it is exactly the term that cancels, leaving the probability minus the one-hot truth on every class at once, which is the row of pulls in section 11. Squared error needs no cancellation at all, since it has no squash and its price is already a quadratic in the raw output.
Gauss reached the regression case for the bell curve, and the sigmoid and the softmax are the same result for a yes-or-no and for one-of-many. In every case the loss is the negative logarithm of a likelihood and the squash is that likelihood’s natural parameter unwound, which is why the pairing is not a convention that hardened.
Absolute error and Huber are on the page to break the pattern on purpose. Their slopes are a sign and a capped miss, neither of them prediction minus truth, and that difference is the whole reason to use them, which Part 5 measures.
Keep in mind
16. Which loss goes with which last layer
The pairing follows from the question, so it can be written as a table, with the last layer straight in every row and the activation function, where there is one, inside the loss.
| the question | the loss, and what the loss applies first |
|---|---|
| how much does this person weigh | squared error on the raw output, no squash; or absolute error or Huber when some weights cannot be trusted. |
| is this person an adult | binary cross-entropy, which applies the sigmoid; one output, a label of zero or one. |
| child, teenager or adult | softmax cross-entropy, which applies the softmax across the row; one output per class, a one-hot truth. |
| a sigmoid probability scored with squared error | the pairing section 14 measured; it trains, and it stops pulling where it is most wrong. |
Keep in mind
Part 5. What a Loss Does to an Outlier
17. One mistyped weight
Take the fifteen people of the line page’s ideal case, who lie close to one line, and suppose the ninth of them, 176 centimetres and 72.5 kilograms, was entered with the digits swapped as 27.5. Nothing about the person changed, and one number in the table did. Score all sixteen under the line that fits the clean fifteen, slope 0.7968 and intercept −67.44, and the mistyped row misses by 45.29 kilograms where nobody else misses by more than a kilogram or so.
| loss | value over 16 rows | the mistyped row's share of the cost | its share of the pull |
|---|---|---|---|
| squared error | 64.18 | 0.9987 | 0.892 |
| absolute error | 3.17 | 0.892 | 0.0625, which is one sixteenth |
| Huber, knee 5 kg | 13.46 | 0.994 | 0.478 |
The ideal fifteen and the mistyped person, scored under the clean least squares line, with a knee of five kilograms.
Under squared error, the corrupted row accounts for 99.87 percent of the loss and 89 percent of the summed gradient magnitudes. Under absolute error, it receives the same output-gradient magnitude as every other row. Huber caps it at the threshold divided by the batch size.
absolute-error gradient magnitude = 1/16 = 0.0625
Huber maximum gradient magnitude = 5/16 = 0.3125These are gradients with respect to individual predictions. Parameter gradients also include the inputs through which those predictions were made.
Keep in mind
18. Three losses, three lines
Now let each loss fit its own line. The widget walks one linear neuron down each loss for four hundred steps from a flat line at the mean weight, on the same people, and draws the three lines it arrives at. Tick the switch to swap in the mistyped weight and watch which lines move.
squared error
absolute error
Huber, knee 5 kg
Four hundred steps at a rate of 0.1 on standardised heights, from a flat line at the mean weight. The closed-form least squares line is …, and the squared-error walk is within … of it.
On the clean fifteen all three lines nearly coincide. Squared error lands on the closed-form least squares line to within 1.1 parts in ten trillion, Huber lands on the same line because every miss is inside its knee, and absolute error settles a little away, slope 0.7993 against 0.7968, since the median line and the mean line are not quite the same line. Each guesses about 72.79 kilograms for the person at 176 centimetres.
With the weight mistyped, the squared-error line drops to a slope of 0.7464 and an intercept of −61.55 and now guesses 69.82 kilograms at 176 centimetres, three kilograms lower than before, for a person who has not changed. The Huber line moves to 0.7908 and −66.75, guessing 72.44, a third of a kilogram lower. The absolute error line moves to 0.7983 and −67.72, guessing 72.78, one hundredth of a kilogram lower.
The mistyped row is missed by 42.3, 44.9 and 45.3 kilograms by the three lines in turn, so the two robust rules leave it almost exactly as wrong as it was and squared error closes three kilograms of the gap at everyone else’s expense.
Keep in mind
19. Robust is a statement about the pull
What the word robust means, when a textbook says it, is that no single row can pull harder than a fixed amount. Absolute error caps the pull at one over the row count and Huber at the knee over the row count, and squared error caps it nowhere. The cost is not what is capped; the mistyped row is most of the cost under all three rules, and it is only under the two robust ones that being most of the cost does not make it most of the say.
The same cap has a price at the other end of the walk. The readouts show the largest slope left in the neuron after the last step, and for squared error and Huber it is 8 parts in a quadrillion, which is a walk that has stopped. For absolute error it is 0.0926 on the clean fifteen and 0.125 with the outlier, because a sign never shrinks as the guess gets close, so the walk keeps stepping by the full learning rate and settles into a twitch about the answer rather than onto it.
The clean absolute-error loss is 0.3609 after four hundred steps and still moving in the third decimal.
Keep in mind
20. A mislabelled adult
Cross-entropy can also be sensitive to incorrect labels. Take sixteen people: eight children below 152 centimetres and eight adults above it. A fixed rule assigns a score from height, then a sigmoid probability.
score = (height − 152) / 5
P(adult) = sigmoid(score)The rule classifies all sixteen correctly and has mean loss about 0.0854. The two people nearest the boundary, at 148 and 156 centimetres, each cost about 0.3711. Now change the 178-centimetre adult’s label to child.
The curve is the rule’s probability of adult. Each bar is that person’s cost, drawn from their label towards the middle.
The model still assigns that person about 0.9945 probability of adulthood, but the changed label now penalizes that confidence. Calculate the loss using the full score rather than rounded probabilities.
score = (178 − 152) / 5 = 5.2
incorrect-label loss = −ln(1 − sigmoid(5.2)) ≈ 5.2055
score gradient contribution = sigmoid(5.2) / 16 ≈ 0.0622
accuracy = 15 / 16 = 0.9375The mean loss rises to about 0.4104, with this row contributing seventy-nine percent of the total. Accuracy records one wrong label; cross-entropy additionally measures how confidently the model disagrees with it.
Keep in mind
Part 6. Where Each Loss Fails
21. The loss is not the score
A classifier is judged by how many people it called correctly, and it is trained by the loss, and the two can disagree. Change the scale of the rule in the widget above from 5 to 2.5 and then to 10. The accuracy is 1.0 at all three, since the probabilities cross one half at the same height whatever the scale, and the loss is 0.0290, 0.0854 and 0.1910, a factor of six apart.
The loss rewards confidence on people already called correctly, which accuracy cannot see, and the training page’s run has stretches where the loss falls for fifty epochs while the accuracy does not move at all.
Keep in mind
22. Where each of the five fails
Every loss on this page fails somewhere, and the failures are measured above rather than listed from memory.
| the loss | where it fails |
|---|---|
| squared error | one mistyped weight took 89 percent of the pull and moved the line three kilograms; it is the maximum-likelihood rule for bell-shaped noise and the wrong rule when the misses include mistakes. |
| absolute error | no slope at zero, no curvature anywhere, so descent twitches by 0.125 at the answer instead of settling; and it fits the median, which is not the mean when the misses are skewed. |
| Huber | a knee in the wrong units is one of the other two in disguise: at 1 kilogram on the four it was absolute error, and at 50 it would be squared error. The knee has to be chosen against the misses. |
| binary cross-entropy | a mislabelled row costs 5.2 and pulls by nearly one, with no cap; the value saturates at 36.04 while the gradient does not; and this SDK expects logits, so an additional sigmoid before the loss changes the objective. |
| softmax cross-entropy | needs the class width stated to build the one-hot truth, and a class index beyond that width or below zero has no meaning; with one class the price is always zero and nothing is learned. |
Keep in mind
Part 7. Implementation and Failure Contracts
23. What a loss must specify
A complete implementation states what it divides by, the rows or every entry or nothing; whether squared error carries the half; the units the Huber knee is measured in and what happens exactly at the knee; which branch absolute error takes at a tie; whether the classification losses apply their own squash, so that a caller knows to leave the last layer straight; how the probabilities are clipped before the logarithm and at what floor; what shape the truth takes, a column of zeros and ones or a one-hot block; and that the value and the gradient are produced by one call, so a caller cannot get one without the other.
Keep in mind
24. The edges, probed
Each row below was tried rather than reasoned about, and the page’s own request refuses at the door the cases the arithmetic lets through.
| the edge | the behaviour |
|---|---|
| an empty batch | the documented contract promises a refusal, and the arithmetic divides by zero rows and answers not-a-number with a warning; recorded rather than repaired, and the request refuses it at the door before the batch is ever measured. |
| one row | accepted; the division is by one and the batch value is the row's own price, 32 for a miss of eight under squared error. |
| outputs and truths of different shapes | refused by name, since the two blocks describe the same rows. |
| a not-a-number in the outputs | passes through; the value and the gradient are not-a-number and nothing is raised. The layers check finiteness on the way in; the losses do not. |
| an infinite output | squared error answers infinity; cross-entropy saturates to its 36.04 ceiling with a gradient of exactly one, since the stable sigmoid handles the overflow. |
| a Huber knee of zero, negative, or not finite | refused at construction; at zero it is absolute error with an extra branch and below zero it is nothing at all. |
| absolute error at an exact tie | accepted, gradient zero; no slope exists and the branch taken is the one that pulls nowhere. |
| Huber exactly at the knee | both pieces answer a price of 0.5 at a knee of one and a slope of one, to the last bit on one side and to one part in a trillion on the other. |
| a label of one half | accepted, and read as a soft label; at the halfway guess it costs the logarithm of two and pulls nowhere. The request refuses anything but zero and one. |
| a label of two | accepted, and nonsense, a pull of minus 1.5 at the halfway guess; the request refuses it. |
| a class index beyond the row | the one-hot builder raises a bare index error, written for an interpreter rather than for a reader; the request refuses it with the width named. |
| a negative class index | wraps silently to the last column, since that is what a negative index does to a numpy block; the request refuses it. |
| a softmax row of one class | accepted; the probability is always one, the price always zero, and nothing is learned. |
| every height the same, in the descent | refused by the line fit before the standardising could divide by a spread of zero, because a column carrying no variation has nothing to divide by. |
| unfitted use, or mismatched feature names | not applicable; a loss holds no fitted state and reads no names, only two blocks of the same shape. |
The empty batch is the one worth dwelling on, because it is the case where the documented contract and the actual behaviour disagree. The contract says an empty block is refused, and the arithmetic divides a sum of nothing by zero rows, which numpy answers with not-a-number and a warning rather than an exception. It is documented here rather than defended, the request refuses the case at the door, and nothing behind this page was changed to serve it.
Check yourself
Questions on Parts 4 to 7
Question 1 of 5
With the slider at the halfway guess, squared error, binary cross-entropy and softmax cross-entropy all report a slope of −0.5. What do the three share?
Try it with Botie
Build with Botie
Write and run Python