#418BPTT — the tanh backward factorEasy

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 h=tanh(z)h = \tanh(z) itself:

hz=1tanh(z)2=1h2\frac{\partial h}{\partial z} = 1 - \tanh(z)^2 = 1 - h^2

(In the lesson's training loop this is the dzh = dh * (1 - h**2) line.) Because 1h211 - h^2 \le 1 always, this factor is one source of vanishing gradients.

Problem statement

Implement tanh_grad(h) returning 1h21 - h^2 elementwise, where h is the tanh activation.

Input

  • h — array-like (or scalar) of tanh activations, each in (1,1)(-1, 1).

Output

Returns an np.ndarray (same shape) of local gradients 1h21 - h^2.

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 h=0h = 0) and collapses toward 0 as the unit saturates (h1|h|\to1) — 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