268. Backpropagate through a 2-layer tanh net for regression and binary classification
Why it matters Interviewers still ask for a hand-derived 2-layer backward pass, and it is the base case for debugging custom layers.
Doing backprop by hand for one small network is the fastest way to trust what autograd does for you.
Write two_layer_grads(X, y, W1, b1, W2, b2, task). X is (n, d), y is (n, 1), W1 (d, h), b1 (h,), W2 (h, 1), b2 (1,). Forward: H = tanh(X @ W1 + b1), out = H @ W2 + b2. If task is "regression", loss = mean over samples of 0.5 * (out - y)^2. If task is "binary", p = sigmoid(out) and loss = mean binary cross-entropy of p against y. Return a dict with keys "loss", "dW1", "db1", "dW2", "db2" (gradients of the mean loss, same shapes as the parameters, loss a float).
def two_layer_grads(X, y, W1, b1, W2, b2, task)Example 1
Input: X = [[1, 2]], y = [[1]], W1 = [[0.5, -0.5], [0.25, 0]], b1 = [0, 0], W2 = [[1], [1]], b2 = [0], task "regression"
Output: loss 0.2454 (rounded) and db2 = [-0.7005] (rounded)
Example 2
Input: R(two_layer_grads([[1.0, 2.0], [-1.0, 0.5]], [[1.0], [0.0]], [[0.5, -0.5], [0.25, 0.1]], [0.1, 0.0], [[1.0], [-2.0]], [0.2], 'binary'))
Output: {'loss': 0.2409, 'dW1': [[-0.1492, 0.3474], [-0.0019, 0.216]], 'db1': [0.088, -0.0356], 'dW2': [[-0.1025], [0.0888]], 'db2': [0.0426]}
Example 3
Input: R(two_layer_grads([[0.0, 1.0], [2.0, -1.0], [1.0, 1.0]], [[0.5], [-1.0], [2.0]], [[0.3, 0.2], [-0.4, 0.6]], [0.0, -0.1], [[0.7], [-0.3]], [0.1], 'regression'))
Output: {'loss': 1.3724, 'dW1': [[-0.1597, -0.1784], [-0.8262, 0.3573]], 'db1': [-0.489, 0.0424], 'dW2': [[0.6101], [-0.7244]], 'db2': [-0.4117]}
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 3 tests to be accepted.
Hint 1
Both tasks give the same output gradient shape: (out - y) / n for MSE with the 0.5 factor, and (p - y) / n for sigmoid plus BCE.
Hint 2
Write the forward pass first and keep H.
Hint 3
Get dout for each task; they have the same form.
Hint 4
tanh'(z) = 1 - tanh(z)^2, and you already have tanh(z) in H.
Common mistakes
- Forgetting to divide by n because the loss is a mean.
- Using the wrong tanh derivative.
- Summing db over the wrong axis.
Understand the idea
Backprop is the chain rule run backwards, one layer at a time. Picture a factory line: when the final product is off, each station gets blamed in proportion to how much it changed the product.
Tiny example with one sample: x = [1, 2], H = tanh([1, -0.5]) = [0.7616, -0.4621], out = 0.2995, y = 1. With loss 0.5 (out - y)^2, dout = out - y = -0.7005, so db2 = -0.7005 and dW2 = H times dout. Then each hidden unit gets dout * W2, multiplied by 1 - H^2.
dout = (out - y) / n for MSE, or (p - y) / n for sigmoid plus BCE. dW2 = H^T dout, dH = dout W2^T, dZ = dH * (1 - H^2), dW1 = X^T dZ, db = column sums
The same four lines appear in every dense layer of every network, including the MLP blocks inside transformers.
Shapes and data flow
X (n,d) -> Z1, H (n,h) -> out (n,1); dZ (n,h); dW1 (d,h); dW2 (h,1)
Topics
Companies5
- Anthropic a candidate reported this question ML Coding at Each Company (CircleCoder)
- DeepMind the topic appears in interview reports Google DeepMind Onsite Interview: Every Round, What It Tests (SpaceComplexity)
- Meta a candidate reported this question Timed ML screen: concept questions then classic models from scratch (Land the Lab)
- NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan
- OpenAI a candidate reported this question Coding Exercises & ML System Design: Real AI Interview Questions (landedjobs)
From public interview reports and guides. Not affiliated with or endorsed by these companies.