05
Trust the trace. Check the derivative.
Hand arithmetic, small perturbations and shape checks catch different failures.
Perturb one parameter
A numerical check provides another route to a derivative. Perturb only parameter by a small amount in both directions and recompute the complete forward loss:
Here is the coordinate vector that changes only the selected parameter, unrelated to our residual . At the default smooth point, gives agreement with all five analytic gradients within the lab’s absolute tolerance .
That agreement is separate from the hand-computed values. Both matter: a checker can miss a shared bug if its “numerical” loss reuses the analytic derivative. Our finite-difference path calls the forward loss alone. Gradcheck mechanics explains the comparison. BP-C07
import { DEFAULT_PARAMS, DEFAULT_EXAMPLE,
backward, finiteDifference } from './model.mjs';
console.log(backward(DEFAULT_PARAMS,
DEFAULT_EXAMPLE).gradients);
console.log(finiteDifference(DEFAULT_PARAMS,
DEFAULT_EXAMPLE, { epsilon: 1e-6 }));The expected analytic gradient is . Run the full companion for recorded outputs and assertions. Small final-digit differences in the numerical estimate are floating-point arithmetic.
The corner is different
Set with other defaults unchanged. Now . Perturbing in opposite directions samples different sides of ReLU’s corner. The centered estimate approaches −3; our chosen backward gradient at the corner is zero. This disagreement is expected: there is no ordinary derivative for both methods to recover.
Even away from a corner, a large perturbation may cross it. An excessively small perturbation can lose precision through subtraction. PyTorch’s gradcheck reference warns about nondifferentiable points and notes its default settings target double precision. Our tests exclude corners from derivative-agreement assertions and check the convention separately. BP-C04, BP-C08
When numbers become vectors
For column input , weights and bias , an affine layer gives . Let be an -coordinate column of sensitivities.
| Quantity | Derivative | Shape |
|---|---|---|
| Weights W | ||
| Bias b | ||
| Input x |
The transpose collects contributions from every output coordinate into each input coordinate. The outer product gives one derivative for every weight entry. A gradient has the same shape as its parameter. These equations use one column-vector example; batch dimensions and reductions need explicit treatment. BP-C13
Diagnose a new case
Try this without the lab. Keep the architecture but change values: . A colleague reports “the early-layer gradient is zero, so ReLU must be inactive.” Is that justified? Find and all five parameter gradients. Will every parameter stay fixed under a positive-rate step?
Open the worked diagnosis
The forward value is , so ReLU is active. Branches are 4 and −4. Prediction is 1, residual −2 and loss 2. Contributions are and . They cancel: .
The early-layer zero comes from cancellation, not an inactive activation or a perfect prediction. Readout weights and output bias can still change. Omitting one route would produce an erroneous nonzero upstream gradient. BP-C16 · derived transfer case
When a gradient surprises you, inspect saved forward values, local derivatives, all downstream routes and the loss reduction. Then inspect the optimizer separately. These questions make “the model learned” a sequence of checkable operations.