The Whole Trip Back

BackpropagationHard+30 XP+30

The Whole Trip Back

This is the backward pass on a real, if tiny, network, from start to finish: run it forward, score it, then find a gradient for every weight and every bias in it.

The network. ddd inputs feed a hidden layer of hhh neurons with ReLU, which feeds an output layer of kkk neurons with no activation. Both layers are dense and use a connection table: W[i][j] is the weight on the wire from unit i of the layer below to neuron j.

parametersizeholds
W1d×hd \times hd×hW1[i][j]: input iii → hidden neuron jjj
b1hhhone bias per hidden neuron
W2h×kh \times kh×kW2[j][m]: hidden neuron jjj → output mmm
b2kkkone bias per output

On one example xxx, the forward pass computes

zj=∑ixi W1[i][j]+b1[j],aj=max⁡(0,zj),y^m=∑jaj W2[j][m]+b2[m]z_j = \sum_i x_i \, \text{W1}[i][j] + \text{b1}[j], \qquad a_j = \max(0, z_j), \qquad \hat y_m = \sum_j a_j \, \text{W2}[j][m] + \text{b2}[m]zj​=∑i​xi​W1[i][j]+b1[j],aj​=max(0,zj​),y^​m​=∑j​aj​W2[j][m]+b2[m]

The loss is the mean squared error over the whole batch. Square the error of every output on every example, add them up, and divide by how many there were:

L=1n k∑e=1n∑m=1k(y^e,m−ye,m)2L = \frac{1}{n\,k} \sum_{e=1}^{n} \sum_{m=1}^{k} \left(\hat y_{e,m} - y_{e,m}\right)^2L=nk1​∑e=1n​∑m=1k​(y^​e,m​−ye,m​)2

ReLU's slope is 111 where z>0z > 0z>0 and 000 where z≤0z \le 0z≤0, including at exactly z=0z = 0z=0.

Task: write backward_pass(inputs, targets, W1, b1, W2, b2), returning

[loss, dW1, db1, dW2, db2]

where loss is LLL and each gradient holds ∂L∂(⋅)\dfrac{\partial L}{\partial(\cdot)}∂(⋅)∂L​ for every entry of the parameter it is named after, laid out exactly like that parameter (dW1 is d×hd \times hd×h, db2 has kkk entries, and so on).

  • inputs has n≥1n \ge 1n≥1 examples of ddd values; targets has the same nnn rows of kkk values.
  • Round every number to 4 decimal places, and only at the end. A table with a single row or a single column is still a list of lists.

For the first test, the forward pass gives z=[1.0, 0.5]z = [1.0,\ 0.5]z=[1.0, 0.5], so a=[1.0, 0.5]a = [1.0,\ 0.5]a=[1.0, 0.5] and y^=2.0\hat y = 2.0y^​=2.0 against a target of 1.01.01.0. The loss is therefore 1.01.01.0. The gradients are yours to find.

Once these gradients exist, the update is one line per parameter: step each one against its own gradient. They all move together, because every gradient here came from the same forward pass, with the weights as they stood then. A classic bug in hand-written training loops updates W2 first and then sends the error back through the new W2. That hands the hidden layer blame for a network that never made the prediction.

Examples

inputs = [[1.0, 2.0]]
targets = [[1.0]]
W1 = [[0.5, -1.0], [0.25, 0.5]]
b1 = [0.0, 0.5]
W2 = [[2.0], [-1.0]]
b2 = [0.5]
returns [1.0, [[4.0, -2.0], [8.0, -4.0]], [4.0, -2.0], [[2.0], [1.0]], [2.0]]
inputs = [[1.0, -1.0], [0.5, 2.0]]
targets = [[0.3, 0.7], [0.1, 0.9]]
W1 = [[1.0, -0.5, 0.25], [0.5, 1.0, -1.0]]
b1 = [0.0, 0.5, 0.25]
W2 = [[0.6, -0.2], [-0.8, 1.0], [0.4, 0.7]]
b2 = [0.5, -0.45]
returns [0.465, [[0.245, 0.25, 0.15], [-0.77, 1.0, -0.15]], [0.14, 0.5, 0.15], [[-0.1, 0.4], [-0.5625, 0.675], [0.825, -0.15]], [0.3, 0.2]]

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