What Is the Hyperbolic Tangent (Tanh) Activation Function Used For

What is the hyperbolic tangent (tanh) activation function used for

Before ReLU took over the deep learning world, tanh was one of my go-to activation functions, especially for recurrent neural networks. I remember being confused about why tanh was preferred over sigmoid in certain layers until I actually plotted both functions side by side and looked at their outputs and gradients. Once I saw the zero-centered output of tanh compared to sigmoid’s always-positive output, the reasoning clicked immediately. Let me walk you through the same realization.

What Problem Does Tanh Solve?

Activation functions exist to introduce non-linearity into neural networks — without them, stacking multiple layers would be mathematically equivalent to a single linear layer, no matter how deep the network is. Tanh is one of the classic non-linear activation functions, and it’s especially useful when I want my activations to be zero-centered, which helps with the dynamics of gradient descent in certain architectures like RNNs and LSTMs.

The Mathematical Definition

The hyperbolic tangent function is defined as:

$$\tanh(x) = \frac{e^{x} – e^{-x}}{e^{x} + e^{-x}}$$

It can also be written in terms of the sigmoid function:

$$\tanh(x) = 2\sigma(2x) – 1$$

where $\sigma$ is the sigmoid function. This relationship is worth remembering — it shows that tanh is really just a rescaled and shifted version of sigmoid.

Key Properties

The Derivative of Tanh

The derivative of tanh, which I need for backpropagation, has a wonderfully simple closed form:

$$\tanh'(x) = 1 – \tanh^2(x)$$

This is convenient computationally because if I’ve already computed $\tanh(x)$ in the forward pass, I can compute the derivative during the backward pass without needing to recompute exponentials.

A Worked Numerical Example

Let’s compute $\tanh(1)$:

$$\tanh(1) = \frac{e^1 – e^{-1}}{e^1 + e^{-1}} = \frac{2.718 – 0.368}{2.718 + 0.368} = \frac{2.350}{3.086} \approx 0.762$$

The derivative at this point:

$$\tanh'(1) = 1 – (0.762)^2 = 1 – 0.581 = 0.419$$

Compare this to $\tanh(3) \approx 0.995$, where the derivative is $1 – 0.990 = 0.010$ — much smaller, illustrating the saturation problem I’ll discuss below.

Tanh vs Sigmoid: Why Zero-Centering Matters

PropertySigmoidTanh
Range$(0, 1)$$(-1, 1)$
Zero-centeredNoYes
Formula$\frac{1}{1+e^{-x}}$$\frac{e^x – e^{-x}}{e^x + e^{-x}}$
Derivative$\sigma(x)(1-\sigma(x))$$1 – \tanh^2(x)$
Max gradient0.251.0

Sigmoid’s output is always positive, which means that during backpropagation, the gradients for weights feeding into a sigmoid-activated neuron all tend to have the same sign. This causes zig-zagging during gradient descent, which can slow convergence. Tanh’s zero-centered output avoids this issue, which is one of the main reasons it was historically preferred over sigmoid in hidden layers.

Visualizing Where Tanh Fits in a Network

flowchart TD
    A[Input x] --> B[Linear Transformation: Wx + b]
    B --> C["Tanh Activation: tanh(z)"]
    C --> D[Output in Range -1 to 1]
    D --> E[Passed to Next Layer or Loss Function]
    D --> F["Gradient: 1 - tanh^2(z)"]
    F --> G[Backpropagation]

Where Tanh Is Commonly Used

  1. Recurrent Neural Networks (RNNs) and LSTMs: The candidate cell state and hidden state computations in vanilla RNNs and LSTMs traditionally use tanh, partly because its bounded, zero-centered output helps regulate the magnitude of information flowing through the recurrent connections over many time steps.
  2. Generative Adversarial Networks (GANs): The generator’s output layer often uses tanh when images are normalized to the range $[-1, 1]$.
  3. Older feedforward architectures: Before ReLU became standard, tanh was a common hidden-layer activation in fully connected networks.
  4. Reinforcement learning policy networks: Continuous action outputs are sometimes squashed with tanh to bound them within a valid action range.

Code Example

PyTorch

import torch
import torch.nn as nn

x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
tanh_output = torch.tanh(x)
print(f"Tanh output: {tanh_output}")

# As a layer in a model
model = nn.Sequential(
    nn.Linear(10, 32),
    nn.Tanh(),
    nn.Linear(32, 1)
)

# Inside an LSTM cell (built-in, uses tanh internally)
lstm = nn.LSTM(input_size=10, hidden_size=20, batch_first=True)

TensorFlow / Keras

import tensorflow as tf

x = tf.constant([-2.0, -1.0, 0.0, 1.0, 2.0])
tanh_output = tf.math.tanh(x)
print(f"Tanh output: {tanh_output.numpy()}")

model = tf.keras.Sequential([
    tf.keras.layers.Dense(32, activation='tanh', input_shape=(10,)),
    tf.keras.layers.Dense(1)
])

# GAN generator example with tanh output for images normalized to [-1, 1]
generator_output = tf.keras.layers.Dense(784, activation='tanh')

Advantages of Tanh

Disadvantages and Limitations

Tanh vs Other Activation Functions

ActivationRangeZero-CenteredVanishing Gradient RiskCommon Use
Tanh$(-1, 1)$YesYes (at extremes)RNNs, LSTMs, GAN generators
Sigmoid$(0, 1)$NoYesBinary classification output, gates in LSTMs
ReLU$[0, \infty)$NoNo (but “dying ReLU” possible)CNNs, feedforward networks
Leaky ReLU$(-\infty, \infty)$NoReducedDeep networks avoiding dying ReLU
Softmax$(0,1)$, sums to 1NoN/AMulti-class output layer

Real-World Use Cases

  1. Speech and audio modeling — LSTMs using tanh gates for time-series audio processing.
  2. Language modeling — earlier RNN/LSTM-based language models relied heavily on tanh activations in their recurrent cells.
  3. Image generation — GAN generators commonly use tanh in the output layer when images are scaled to $[-1, 1]$.
  4. Financial time-series prediction — RNNs and LSTMs applied to stock or economic data often use tanh in their internal gating mechanisms.
  5. Robotics and control systems — bounding continuous control outputs to a symmetric range using tanh.

Best Practices

Summary

The tanh activation function squashes inputs into the range $(-1, 1)$ and is zero-centered, which historically made it a better choice than sigmoid for hidden layers. It remains a staple inside recurrent architectures like LSTMs and GRUs, and in generator networks for GANs, thanks to its bounded, symmetric output. However, like sigmoid, it suffers from vanishing gradients at saturation, which is why ReLU and its variants have largely replaced it in deep feedforward and convolutional networks. Knowing where tanh still shines — recurrent cells, bounded outputs, symmetric normalization — helps you use it deliberately rather than by default.

References

Exit mobile version