Tanh, short for hyperbolic tangent, is a smooth, S-shaped activation function that maps any real input to an output between minus one and one. In neural networks, the tanh activation introduces nonlinearity into neurons and gates, and it is a standard building block in artificial intelligence for recurrent architectures such as LSTM and gated recurrent units.
Its defining property is that it is zero-centered and symmetric: negative inputs give negative outputs, positive inputs give positive outputs, and zero maps exactly to zero. This made tanh the preferred hidden-layer activation before ReLU became dominant, and it remains essential wherever a network needs a bounded, signed signal.
How the tanh function is defined
The hyperbolic tangent is defined as tanh(x) = (e^x – e^-x) / (e^x + e^-x). It is an odd function, so tanh(-x) equals -tanh(x), and it approaches its horizontal asymptotes at -1 and +1 as the input grows large in either direction.
Tanh is a rescaled sigmoid function: tanh(x) = 2σ(2x) – 1, where σ is the logistic sigmoid. Both curves have the same shape, but tanh is stretched to twice the output range and centered on zero. Its derivative is 1 – tanh²(x), which is cheap to compute from the forward output and reaches its maximum of 1 at x = 0.
The table below shows how quickly the output saturates and the gradient shrinks as inputs move away from zero.
| Input x | tanh(x) | Derivative 1 – tanh²(x) |
|---|---|---|
| 0 | 0.000 | 1.000 |
| 0.5 | 0.462 | 0.786 |
| 1 | 0.762 | 0.420 |
| 2 | 0.964 | 0.071 |
| 3 | 0.995 | 0.010 |
Beyond an absolute input of about 3, the gradient falls below one percent of its peak. This region is called saturation, and it governs most of the practical behavior of tanh in training.
Why zero-centered output matters
Zero-centered output matters because it keeps the inputs to the next layer balanced around zero. With the sigmoid, every activation is positive, so the gradients of all weights feeding a neuron share the same sign within one update. This forces zigzag optimization paths and slows convergence.
Tanh avoids that bias. Its activations have a mean near zero when inputs are symmetric, which yields better-conditioned gradients and often faster training than the sigmoid in shallow networks. For the same reason, classic guidance recommended normalizing inputs to zero mean when using tanh layers.
Weight initialization was designed around this property. Xavier initialization assumes a roughly linear, zero-centered activation near the origin, which matches tanh closely, and it keeps the variance of activations stable across layers so that units start in the responsive central region.
Tanh compared with other activation functions
Tanh sits between the sigmoid and the ReLU family in its trade-offs. It improves on the sigmoid through zero-centering, but it still saturates on both sides, while ReLU and its variants keep a constant gradient for positive inputs.
| Activation | Output range | Zero-centered | Saturation |
|---|---|---|---|
| Tanh | -1 to 1 | Yes | Both sides |
| Sigmoid function | 0 to 1 | No | Both sides |
| ReLU | 0 to infinity | No | Negative side only |
| Leaky ReLU | Unbounded | Approximately | None |
| GELU | About -0.17 to infinity | Approximately | Negative side, softly |
ReLU also costs less to compute, since it needs no exponentials. That efficiency and its resistance to the vanishing gradient problem explain why ReLU, GELU and SwiGLU replaced tanh in the hidden layers of deep feedforward networks and transformers.
Where tanh is used today
Tanh is used today mainly where a bounded output with a sign is the natural representation. Its most important home is the recurrent cell, where it shapes the candidate state and the exposed hidden state.
| Location | Role of tanh | Why it fits |
|---|---|---|
| LSTM candidate state | Proposes new memory content | Allows both increases and decreases |
| LSTM output | Squashes the cell state | Keeps hidden values bounded |
| Gated recurrent unit | Forms the candidate activation | Signed, stable recurrent signal |
| Policy network output | Maps actions to a fixed range | Matches control limits such as -1 to 1 |
| GAN generator output | Produces pixel values | Matches images scaled to -1 to 1 |
| GELU approximation | Part of the fast formula | Smooth curve close to the exact version |
In recurrent cells, tanh works together with sigmoid gates: the gate decides how much information passes, while tanh decides its direction and magnitude. Because the state is bounded, repeated application over long sequences does not let activations explode.
Limitations and common mistakes with tanh
The central limitation of tanh is the vanishing gradient in deep stacks. Each saturated layer multiplies the backpropagated gradient by a small derivative, so a network with many tanh layers can leave its early layers nearly untrained.
| Symptom | Likely cause | Fix |
|---|---|---|
| Loss stalls early | Units saturated at ±1 | Lower initial weight scale |
| Early layers do not learn | Gradient vanishing through depth | Use ReLU-type layers or residual connections |
| Outputs clipped at range limits | Targets outside -1 to 1 | Rescale targets or change output activation |
| Slow training on raw features | Inputs far from zero | Standardize inputs to zero mean, unit variance |
A further mistake is to use tanh on a classification output layer. Class probabilities need softmax or sigmoid, because tanh values are not valid probabilities and do not sum to one.
In simple terms
Tanh is a small math rule inside a neural network. It takes any number and squeezes it into a range from minus one to one. Small numbers pass through almost unchanged. Very large numbers get flattened near the edges.
Think of a volume knob with firm stops at both ends. You can turn it up or down, but never past the limits. This keeps signals in the network calm and balanced. The catch is that, at the stops, turning further changes nothing, so learning slows down.
A practical example
A robotics team trains a reinforcement learning agent to steer a small delivery robot. The steering command must stay between full left and full right, encoded as -1 and +1. The team places a tanh activation on the policy network output, so every action the network produces is automatically inside the safe range.
In an early version, the actor network started with large initial weights, and almost every output was pinned at exactly -1 or +1. The robot only jerked hard left or hard right, and learning stalled because the tanh gradient was nearly zero. After the engineers reduced the initial weight scale of the final layer and standardized the sensor inputs, outputs spread across the full range and the robot learned smooth turns within a few hours of simulated training.