Consider a small ReLU network with input x=[1,−1]⊤, hidden layer z(1)=W(1)x with W(1)=[1−1−12], h=ReLU⁡(z(1)), and scalar output y^=w(2)⊤h with w(2)=[2,−1]⊤ (no biases). For target y=3 and squared loss L=12(y^−y)2, what are the exact gradients ∂L∂w(2) and ∂L∂W(1)?

Deep learning, Backpropagation
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can run a forward pass and a matrix backpropagation pass by hand on a ReLU network, including blocking gradients through inactive ReLU units.

Key idea

Compute z(1),h,y^ in the forward pass, then propagate δout=y^−y backwards using δ(1)=(w(2) δout)⊙1[z(1)>0] and ∂L∂W(1)=δ(1)x⊤, where ⊙ is element-wise multiplication and 1[z(1)>0] is the indicator vector (1 where zi(1)>0, 0 otherwise).

Steps

  1. Forward pass: z(1)=[1(1)+(−1)(−1)(−1)(1)+2(−1)]=[2−3], so h=ReLU⁡(z(1))=[20].
  2. Output and loss: y^=2(2)+(−1)(0)=4, and L=12(4−3)2=0.5.
  3. Output gradient: δout=∂L∂y^=y^−y=4−3=1, so ∂L∂w(2)=δout h=[20].
  4. Backpropagate through ReLU: ∂L∂h=w(2) δout=[2−1]. Since z1(1)=2>0 and z2(1)=−3<0, the ReLU mask is [10], giving δ(1)=∂L∂z(1)=[20].
  5. Weight gradient: ∂L∂W(1)=δ(1)x⊤=[20][1−1]=[2−200].

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Read this

Previous in Deep learning: What is the general message-passing neural network (MPNN) update for node v, and how do you prove that relabelling nodes with any permutation matrix P makes a node-level GNN layer permutation-equivariant while global sum or mean pooling makes a graph-level readout permutation-invariant?
Next in Deep learning: For a mini-batch of N examples stacked as rows in X∈RN×d, a two-layer network computes Z=XW1+1b1⊤, H=ϕ(Z) and Y^=HW2+1b2⊤, where W1∈Rd×m, b1∈Rm, W2∈Rm×K, b2∈RK and 1∈RN is the all-ones vector. Given the batch-averaged loss gradient Δ2=∂L∂Y^∈RN×K, what are the gradients with respect to W2,b2,W1,b1, and how do matrix dimensions verify the transposes?