Trust, but Nudge

BackpropagationMedium+20 XP+20

Trust, but Nudge

A hand-written backward pass is easy to get subtly wrong. A factor of 222 goes missing, a ReLU gate is forgotten, a table is read the wrong way round. None of these crash. Training just goes quietly worse, and nothing tells you why.

The standard defence is to audit backprop with the crude method it replaced: nudge one weight and watch what the loss does. That costs two loss evaluations per parameter, far too slow to train with. It is completely independent of the backward pass, though, which is exactly what an audit needs.

The nudge. For parameter iii, hold every other parameter where it is and evaluate the loss twice: once with wiw_iwi​ nudged up by ε\varepsilonε, once nudged down by ε\varepsilonε. The numerical gradient is

gi=L(…, wi+ε, … )  −  L(…, wi−ε, … )2εg_i = \frac{L(\dots,\ w_i + \varepsilon,\ \dots) \;-\; L(\dots,\ w_i - \varepsilon,\ \dots)}{2\varepsilon}gi​=2εL(…, wi​+ε, …)−L(…, wi​−ε, …)​

The comparison. Measure how far the claimed gradient cic_ici​ is from gig_igi​ with the relative error

ri=∣ci−gi∣max⁡(∣ci∣, ∣gi∣)r_i = \frac{|c_i - g_i|}{\max\left(|c_i|,\ |g_i|\right)}ri​=max(∣ci​∣, ∣gi​∣)∣ci​−gi​∣​

taking ri=0r_i = 0ri​=0 when cic_ici​ and gig_igi​ are both exactly 000. Parameter iii is flagged when ri>tolr_i > \text{tol}ri​>tol.

Task: write gradient_check(loss_fn, params, claimed, eps, tol).

  • loss_fn takes a list of parameter values, in the same order as params, and returns the loss as a number.
  • params holds the current parameter values, and claimed holds the gradient a backward pass reported for each one.
  • eps is ε\varepsilonε and tol is the flagging threshold. Both are positive.
  • Return [numerical, flagged]. numerical is the list of gig_igi​, each rounded to 4 decimal places. flagged lists the indices of the flagged parameters, counting from 000, in increasing order. Flag using the unrounded gig_igi​.

In the tests, loss_fn is a small network's loss on fixed data, written as a lambda. For example

is a single ReLU neuron with weight p[0] and bias p[1], seeing the input 1.51.51.5. Its output is multiplied by an output weight p[2] and scored by squared error against a target of 2.02.02.0.

Framework authors test their own backward passes this way. They run the check once, on a tiny network, and then switch it off. A layer with ten thousand weights would need twenty thousand loss evaluations to check a single step, which is exactly the cost that one backward pass exists to avoid.

Examples

loss_fn = lambda p: ((p[0] * 1.0 + p[1] - 3.0) ** 2 + (p[0] * 2.0 + p[1] - 4.0) ** 2) / 2
params = [1.0, 0.5]
claimed = [-4.5, -1.5]
eps = 0.001
tol = 0.0001
returns [[-4.5, -3.0], [1]]
loss_fn = lambda p: (max(0.0, 1.5 * p[0] + p[1]) * p[2] - 2.0) ** 2
params = [0.8, -0.2, 1.5]
claimed = [-2.25, -1.5, -1.0]
eps = 0.01
tol = 0.0001
returns [[-2.25, -1.5, -1.0], []]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run