Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Neural networks

Backpropagation through a two-layer network

Hard · Target 30 minutes · Python and NumPy

For the network A = XW1 + b1, H = max(0, A), Z = HW2 + b2 and the loss L = −(1/N) Σn log softmax(Zn)yn, return [dW1, db1, dW2, db2], the gradients of L with respect to each parameter, each with the same shape as the parameter. y holds integer labels in 0…C−1.

The tests are generated so that no hidden pre-activation is exactly zero.

Examples

Input X = [[1, 2]], y = [0], W1 = [[1, 0], [0, 1]], b1 = [0, 0], W2 = [[1, 0], [0, 1]], b2 = [0, 0]

Output [[[-0.7311, 0.7311], [-1.4621, 1.4621]], [-0.7311, 0.7311], [[-0.7311, 0.7311], [-1.4621, 1.4621]], [-0.7311, 0.7311]]

One example. dZ = P − one_hot(0); everything else follows from it.

Input X = [[-1, -1]], y = [1], W1 = [[1, 0], [0, 1]], b1 = [0, 0], W2 = [[1, 0], [0, 1]], b2 = [0, 0]

Output [[[0, 0], [0, 0]], [0, 0], [[0, 0], [0, 0]], [0.5, -0.5]]

Both hidden units are off, so no gradient reaches W1 or b1.

Input X = [[1, 0], [0, 1]], y = [0, 1], W1 = [[2, -1], [-1, 2]], b1 = [0.5, 0.5], W2 = [[1, -1], [-1, 1]], b2 = [0, 0.1]

Output [[[-0.0074, 0], [0, -0.0061]], [-0.0074, -0.0061], [[-0.0092, 0.0092], [0.0076, -0.0076]], [-0.0007, 0.0007]]

Two examples: the 1/N averages their contributions.

Constraints

  • 1 ≤ N ≤ 256, 1 ≤ D, H ≤ 128, 2 ≤ C ≤ 32
  • Checked against central finite differences to a tolerance of 10⁻⁶

Hints

Hint 1

Run the forward pass and keep A, H and P. The backward pass needs all three.

Hint 2

Start with dZ = (P − Y)/N. Then dW2 = Hᵀ dZ, db2 = dZ summed over rows, and dH = dZ W2ᵀ.

Hint 3

Through the ReLU, dA = dH where A > 0 and 0 elsewhere. Then dW1 = Xᵀ dA and db1 = dA summed over rows.

Where candidates go wrong

  • Masking with H > 0 instead of A > 0. They agree here, but only because ReLU(A) > 0 exactly when A > 0; for leaky ReLU they do not.
  • Forgetting the 1/N, so that every gradient is N times too large.
  • Summing db over the wrong axis and returning a vector of length N.
  • Transposing the wrong factor: dW2 must have W2's shape (H, C). Hᵀ dZ is the only product that does.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

The defining question of ML research interviews. Interviewers want each gradient stated with its shape before it is coded, and they will ask how you know your answer is right. The answer they hope for is a gradient check.

The theory behind it is in the lesson Backpropagation from scratch.


Keep going