Skip to content

Loss and Gradients: How a Model Learns

6 min reading

Outcome: Compute two gradient-descent updates, explain the learning rate and distinguish reducing training loss from proving useful generalization.

Page details

GenAI foundations · Chapter 6 · Sources

Published · Last edited: · ~6 min reading · ~17 min with practice

20px

Know first: Neural Networks in Twenty Minutes: Weights, Layers, and Nonlinearity

GenAI foundations glossary

Hypothetical scenario. Dev reports that Northwind's adapted model has a lower training loss. Priya asks whether it now answers refund questions better. The optimization result is evidence about one objective on particular data; answering Priya requires an evaluation of the actual task.

The objective gives error a number

A loss function assigns a numerical penalty to a prediction under a chosen objective. A training process uses that signal to adjust parameters. The choice of objective matters: matching observed next tokens, predicting a class and following a preference are not identical tasks, even if each produces a loss number.

For a small teaching example, use one adjustable parameter w and a loss L(w) = (w − 3)². The loss is zero when w is three and positive otherwise. This is not a language model or a real business metric; it is a simple landscape that lets every update be shown without hiding the arithmetic in software.

A gradient measures local change in the loss with respect to parameters. In one dimension, it is a slope. A positive slope means increasing the parameter locally increases loss; a negative slope means increasing the parameter locally decreases loss. The word “locally” matters because a slope describes behavior near the current position, not a guarantee about an arbitrarily large move.

Current parameterw = 0Local slope2(w − 3) = −6Subtract 0.1 × slopew becomes 0.6
Current parameter: w = 0 → Local slope: 2(w − 3) = −6 → Subtract 0.1 × slope: w becomes 0.6. Illustrative conceptual flow.

Follow two updates

The derivative of the chosen square is dL/dw = 2(w − 3). Begin at w = 0. The loss is (0 − 3)² = 9, and the gradient is 2(0 − 3) = −6. Choose learning rate η = 0.1. A gradient-descent update subtracts the scaled gradient: w_new = w − η × gradient.

The first step gives 0 − 0.1 × (−6) = 0.6. The negative gradient makes the subtraction add a positive amount. The new loss is (0.6 − 3)² = (−2.4)² = 5.76, lower than nine. The direction came from the slope; the step size came from the chosen learning rate.

At w = 0.6, recompute the gradient: 2(0.6 − 3) = −4.8. The second update gives 0.6 − 0.1 × (−4.8) = 0.6 + 0.48 = 1.08. Its loss is (1.08 − 3)² = (−1.92)² = 3.6864. Reusing the first gradient indefinitely would ignore how the local slope changes as the parameter moves.

The learning rate is part of the mechanism

Now start again at zero but choose η = 1. The first update becomes 0 − 1 × (−6) = 6. The loss at six is (6 − 3)² = nine, the same as the starting loss. The next gradient is six, so the next update returns to zero. In this chosen quadratic, that learning rate creates an oscillation rather than progress toward three.

The contrast shows why “follow the gradient” is incomplete advice. Direction and step size jointly determine the update. A very small rate can make progress slow; a large rate can overshoot or destabilize the chosen optimization. Real training adds batches, multiple parameters, changing gradients and optimizer state; its behavior cannot be predicted from this single square alone.

With several parameters the gradient is a vector of partial derivatives. Each coordinate describes local sensitivity to one parameter while the others are held fixed for that derivative. The optimizer combines this information into an update. Large models repeat that process across many numbers, but the need to name the loss and the update rule remains.

What the math does and does not establish

In the wild. Mathematics for Machine Learning, chapter 5 on vector calculus, develops derivatives and gradients as tools for describing local change. Those tools explain the direction used by optimization. They do not decide whether Northwind's training examples represent the support requests it will receive.

A training example can be memorized or reflect a pattern that does not generalize. Evaluation uses data and criteria separate from the updates being optimized. If Priya cares about current policy citations and correct abstention, those outcomes need to be measured directly rather than inferred from a smaller training loss alone.

Learning is not one magical pass

Misconception: one pass through examples teaches every relevant fact reliably. A pass is a traversal of a dataset, not a guarantee about the resulting model. Training quality depends on the examples, objective, optimization and evaluation conditions. More passes can reduce training loss while worsening behavior on new cases.

A second misconception is that a negative gradient means the parameter should be made smaller. In this example the gradient −6 says increasing w lowers loss locally; descent subtracts the negative number and increases w. Trace the signs before applying intuition to a plot.

Use the toy update to interpret learning-rate discussions and distinguish training changes from inference changes. It deliberately excludes the full mechanics of language-model training. The next foundation asks how to evaluate a resulting claim: what was tested, how uncertain is the estimate and does the test represent the use that matters?

Check yourself

Why does the first update increase w even though the rule subtracts a gradient?

The gradient is −6. Subtracting 0.1 × −6 adds 0.6, moving w toward the chosen minimum at three.

What happens with learning rate one in this exact toy loss?

The parameter alternates between zero and six, with loss nine at both. The large step does not reduce the objective.

The training loss fell. Can Priya conclude refund answers are now correct?

No. She needs separate task-representative evaluation, including source support and abstention. Lower training loss does not prove generalization or operational correctness.

Go deeper