ML0029 Tanh

What are the advantages and disadvantages of using the tanh activation function?

Answer

The tanh (hyperbolic tangent) activation squashes its input into the range (-1, 1) with a smooth S-shape. Its headline advantage over sigmoid is that its output is zero-centered: activations average near zero, so the gradients flowing into the next layer do not systematically push all weights in one direction, which makes optimization easier and convergence faster. It is also smooth and infinitely differentiable, and its maximum derivative of 1 (versus sigmoid’s 0.25) gives stronger gradients near the active region. The disadvantages are the flip side of saturation: for large positive or negative inputs the function flattens and its derivative approaches zero, so deep stacks of tanh units suffer the vanishing gradient problem; and like sigmoid it requires exponentials, making it computationally more expensive than ReLU. Because its output is not in (0, 1), it is also unsuitable as a probability output; its modern niche is mainly inside RNN/LSTM cell dynamics rather than feedforward hidden layers.

(1) Zero-Centered: Output range (-1, 1) centers activations, easing optimization versus sigmoid’s all-positive outputs.
(2) Stronger Gradient: Maximum derivative 1 at the origin (4× sigmoid’s), but it still saturates in the tails.
(3) Costs: Vanishing gradients in deep stacks, exponential computation, and no probability interpretation of the output.

Tanh curve between minus one and one with its derivative peaking at one and vanishing in the tails

Figure 1: Tanh and its derivative 1 - \tanh^2(x): the zero-centered S-curve has a healthy gradient (peak 1) only in the narrow central region; in both tails the derivative collapses toward zero.

Mathematical Formulation:
\tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} = 2\,\sigma(2x) - 1
\frac{d}{dx}\tanh(x) = 1 - \tanh^2(x) \in (0, 1]

Where:

  • x is the neuron’s pre-activation; \sigma(\cdot) is the logistic sigmoid, showing tanh is just a rescaled sigmoid.
  • 1 - \tanh^2(x) is the derivative: maximal (1) at x = 0, decaying toward 0 as |x| grows, the saturation that drives vanishing gradients.

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *