Problems
Loading...
1 / 1

Implement Tanh Activation

Activation Functions
Easy

Implement the Tanh (Hyperbolic Tangent) activation function. Tanh squashes values to the range (-1, 1) and is symmetric around zero.

Tanh Formula:

tanh(x)=exexex+ex\tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}

Function Arguments

  • x - Input (scalar, list, or NumPy array)
Loading visualization...

Examples

Input: [0, 1, -1, 3]

Output: [0.0, 0.7616, -0.7616, 0.9951]

Symmetric around zero, bounded between -1 and 1

Input: 0.0

Output: [0.0]

Scalar input returns 1D array with shape (1)

Input: [[0, 1], [-1, 2]]

Output: [[0.0, 0.7616], [-0.7616, 0.9640]]

Works element-wise on multi-dimensional arrays

Requirements

  • Return np.ndarray of floats
  • Handle scalar, list, and NumPy array inputs
  • Vectorized implementation only (no loops)
  • Preserve input shape

Constraints

  • Time limit: 200ms; Memory ≤ 64MB
Try Similar Problems