Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Backpropagation through an activation function is a core step in training neural networks, where you compute how the loss changes with respect to the pre-activation values. In this problem you’ll implement the backward pass for a common activation so gradients can flow from later layers to earlier ones.
Implement the function
Rules:
activation == "relu", use ( g'(z) = 1 ) when ( z > 0 ) else ( 0 ).activation == "sigmoid", use ( \sigma(z) = \frac{1}{1 + e^{-z}} ) and ( \sigma'(z) = \sigma(z)\bigl(1-\sigma(z)\bigr) ).Output:
| Argument | Type |
|---|---|
| Z | np.ndarray |
| dA | np.ndarray |
| activation | str |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Return an array
Use NumPy only; no DL frameworks
Elementwise ops; preserve (m,n) shape
Use the chain rule elementwise: dZ = dA * g_prime(Z); keep shapes (m, n) aligned.
For ReLU backward, build a mask from Z: derivative is 1 where Z > 0 else 0, then multiply by dA.
For sigmoid backward, compute s = 1/(1+exp(-Z)) first, then g'(Z) = s*(1-s).