Derivatives of Exponentials and Logs
The exponential is the one function that is its own derivative, and the natural log has the simple derivative . These two facts explain why appears everywhere growth, decay and probability show up, and they give the famous shortcut for the sigmoid used in neural networks: .
The core formulas
| Function | Derivative | With the chain rule |
|---|---|---|
| () | ||
| () | ||
| () | ||
Why ? From the definition,
and is exactly the base for which . For any other base that limit is , which is where the extra factor in comes from. Since is the inverse of , differentiating with the chain rule gives , so .
Numeric evidence for the special base
import math
print(" h (2^h-1)/h (e^h-1)/h (3^h-1)/h")
for k in range(1, 7):
h = 10.0**-k
row = [(b**h - 1) / h for b in (2, math.e, 3)]
print(f"{h:7.0e} {row[0]:.7f} {row[1]:.7f} {row[2]:.7f}")
print(f"ln 2 = {math.log(2):.7f}, ln e = 1, ln 3 = {math.log(3):.7f}") h (2^h-1)/h (e^h-1)/h (3^h-1)/h
1e-01 0.7177346 1.0517092 1.1612317
1e-02 0.6955550 1.0050167 1.1046692
1e-03 0.6933875 1.0005002 1.0992160
1e-04 0.6931712 1.0000500 1.0986726
1e-05 0.6931496 1.0000050 1.0986183
1e-06 0.6931474 1.0000005 1.0986129
ln 2 = 0.6931472, ln e = 1, ln 3 = 1.0986123
Base 2 gives a slope factor below 1, base 3 above 1, and sits exactly where the factor is 1.
SymPy confirms
import sympy as sp
x = sp.symbols('x', positive=True)
for f in [sp.exp(3 * x), 2**x, sp.log(x**2 + 1), sp.log(x, 10), x * sp.log(x), sp.exp(-x**2)]:
print(f"d/dx {str(f):<14} = {sp.diff(f, x)}")d/dx exp(3*x) = 3*exp(3*x)
d/dx 2**x = 2**x*log(2)
d/dx log(x**2 + 1) = 2*x/(x**2 + 1)
d/dx log(x)/log(10) = 1/(x*log(10))
d/dx x*log(x) = log(x) + 1
d/dx exp(-x**2) = -2*x*exp(-x**2)
SymPy writes log for the natural log , and log(x, 10) becomes log(x)/log(10).
Logarithmic differentiation
For awkward products and powers like , take logs first:
Neither the power rule (exponent not constant) nor the rule (base not constant) applies directly, but logs turn the power into a product.
The sigmoid and its derivative
The logistic sigmoid squashes any number into :
Differentiate with the chain rule ():
The last step uses . This is why a framework only needs to remember the forward output : the backward pass is , with no extra exponential.
import sympy as sp
z = sp.symbols('z')
s = 1 / (1 + sp.exp(-z))
print("σ' − σ(1 − σ) simplifies to:", sp.simplify(sp.diff(s, z) - s * (1 - s)))
print("max of σ' (at z = 0):", sp.diff(s, z).subs(z, 0))
import math
sig = lambda t: 1 / (1 + math.exp(-t))
for t in [-4, -1, 0, 2]:
a = sig(t)
numeric = (sig(t + 1e-5) - sig(t - 1e-5)) / 2e-5
print(f"z = {t:2} a(1-a) = {a * (1 - a):.6f} numeric = {numeric:.6f}")σ' − σ(1 − σ) simplifies to: 0
max of σ' (at z = 0): 1/4
z = -4 a(1-a) = 0.017663 numeric = 0.017663
z = -1 a(1-a) = 0.196612 numeric = 0.196612
z = 0 a(1-a) = 0.250000 numeric = 0.250000
z = 2 a(1-a) = 0.104994 numeric = 0.104994
In software
- Logistic regression and neural nets: the sigmoid shortcut is used in every backward pass through a sigmoid (logistic regression, neural networks). Paired with cross-entropy loss , the chain rule gives : the from the log cancels the neatly.
- Log-likelihood: statisticians maximise instead of the likelihood itself, because turns products into sums and is easy to differentiate. Same maximiser, simpler maths, and no underflow.
- Compound growth and decay: has . The rate is proportional to the amount, which models interest, population, cooling and radioactive decay. Exponential smoothing in games and UIs (
x += (target − x)·(1 − e^(−k·dt))) is frame-rate independent for the same reason (damping).
Exercises
Exercise 1: Chain with an exponential
Differentiate .
Answer
Solution
Exercise 2: Log of a product
Differentiate for .
Answer
Solution
Exercise 3: Logarithmic differentiation
Differentiate for .
Answer
Solution
Exercise 4: Cross-entropy gradient
For one example with label , . Show that and check it numerically at .
Answer
at .
Solution
import math
sig = lambda z: 1 / (1 + math.exp(-z))
L = lambda z: -math.log(sig(z))
z, h = 0.7, 1e-6
print(round(sig(z) - 1, 4), round((L(z + h) - L(z - h)) / (2 * h), 4))-0.3318 -0.3318
Test yourself
What is ?
If , what is ?
- 0.16
- 0.8
- 0.2
- 0.64
What is ?