TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
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)=ex−e−xex+e−x\tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} tanh(x)=ex+e−xex−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
Sigmoid NumpyRelu ActivationRnn Step ForwardGru Cell ForwardSoftmax Function

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

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)=ex−e−xex+e−x\tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} tanh(x)=ex+e−xex−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
Sigmoid NumpyRelu ActivationRnn Step ForwardGru Cell ForwardSoftmax Function

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.