ReLU Backward Pass
ReLU Backward Pass
The ReLU activation function is:
Its derivative is a step function:
During backpropagation we apply the chain rule: multiply the upstream gradient d_out by this local derivative element-wise:
Gradients only flow through neurons that were active (positive) during the forward pass. Neurons where receive zero gradient — the so-called dead neuron problem with ReLU.
Your task:
Implement relu_backward(x, d_out) that returns the gradient of the loss with respect to the pre-activation input x.
Example Tests
Mixed signs: gradient flows only where x > 0
Input: {"x":[1,-1,2],"d_out":[0.5,0.5,0.5]}
Expected: [0.5,0,0.5]
All negative: gradient is zero everywhere (dead neurons)
Input: {"x":[-1,-2,-3],"d_out":[1,1,1]}
Expected: [0,0,0]
All positive: upstream gradient passes through unchanged
Input: {"x":[1,2,3],"d_out":[2,3,4]}
Expected: [2,3,4]