Search

Search papers and lessons

Hyperbolic Tangent

Level 2 · 7 min read

The hyperbolic tangent squashes any real number into the range from -1 to 1 along an S-shaped curve centred at zero, a zero-centred cousin of the sigmoid.

On this page

  • Colour = topic mix
  • Neural network basics
  • Discrete math
  • Sign in to see your progress

TL;DR

The hyperbolic tangent, written tanh⁡x\tanh x, takes any real number and returns a value strictly between −1-1 and 11. Its graph is an S-shape like the sigmoid, but it passes through the origin: tanh⁡0=0\tanh 0 = 0. 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 tanh⁡\tanh 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.

−4−3−2−101234−1−0.500.51xoutput
  • tanh(x)
  • y = x
Near zero, tanh follows the dashed line y = x closely. Beyond about |x| = 2 it bends over and approaches its limits of plus or minus 1.

The math

Definition: Hyperbolic tangent

tanh⁡x=ex−e−xex+e−x\tanh x = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}

Here xx is any real number and e≈2.71828e \approx 2.71828 is Euler's number (the exponential function with base ee).

Multiplying the top and bottom by exe^{x} gives an equivalent form that is often easier to evaluate:

tanh⁡x=e2x−1e2x+1=1−2e2x+1\tanh x = \frac{e^{2x} - 1}{e^{2x} + 1} = 1 - \frac{2}{e^{2x} + 1}

Range, limits and symmetry

As xx grows large and positive, e−2xe^{-2x} shrinks to zero and tanh⁡x\tanh x approaches 11. As xx grows large and negative, tanh⁡x\tanh x approaches −1-1. The function is strictly increasing, so it never reaches either limit.

It is an odd function. Replacing xx with −x-x swaps the sign of the numerator and leaves the denominator unchanged, so

tanh⁡(−x)=−tanh⁡x.\tanh(-x) = -\tanh x.

In particular tanh⁡0=0\tanh 0 = 0.

Relation to the sigmoid

The sigmoid is σ(z)=1/(1+e−z)\sigma(z) = 1/(1 + e^{-z}). Substituting z=2xz = 2x:

2σ(2x)−1=21+e−2x−1=1−e−2x1+e−2x.2\sigma(2x) - 1 = \frac{2}{1 + e^{-2x}} - 1 = \frac{1 - e^{-2x}}{1 + e^{-2x}}.

Multiply the top and bottom by exe^{x} and the right-hand side becomes ex−e−xex+e−x=tanh⁡x\dfrac{e^{x} - e^{-x}}{e^{x} + e^{-x}} = \tanh x. So

tanh⁡x=2σ(2x)−1.\tanh x = 2\sigma(2x) - 1.

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 12\tfrac{1}{2}.

The derivative

Write N=ex−e−xN = e^{x} - e^{-x} and D=ex+e−xD = e^{x} + e^{-x}. Then N′=DN' = D and D′=ND' = N. The quotient rule gives

ddxtanh⁡x=N′D−ND′D2=D2−N2D2=1−tanh⁡2x.\frac{d}{dx}\tanh x = \frac{N'D - ND'}{D^2} = \frac{D^2 - N^2}{D^2} = 1 - \tanh^2 x.

So the slope at any point depends only on the output at that point. The slope is 11 at x=0x = 0 and shrinks toward 00 as ∣x∣|x| grows. This is the fact that matters most in training, because backpropagation multiplies these slopes together (Backpropagation).

−4−3−2−10123400.51xvalue
  • tanh(x)
  • derivative
The derivative peaks at 1 when x = 0 and falls towards 0 once the curve flattens.

Worked examples

Example
Evaluate tanh 1 by hand
  1. Compute e1=2.718282e^{1} = 2.718282 and e−1=0.367879e^{-1} = 0.367879.
  2. Numerator: 2.718282−0.367879=2.3504032.718282 - 0.367879 = 2.350403.
  3. Denominator: 2.718282+0.367879=3.0861612.718282 + 0.367879 = 3.086161.
  4. Divide: 2.350403/3.086161=0.7615942.350403 / 3.086161 = 0.761594.

So tanh⁡1≈0.7616\tanh 1 \approx 0.7616. An input of 1 is squashed to about three quarters of the maximum output.

Example
A tanh neuron and its slope

Take one neuron with weight w=1.5w = 1.5, input x=0.8x = 0.8 and bias b=−0.4b = -0.4. It computes h=tanh⁡zh = \tanh z where z=wx+bz = wx + b.

  1. Pre-activation: z=1.5×0.8−0.4=1.2−0.4=0.8z = 1.5 \times 0.8 - 0.4 = 1.2 - 0.4 = 0.8.
  2. Compute e0.8=2.225541e^{0.8} = 2.225541 and e−0.8=0.449329e^{-0.8} = 0.449329.
  3. Numerator: 2.225541−0.449329=1.7762122.225541 - 0.449329 = 1.776212. Denominator: 2.225541+0.449329=2.6748702.225541 + 0.449329 = 2.674870.
  4. Output: h=1.776212/2.674870=0.664037h = 1.776212 / 2.674870 = 0.664037.
  5. Slope at this input: 1−0.6640372=1−0.440945=0.5590551 - 0.664037^2 = 1 - 0.440945 = 0.559055.

The neuron outputs 0.664. A small change in zz would change the output by about 0.56 times as much. That is a healthy slope, so the neuron is still learning.

Example
Saturation at a large input

Take z=4z = 4, using tanh⁡z=1−2/(e2z+1)\tanh z = 1 - 2/(e^{2z} + 1).

  1. Compute e8=2980.958e^{8} = 2980.958.
  2. Add one: e8+1=2981.958e^{8} + 1 = 2981.958.
  3. Divide: 2/2981.958=0.0006712 / 2981.958 = 0.000671.
  4. Output: tanh⁡4=1−0.000671=0.999329\tanh 4 = 1 - 0.000671 = 0.999329.
  5. Slope: 1−tanh⁡24=(1−tanh⁡4)(1+tanh⁡4)=0.000671×1.999329=0.0013411 - \tanh^2 4 = (1 - \tanh 4)(1 + \tanh 4) = 0.000671 \times 1.999329 = 0.001341.

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 ∣x∣|x| 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.

SituationProblemBetter choice
Deep feed-forward hidden layersUnits saturate, so early layers stop learningRectified Linear Unit (ReLU) or a variant
Output must be a probability in (0, 1)Tanh gives values in (-1, 1)[[sigmoid-function
Recurrent hidden stateSaturation is common over long sequencesKeep tanh, use gated cells ([[gated-recurrent-unit
Inputs that are not normalisedLarge pre-activations push units into saturationNormalise 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 tanh⁡x\tanh x as xx goes to positive infinity and to negative infinity?Show answer
As xx goes to positive infinity, tanh⁡x\tanh x approaches 11. As xx goes to negative infinity, it approaches −1-1. The function never reaches either value, because e−2xe^{-2x} is never exactly zero.
QuestionComputation: evaluate tanh⁡0.5\tanh 0.5 to four decimal places using the definition.Show answer
Compute e0.5=1.648721e^{0.5} = 1.648721 and e−0.5=0.606531e^{-0.5} = 0.606531. The numerator is 1.0421901.042190 and the denominator is 2.2552522.255252. Dividing gives 0.4621170.462117, so tanh⁡0.5≈0.4621\tanh 0.5 \approx 0.4621.
QuestionWhy: why do many people prefer tanh over the sigmoid for hidden layers?Show answer
Tanh is zero-centred, so its outputs average near zero and the next layer receives inputs that are balanced around zero. The sigmoid outputs are always positive, which pushes the gradients of the next layer's weights in the same direction and makes training zig-zag. Tanh also has a larger maximum slope, 1 compared with 0.25.
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
At 0.99 the slope is 1−0.992=0.01991 - 0.99^2 = 0.0199, about 0.02. Gradients passing back through many such steps shrink geometrically, so the early inputs stop affecting learning. Remedies include keeping the recurrent weights small through careful initialisation, using gated cells such as GRU or LSTM that give the state a direct path, or reducing how often the state saturates. Gradient clipping addresses the opposite problem, exploding gradients, so it does not fix this one.

Sources