#157Backprop — all four gradients for one hidden layerMediumNeural NetworksCalculus
Backprop — all four gradients for one hidden layer
Background
The full backward pass for the canonical net (sigmoid everywhere, MSE loss) is four moves. Given the cached forward values and W2:
Here W2 has shape (h, o), so W2 @ delta2 is the (h,) back-propagated error.
Problem statement
Implement backprop(x, y, a1, a2, W2) returning (dW1, db1, dW2, db2).
Input
x— input vector(i,).y— true label(o,).a1— cached hidden activation(h,).a2— cached prediction(o,).W2— output weights, shape(h, o).
Output
Returns (dW1, db1, dW2, db2) with shapes (i, h), (h,), (h, o), (o,) — each matching its parameter.
Examples
Example
Input: x = [0.5, -0.5], y = [1.0], a1 = [0.6, 0.4], a2 = [0.7], W2 = [[0.5], [-0.5]]
Output: dW2 = [[-0.0378], [-0.0252]], db2 = [-0.063],
dW1 = [[-0.00378, 0.00378], [0.00378, -0.00378]], db1 = [-0.00756, 0.00756]
Constraints
- Use the four formulas above, in order.
- Each gradient must match the shape of the parameter it updates.
dW2 = outer(a1, delta2),dW1 = outer(x, delta1).
Notes
- Plug these into and you have one training step — the whole learning loop in a handful of lines.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Worked example - dW2 for the 2-1 net
- •Reference db2 on a 3-4-2 net
- •Sample dW1 for a 2-3-1 net