Sample gradients
A sample gradient differentiates one selected example’s loss at the current parameter.
On this page 8 sections
01A model’s training loss combines errors from examples#
A model’s training loss combines errors from examples. A sample gradient measures one example’s contribution at the current parameter. Computing a small contribution can be much cheaper than processing a whole dataset, which is why stochastic training uses it.
Each example has its own loss and gradient at the current parameter. For one linear prediction with half-squared error:
With w=2, x=4 and y=3, prediction is 8 and residual is 5. Multiply that residual by the feature to get gradient 20.
For prediction and loss , set residual . The square contributes derivative with respect to the prediction; the prediction contributes derivative with respect to . Multiplying gives . At , the residual is 5 and the gradient is 20.
Loss . Given , find .
Show answer and explanation
Residual -7 times feature -2 gives 14.
02For a vector parameter, prediction is w^T x#
For a vector parameter, prediction is . The gradient of half-squared loss is .
Compute the residual once, then multiply every feature coordinate by it.
Half-squared loss, prediction . Given , , . Find the gradient in w.
Show answer and explanation
3×(1,2)=(3,6).
03The residual alone omits the feature multiplier#
The residual alone omits the feature multiplier. Its sign and magnitude can differ from the gradient.
A selected-example gradient can differ from the dataset average. Uniform sampling makes it unbiased for that average, not identical on every draw.
Loss ; . Draft gradient: . Correct it.
Show answer and explanation
Residual 3 times feature 5 gives 15.
04A closer look#
The residual and gradient are different: the feature value scales how much a weight change affects the prediction. An example with feature zero gives no gradient for that weight under this model, even if its prediction has a nonzero error.
Differentiate the stated per-example loss; for half-squared linear error, multiply the residual by the feature.
- Compute a single-example gradient for an empirical objective.
Sources & further reading
- [1]D2L §12.4.1, equations12.4.1–12.4.4 ↗D2L §12.4.1, equations12.4.1–12.4.4 · Article