Loss and backpropagation intuition
What training changes
Training adjusts a model’s parameters using examples and an objective. The forward pass computes a prediction and a loss; the backward pass computes derivatives of that loss with respect to parameters. An optimizer uses these derivatives to propose new parameter values. These are separate operations: computing a gradient alone does not update a weight. This chapter uses small finite numbers and a single numerical prediction, not a full production model.
A single weight and squared loss
Suppose a fictional packing-time estimate is y_hat = w × x, where x is a scaled workload and w is a trainable weight. For x = 2, target y = 6 and w = 1, the prediction is 2. Define L = (y_hat − y)², so L = 16. (Other conventions divide by two or average across examples; the derivative must match the chosen loss.) The chain rule gives dL/dw = dL/dy_hat × dy_hat/dw = 2(y_hat − y) × x = −16. Gradient descent sets w_new = w − rate × dL/dw. A rate of 0.125 moves w to 3, yielding prediction 6 and loss 0 in this special case. A larger rate can overshoot. The first program prints 16 and −16, then 3.0 and 0.0.
def loss_and_gradient(x, y, w): residual = w * x - y return residual ** 2, 2 * residual * x
loss, grad = loss_and_gradient(2, 6, 1)new_w = 1 - 0.125 * gradprint(loss, grad)print(new_w, loss_and_gradient(2, 6, new_w)[0])Backpropagation through two operations
Now let h = a × x and y_hat = b × h. With a = 1, b = 2, x = 2 and y = 6, h = 2, y_hat = 4 and L = 4. Work backward: dL/dy_hat = −4; dL/db = −4 × h = −8; dL/dh = −4 × b = −8; dL/da = −8 × x = −16. Both gradients use the same original parameters. Update both only after calculating them. This is a two-stage linear network for explaining derivatives; without a nonlinear activation it is equivalent to a single linear map. Real neural networks also propagate derivatives through activation functions and sum contributions when a value is reused along multiple paths.
Checking gradients independently
A central finite difference approximates the derivative with [L(w + ε) − L(w − ε)] / (2ε). Change one parameter at a time, holding other inputs fixed. The next program compares the two analytic derivatives with ε = 0.00001; it prints −16 and −8 followed by −16.0 and −8.0. Compare with a numerical tolerance rather than exact equality. Extremely small ε can amplify floating-point rounding; large ε may poorly approximate a local slope. At a nondifferentiable point (for example ReLU at zero), a finite difference need not agree with the convention used by an autodiff system.
def network_loss(x, y, a, b): hidden = a * x prediction = b * hidden return (prediction - y) ** 2
def gradients(x, y, a, b): hidden = a * x prediction = b * hidden d_prediction = 2 * (prediction - y) d_b = d_prediction * hidden d_hidden = d_prediction * b d_a = d_hidden * x return d_a, d_b
x, y, a, b = 2, 6, 1, 2h = 0.00001g_a, g_b = gradients(x, y, a, b)fd_a = (network_loss(x, y, a+h, b) - network_loss(x, y, a-h, b)) / (2*h)fd_b = (network_loss(x, y, a, b+h) - network_loss(x, y, a, b-h)) / (2*h)print(g_a, g_b)print(round(fd_a, 5), round(fd_b, 5))Losses and optimizers
Mean squared error averages squared residuals for regression. For binary classification a common loss is binary cross-entropy; it penalizes incorrect probability estimates, especially confident errors. Multiclass cross-entropy typically uses logits with a stable log-softmax calculation. A framework may expect raw logits rather than probabilities; check its documented input contract before adding sigmoid or softmax. Backpropagation computes gradients; plain SGD subtracts a scaled gradient estimate, momentum incorporates past updates, and Adam adapts updates using moving estimates of gradients and squared gradients. None guarantees a global minimum for a general neural network.
Common mistakes and practice
Do not change b before calculating the gradient for a. Do not confuse a negative gradient with a negative loss. Deep chains can shrink or amplify gradients, causing vanishing or exploding gradients; initialization, activation choice, normalization, residual connections and sometimes gradient clipping can help, but each has trade-offs. Initialization matters even here: if both a and b start at zero, both gradients are zero despite nonzero loss. Practice: for x = 3, y = 9, w = 2 compute the prediction, squared loss and gradient, then apply a learning rate of 1/18. Expected: 6, 9, −18, new weight 3. Next: organize updates into batches and epochs.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Loss and backpropagation intuition quiz
Check calculations and training decisions.
For L(w)=(w*x−y)², x=2, y=6 and w=1, what is dL/dw?
Why compute both gradients before a simultaneous gradient-descent update of the two-stage model?
For the chapter’s centered finite-difference estimate of a partial derivative, what is perturbed?
For prediction b*a*x and squared loss, what are the gradients with respect to a and b when a=b=0?
Which operation applies parameter changes?