pre is [1.5, -0.5, 2.0, -3.0] and dh is [0.1, 0.2, 0.3, 0.4]. What is dh * (pre > 0)?
Pick one to see what it prints.
dh * (pre > 0) is right. The gradient passes where pre was positive and stops where it wasn't. Try the other two masks:
Where the gradient gets throughInteractive
unit 0unit 1unit 2unit 3
pre1.5-0.52-3
h1.5020
pre > 0onoffonoff
dh0.10.20.30.4
dpre0.100.30
The gradient passes where pre was positive and stops where it wasn't, so units 1 and 3 pass nothing back.
ReLU's local derivative is 1 where its input was positive and 0 where it wasn't. So dpre = dh * (pre > 0).
At exactly 0 the slope isn't defined, and pre > 0 uses 0 there, the usual choice. A real-valued pre is almost never exactly 0.0, so the choice rarely matters.
The third line is the trap. ReLU's output is never negative, so h >= 0 is True everywhere, and nothing gets masked.
h > 0 happens to equal pre > 0 for ReLU, but the habit breaks on the next activation you meet. GELU dips below zero and comes back up. Its output is -0.10 at both -0.25 and -1.5, where its slopes are +0.30 and -0.13. The output can't tell you which slope applies. Save the input in the forward pass and mask with it.
In the editor, write relu_backward(dh, pre): the gate, with the mask taken from pre.
Pick an answer above to see the rest
Instructions
1
Write relu_backward(dh, pre): the gradient at pre, given dh, the gradient at h = np.maximum(pre, 0).
2
Take the mask from pre, and pass nothing where pre is exactly 0.