Skip to content
Calcrivo

Tanh Calculator

Compute the hyperbolic tangent activation function output for a given input.

Inputs

tanh(x)

0.761594

tanh'(x)

0.419974

Step by step

  1. tanh(x) = (e^x − e^(−x)) / (e^x + e^(−x))

    (e^1 − e^(−1)) / (e^1 + e^(−1))

    = 0.761594

  2. Derivative: 1 − tanh(x)²

    1 − 0.761594²

    = 0.419974

How it works

Tanh squashes real-valued inputs into the range (−1, 1): tanh(x) = (e^x − e^(−x)) / (e^x + e^(−x)), and is simply a rescaled and shifted version of the sigmoid function (tanh(x) = 2·σ(2x) − 1). Its key advantage over sigmoid is being zero-centered, meaning outputs can be negative, which helps keep gradients balanced during training. Like sigmoid, its derivative, 1 − tanh(x)², is at most 1 (at x=0) and shrinks toward 0 for large |x|, so tanh still suffers from vanishing gradients in very deep networks, but it remains a standard choice for RNN/LSTM hidden state activations.

Formulas

Tanh function

tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x))

x
Input value
e
Euler's number (≈2.718)

Tanh derivative

tanh'(x) = 1 - tanh(x)^2

tanh(x)
Tanh output at x

Frequently Asked Questions

How is tanh related to sigmoid?

Tanh is a rescaled sigmoid: tanh(x) = 2σ(2x) − 1, which maps sigmoid's (0,1) output range to (−1,1) — both share the same S-shaped curve, but tanh's zero-centering often makes it converge faster in practice for hidden layers.

Why is being zero-centered an advantage?

When an activation function's outputs are all positive (like sigmoid's), gradients for the next layer's weights tend to all move in the same direction, slowing convergence; tanh's symmetric (−1,1) output allows both positive and negative signals, generally leading to more balanced gradient updates.

Where is tanh commonly used in modern architectures?

Tanh remains standard for the hidden state and gate activations inside LSTM and GRU recurrent cells, and appears in the GELU approximation formula, though it has largely been replaced by ReLU-family functions in standard feedforward and convolutional hidden layers.

You might also need