Hyperbolic Tangent#

Note

Hyperbolic tangent is also called tanh for short.

Introduction#

Tanh is used when you want to limit the range of the output to \( (-1, 1) \). It looks like sigmoid.

How does tanh look, and how it works in code?#

%matplotlib inline

import numpy as np
from matplotlib import pyplot as plt
def tanh(x):
    exp = np.exp(x)
    inv_exp = 1 / exp
    return (exp - inv_exp) / (exp + inv_exp)
x = np.arange(-10, 11)
y = tanh(x)
print("x =", x)
print("y =", y)
x = [-10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7
   8   9  10]
y = [-1.         -0.99999997 -0.99999977 -0.99999834 -0.99998771 -0.9999092
 -0.9993293  -0.99505475 -0.96402758 -0.76159416  0.          0.76159416
  0.96402758  0.99505475  0.9993293   0.9999092   0.99998771  0.99999834
  0.99999977  0.99999997  1.        ]
x = np.arange(-200, 210) / 20
y = tanh(x)
plt.plot(x, y)
plt.show()
../../../_images/df0959e5933a856c01df71b23fcd324efa7e776d2f1e708f79d1053ed35f9a62.png

Notice that the range of tanh function is in the range \( (-1, 1) \) instead of \( (0, 1) \) like sigmoid.