TL;DR
The hyperbolic tangent, written , takes any real number and returns a value strictly between and . Its graph is an S-shape like the sigmoid, but it passes through the origin: . It is an odd function, so a negative input gives an output of the same size with the opposite sign.
Where you'll see it: as the squashing step inside recurrent networks, in the content path of LSTM and GRU cells, and in attention scores that combine a query and a key.
Intuition
Think of as a dimmer with a limit. Small inputs pass through almost unchanged. Near zero the curve is nearly a straight line of slope 1. Large inputs get compressed: once the input reaches about 3, the output is within half a percent of its ceiling of 1, and extra input adds almost nothing.
It exists because a neural unit computes a weighted sum, which can be any real number. If every layer only did weighted sums, a stack of layers would collapse into one linear map, no matter how deep. A nonlinearity between layers breaks that collapse. Tanh is one such nonlinearity. It keeps values bounded, so repeated use does not blow up, and because it is centred at zero, its outputs tend to average near zero. That keeps the next layer's inputs balanced.
- tanh(x)
- y = x
The math
Here is any real number and is Euler's number (the exponential function with base ).
Multiplying the top and bottom by gives an equivalent form that is often easier to evaluate:
Range, limits and symmetry
As grows large and positive, shrinks to zero and approaches . As grows large and negative, approaches . The function is strictly increasing, so it never reaches either limit.
It is an odd function. Replacing with swaps the sign of the numerator and leaves the denominator unchanged, so
In particular .
Relation to the sigmoid
The sigmoid is . Substituting :
Multiply the top and bottom by and the right-hand side becomes . So
Tanh is the sigmoid squeezed horizontally by a factor of two, stretched vertically by two, and shifted down by one. That is why it has the same S-shape with a range of width 2 centred on 0 instead of width 1 centred on .
The derivative
Write and . Then and . The quotient rule gives
So the slope at any point depends only on the output at that point. The slope is at and shrinks toward as grows. This is the fact that matters most in training, because backpropagation multiplies these slopes together (Backpropagation).
- tanh(x)
- derivative
Worked examples
- Compute and .
- Numerator: .
- Denominator: .
- Divide: .
So . An input of 1 is squashed to about three quarters of the maximum output.
Take one neuron with weight , input and bias . It computes where .
- Pre-activation: .
- Compute and .
- Numerator: . Denominator: .
- Output: .
- Slope at this input: .
The neuron outputs 0.664. A small change in would change the output by about 0.56 times as much. That is a healthy slope, so the neuron is still learning.
Take , using .
- Compute .
- Add one: .
- Divide: .
- Output: .
- Slope: .
The output is almost exactly 1, and the slope is about 0.0013. Compared with the slope of 1 at zero, the gradient has shrunk by a factor of about 750. A unit stuck here passes almost no learning signal backwards.
Tradeoffs and failure modes
Tanh is a good choice when you want outputs that are bounded and centred on zero. Its main weakness is saturation: for large the slope is almost zero, so gradients vanish. Its maximum slope is 1, compared with 0.25 for the sigmoid, so a tanh chain loses gradient more slowly than a sigmoid chain. It still loses it.
| Situation | Problem | Better choice |
|---|---|---|
| Deep feed-forward hidden layers | Units saturate, so early layers stop learning | Rectified Linear Unit (ReLU) or a variant |
| Output must be a probability in (0, 1) | Tanh gives values in (-1, 1) | [[sigmoid-function |
| Recurrent hidden state | Saturation is common over long sequences | Keep tanh, use gated cells ([[gated-recurrent-unit |
| Inputs that are not normalised | Large pre-activations push units into saturation | Normalise inputs or use Batch Normalization |
Initialisation matters too. Xavier initialisation chooses weight variance so that pre-activations stay in the near-linear region of tanh at the start of training.
History
Hyperbolic functions were studied in the 18th century, when mathematicians looked for hyperbola analogues of the circular trigonometric functions. Their modern definitions and notation came out of that work.
Tanh entered neural networks in the 1990s. The 1986 backpropagation paper by Rumelhart, Hinton and Williams used the logistic sigmoid. Later, LeCun and colleagues in their 1998 "Efficient BackProp" argued that a scaled tanh, which is zero-centred, trains faster than the sigmoid. The 1997 LSTM design by Hochreiter and Schmidhuber used tanh for the candidate content and sigmoid for the gates. Tanh stayed common in hidden layers until ReLU became the default in the early 2010s.
Check yourself
QuestionRecall: what are the limits of as goes to positive infinity and to negative infinity?Show answer
QuestionComputation: evaluate to four decimal places using the definition.Show answer
QuestionWhy: why do many people prefer tanh over the sigmoid for hidden layers?Show answer
QuestionApplied scenario: a tanh recurrent unit keeps its state near 0.99 for many steps. What does this do to the gradient, and what would you change?Show answer
Sources
- Hyperbolic functions (Wikipedia): https://en.wikipedia.org/wiki/Hyperbolic_functions
- Activation function (Wikipedia): https://en.wikipedia.org/wiki/Activation_function
- Goodfellow, Bengio and Courville, Deep Learning, Ch. 6: https://www.deeplearningbook.org/