← back

Implement the Tanh Activation Function

#264 · Deep Learning · Easy

⊣ Solve on deep-ml.com

Problem

Implement the Tanh (Hyperbolic Tangent) activation function: tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x)).

Solution

Apply the formula element-wise, using a numerically stable formulation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import math

def tanh(x: float) -> float:
    if x > 20:
        return 1.0
    if x < -20:
        return -1.0
    e_pos = math.exp(x)
    e_neg = math.exp(-x)
    return (e_pos - e_neg) / (e_pos + e_neg)


def tanh_list(values: list[float]) -> list[float]:
    return [round(tanh(v), 6) for v in values]

Explanation

  1. Compute e^x and e^{-x}.
  2. Return (e^x - e^{-x}) / (e^x + e^{-x}).
  3. Output is always in the range (-1, 1).
  4. For very large or very small x, clamp to +1 or -1 for numerical stability.
  5. Tanh is zero-centered (unlike sigmoid), which can help with gradient flow during training.

Complexity

  • Time: O(n) for n elements
  • Space: O(n) for output