Answers · How models work
Why train on a loss function instead of just using accuracy?
In shortBecause accuracy is a pass or fail count that is flat almost everywhere, so it gives the optimiser no slope to follow, while a loss changes smoothly and tells every weight which way to move.
Train on a loss because accuracy is a count of correct answers, and a count is flat almost everywhere: its derivative with respect to every weight is zero except at the exact points where a prediction flips. A loss is a smooth number that changes when the model becomes more or less confident, so it supplies a gradient for every weight on every step.
A count has no slope
Take one binary example, true label , model probability . Accuracy thresholds at : when and otherwise. Cross entropy is . At , and . At , still, but . The same accuracy covers a confident correct answer and a barely correct one, and the loss separates them by .
| p | accuracy A | loss L | dL/dp |
|---|---|---|---|
| 0.9 | 1 | 0.105 | -1.111 |
| 0.51 | 1 | 0.673 | -1.961 |
| 0.49 | 0 | 0.713 | -2.041 |
Across a batch the same point holds. Take three examples with true labels and predicted probabilities . With a threshold every prediction is on the correct side, so accuracy is . The cross entropy is the average of , , and , which is . Change the probabilities to and accuracy is still , but the loss is the average of three copies of , so . The count cannot tell these two batches apart; the loss can, and its gradient points toward the first.
What the gradient sees
The optimiser is not reading the label or the thresholded answer. It is reading one scalar and asking, for each weight, how a small change would alter that scalar. For accuracy the answer is zero almost everywhere, because a small change in a weight changes a probability by a small amount without changing which side of it lies on. At the boundary the answer is a jump: the function is discontinuous, so the derivative is not defined in the ordinary sense. A gradient made of zeros and undefined spikes cannot train a model with many weights. Cross entropy gives a finite slope everywhere is not exactly or , and that slope is largest when the model is confidently wrong. With and , the chain rule gives at . The update with moves by . The accuracy gradient is zero, so the same update would move nothing.
The natural guess is that if accuracy is what you care about, you should optimise accuracy directly. That confuses the score you report with the score the backward pass can differentiate. Accuracy also hides confidence: a prediction at and one at count the same if both are on the correct side, even though the second is much less likely to flip under a small weight change. A loss such as cross entropy keeps that confidence information, so the update can keep improving a correct answer instead of stopping as soon as it crosses the threshold. The loss is not a proxy for accuracy in the sense of being a convenient approximation to the same number; it is a different number, chosen because it has the slope that accuracy lacks.
Where accuracy still belongs
Accuracy remains the right number for reporting, for comparing finished runs, and often for early stopping when the classes are balanced. It is also the right objective if you abandon gradients and use a black-box search that only needs a score, though that costs many more evaluations. With gradient descent the objective has to be differentiable almost everywhere and has to change when the model becomes more confident. If the loss is badly chosen, the model will minimise that loss exactly: on a heavily imbalanced set, a model can reach high accuracy by always predicting the majority class while the loss and the behaviour you care about are poor. That is a reason to choose a loss that encodes the cost of errors, not a reason to train on the count.
Worth keeping
- Accuracy is a step function of the weights, so its gradient is zero almost everywhere.
- A loss is the only part of the training loop that says what good means.
- Accuracy is for reporting and model selection; the gradient needs a smooth surrogate.
Where this goes next
This question is one step of Training a Model From Scratch, a course that works through its subject from the beginning. The page that teaches this part of it is The Loop Everything Sits In, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why do we need a forward pass before we can backpropagate?The gradient is built from local derivatives evaluated at the values the forward pass computed, so the loss alone cannot supply it.
- Why does a bigger batch make the gradient less noisy?Each step's gradient is an average, and an average's error shrinks with the square root of the number of examples, so four times the batch only halves the noise.
- Why did my loss suddenly spike and can I recover without restarting?Your loss spike is probably one bad batch, and you can usually recover by waiting or rolling back to a recent checkpoint instead of restarting the whole run.
- Why does training loss keep falling while validation loss starts rising?Training loss keeps falling because the model is memorising the batches it is shown; held-out loss rises when that memorisation stops helping on data it has not seen.
- Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
- Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.