TensorTonic
Problems
Implement Tanh Activation
Activation Functions
Easy

Apply the hyperbolic tangent activation elementwise:

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​

Here, xxx is each input value. Return a NumPy array with the same shape as the input and values in (−1,1)(-1,1)(−1,1).

Loading visualization...

Examples

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

Output: [0.0, 0.761594, -0.761594, 0.995055]

Explanation: Tanh maps zero to zero and maps positive and negative inputs symmetrically toward 1 and -1.

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

Output: [[0.0, 0.761594], [-0.761594, 0.964028]]

Hint 1

Convert the input with np.asarray(x, dtype=float).

Hint 2

Use NumPy's vectorized np.tanh function.

Requirements

  • Apply hyperbolic tangent elementwise
  • Preserve the input shape
  • Return a NumPy array of floating-point values

Constraints

  • x is a nonempty finite numeric list of any shape
  • x contains at most 1,000,000 values
  • Use NumPy only
Try Similar Problems
Implement Sigmoid in NumPyEasyImplement ReLU Activation in NumPyEasyImplement Numerically Stable SoftmaxMediumRNN Step Forward (Tanh Cell)EasyBuild a Mini GRU Cell (Forward Pass)MediumImplement Leaky ReLU (with α)EasyELU ActivationEasySELU ActivationEasyActivation FunctionsEasySoftmax from ScratchEasyDerivatives of Common Activation FunctionsEasyContinuity of Activation FunctionsMedium
Scratchpad

Accepts: array

You must run your code first.
Implement Tanh Activation

Implement Tanh Activation

Activation Functions
Easy

Apply the hyperbolic tangent activation elementwise:

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​

Here, xxx is each input value. Return a NumPy array with the same shape as the input and values in (−1,1)(-1,1)(−1,1).

Loading visualization...

Examples

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

Output: [0.0, 0.761594, -0.761594, 0.995055]

Explanation: Tanh maps zero to zero and maps positive and negative inputs symmetrically toward 1 and -1.

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

Output: [[0.0, 0.761594], [-0.761594, 0.964028]]

Hint 1

Convert the input with np.asarray(x, dtype=float).

Hint 2

Use NumPy's vectorized np.tanh function.

Requirements

  • Apply hyperbolic tangent elementwise
  • Preserve the input shape
  • Return a NumPy array of floating-point values

Constraints

  • x is a nonempty finite numeric list of any shape
  • x contains at most 1,000,000 values
  • Use NumPy only
Try Similar Problems
Implement Sigmoid in NumPyEasyImplement ReLU Activation in NumPyEasyImplement Numerically Stable SoftmaxMediumRNN Step Forward (Tanh Cell)EasyBuild a Mini GRU Cell (Forward Pass)MediumImplement Leaky ReLU (with α)EasyELU ActivationEasySELU ActivationEasyActivation FunctionsEasySoftmax from ScratchEasyDerivatives of Common Activation FunctionsEasyContinuity of Activation FunctionsMedium
Scratchpad

Accepts: array

You must run your code first.
Problems
Loading...
1 / 1