Implement Tanh Activation
Implement Tanh Activation
Implement the Tanh (Hyperbolic Tangent) activation function. Tanh squashes values to the range (-1, 1) and is symmetric around zero.
Tanh Formula:
tanh(x)=ex+e−xex−e−xFunction Arguments
x- Input (scalar, list, or NumPy array)
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.ndarrayof 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
Log in to take notes on this problem
Accepts: array
Implement Tanh Activation
Implement Tanh Activation
Implement the Tanh (Hyperbolic Tangent) activation function. Tanh squashes values to the range (-1, 1) and is symmetric around zero.
Tanh Formula:
tanh(x)=ex+e−xex−e−xFunction Arguments
x- Input (scalar, list, or NumPy array)
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.ndarrayof 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
Log in to take notes on this problem
Accepts: array