Hyperbolic Functions
The hyperbolic functions sinh, cosh and tanh are built from and . They behave like cousins of sine and cosine, with similar names and similar identities, but they describe hanging cables, special relativity and one of the best-known activation functions in neural networks, tanh.
Definitions
The reciprocal functions follow the trig pattern: , , .
| Function | Domain | Range | Even / odd | Value at 0 |
|---|---|---|---|---|
| all reals | all reals | odd | 0 | |
| all reals | even | 1 | ||
| all reals | odd | 0 |
is the even part of and the odd part: . For large positive , is tiny, so and gets very close to 1.
import math
print(" x sinh cosh tanh")
for x in [-3, -1, 0, 0.5, 1, 3]:
s = (math.exp(x) - math.exp(-x)) / 2
c = (math.exp(x) + math.exp(-x)) / 2
print(f"{x:4} {s:8.4f} {c:8.4f} {s / c:8.4f}")
print("library check:", math.sinh(1), math.cosh(1), math.tanh(1)) x sinh cosh tanh
-3 -10.0179 10.0677 -0.9951
-1 -1.1752 1.5431 -0.7616
0 0.0000 1.0000 0.0000
0.5 0.5211 1.1276 0.4621
1 1.1752 1.5431 0.7616
3 10.0179 10.0677 0.9951
library check: 1.1752011936438014 1.5430806348152437 0.7615941559557649
Why "hyperbolic"?
The trig functions live on the circle : the point is always on it. The hyperbolic functions live on the hyperbola : the point is always on its right branch, because
Compare this with . Only a sign is different, and that sign difference runs through all the identities:
| Trigonometric | Hyperbolic |
|---|---|
import sympy as sp
x = sp.symbols('x')
print(sp.simplify((sp.cosh(x)**2 - sp.sinh(x)**2).rewrite(sp.exp)))
print(sp.simplify((2*sp.sinh(x)*sp.cosh(x) - sp.sinh(2*x)).rewrite(sp.exp)))
print(sp.diff(sp.sinh(x), x), "|", sp.diff(sp.cosh(x), x), "|", sp.diff(sp.tanh(x), x))1
0
cosh(x) | sinh(x) | 1 - tanh(x)**2
.rewrite(sp.exp) writes each function in terms of , which makes the cancellation easy for simplify. The derivative of tanh, , is the same as .
Inverse hyperbolic functions
Because the hyperbolic functions are built from , their inverses are logarithms:
In Python these are math.asinh, math.acosh, math.atanh (and np.arcsinh and so on).
The hanging cable
A chain or cable hanging freely between two posts takes the shape of a catenary, , not a parabola as Galileo guessed. The two curves look alike near the bottom, because for small , but they separate further out.
In software: tanh as an activation
squashes any input into . It is zero-centred and odd, which made it popular in early neural networks and it is still used inside LSTM cells and some attention variants. It is closely related to the sigmoid :
so tanh is just a sigmoid stretched to the range . Its derivative is easy to compute from the output value itself, which backpropagation exploits. But for large the derivative is nearly zero: the neuron saturates and learning slows down (the "vanishing gradient" problem).
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 401)
t = np.tanh(x)
sig = 1 / (1 + np.exp(-x))
plt.figure(figsize=(6, 4))
plt.plot(x, t, label="tanh x")
plt.plot(x, 1 - t**2, "--", label="derivative 1 − tanh²x")
plt.plot(x, sig, ":", label="sigmoid x")
plt.grid(alpha=0.3); plt.legend()
plt.title("tanh, its derivative and the sigmoid")
print("tanh(x) == 2σ(2x) − 1 :", np.allclose(t, 2 / (1 + np.exp(-2 * x)) - 1))
for v in [0, 1, 2, 4]:
print(f"slope at x = {v}: {1 - np.tanh(v)**2:.5f}")tanh(x) == 2σ(2x) − 1 : True
slope at x = 0: 1.00000
slope at x = 1: 0.41997
slope at x = 2: 0.07065
slope at x = 4: 0.00134
At the slope is about 0.0013: a gradient flowing back through that neuron is multiplied by almost zero. See Neural Networks for how modern networks avoid this.
Exercises
Exercise 1: Evaluate from the definition
Compute and exactly.
Answer
, .
Solution
Exercise 2: Prove an identity
Show from the definitions that and , and use them to prove .
Answer
.
Solution
Exercise 3: tanh from sigmoid
Check numerically that at and , with written by hand.
Answer
Both sides agree: about 0.291313 at and −0.935409 at .
Solution
import math
def sigma(z):
return 1 / (1 + math.exp(-z))
for x in [0.3, -1.7]:
print(x, round(math.tanh(x), 6), round(2 * sigma(2 * x) - 1, 6))0.3 0.291313 0.291313
-1.7 -0.935409 -0.935409
Algebraically: ; multiply top and bottom by to get .
Exercise 4: Inverse tanh
Solve for using the log formula, then check with math.atanh.
Answer
Solution
import math
print(round(0.5 * math.log(1.6 / 0.4), 6), round(math.atanh(0.6), 6), round(math.log(2), 6))0.693147 0.693147 0.693147
Test yourself
What is the range of ?
- all reals
Which identity is true for hyperbolic functions?
Why can tanh neurons learn slowly when their inputs are large?
- tanh overflows for big inputs
- The derivative is close to 0 there, so gradients nearly vanish
- tanh is not differentiable
- tanh outputs are always negative