#418BPTT — the tanh backward factorEasyRNNNeural NetworksCalculus
BPTT — the tanh backward factor
Background
Every BPTT step multiplies by the local derivative of the cell's nonlinearity. For tanh, the derivative is expressible from the activation itself:
(In the lesson's training loop this is the dzh = dh * (1 - h**2) line.) Because always, this factor is one source of vanishing gradients.
Problem statement
Implement tanh_grad(h) returning elementwise, where h is the tanh activation.
Input
h— array-like (or scalar) of tanh activations, each in .
Output
Returns an np.ndarray (same shape) of local gradients .
Examples
Example 1 — steepest at 0
Input: h = 0
Output: 1.0
Example 2
Input: h = [0, 0.5, -0.5]
Output: [1. 0.75 0.75]
Constraints
- Use the cached form
1 - h**2— no need to recompute tanh. - Return an
np.ndarray.
Notes
- The derivative peaks at 1 (when ) and collapses toward 0 as the unit saturates () — exactly where gradient flow dies.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example
- •Vectorised sample
- •Example identity match