Differentiability
A function is differentiable at a point when its graph looks like a straight line if you zoom in far enough there. Differentiability is a stronger kind of smoothness than continuity: corners, cusps, jumps and vertical tangents all spoil it. This lesson shows how to spot those cases, and why the corner in ReLU does not stop neural networks from training.
Zoom in until it is straight
The slope of the line through two nearby points and is the difference quotient
is differentiable at when this quotient has a limit as . That limit is the derivative , the slope of the tangent line, which the next lesson studies in detail. Because a limit must agree from both sides, the slopes seen from the left () and from the right () have to match.
Where it fails
| Failure | Example at 0 | What the slopes do |
|---|---|---|
| Corner | left slope , right slope | |
| Cusp | slopes on the left, on the right | |
| Vertical tangent | slopes from both sides | |
| Discontinuity | step function | quotient blows up on one side |
Let's measure the one-sided slopes of and of at 0 numerically:
def slope(f, a, h):
return (f(a + h) - f(a)) / h
for name, f in [("|x|", abs), ("x^2", lambda x: x * x)]:
print(name)
for h in [0.1, 0.001, 0.00001]:
print(f" h = {h:<8} left: {slope(f, 0, -h):+.5f} right: {slope(f, 0, h):+.5f}")|x|
h = 0.1 left: -1.00000 right: +1.00000
h = 0.001 left: -1.00000 right: +1.00000
h = 1e-05 left: -1.00000 right: +1.00000
x^2
h = 0.1 left: -0.10000 right: +0.10000
h = 0.001 left: -0.00100 right: +0.00100
h = 1e-05 left: -0.00001 right: +0.00001
For both sides tend to 0: the derivative exists and equals 0. For the sides stay at and forever, so no single tangent exists. SymPy agrees:
import sympy as sp
x, h = sp.symbols('x h')
q = (sp.Abs(0 + h) - sp.Abs(0)) / h
print("|x| left slope :", sp.limit(q, h, 0, dir='-'))
print("|x| right slope:", sp.limit(q, h, 0, dir='+'))
r = (sp.cbrt(h) - 0) / h
print("cbrt slope at 0:", sp.limit(r, h, 0, dir='+'))
print("x^2 derivative at 3:", sp.limit(((3 + h)**2 - 9) / h, h, 0))|x| left slope : -1
|x| right slope: 1
cbrt slope at 0: oo
x^2 derivative at 3: 6
Differentiable implies continuous
If exists then is continuous at :
The converse is false: is continuous everywhere but not differentiable at 0. There are even functions that are continuous everywhere and differentiable nowhere (the Weierstrass function), whose graphs are "all corners", a bit like a stock chart at every zoom level.
| Property | Smoothness level |
|---|---|
| continuous | no breaks |
| differentiable | no breaks, corners, cusps or vertical tangents |
| continuously differentiable () | derivative exists and is itself continuous |
In software: ReLU and its corner
The most popular activation in deep learning is
It has a corner at , exactly like , so it is not differentiable there. Training uses gradients, so why does nobody care?
- Exact zeros are rare. Inputs to a ReLU are sums of many products of floats. Hitting exactly
0.0is very unlikely, and a single point has no influence on the average gradient. - Frameworks just pick a value. PyTorch, JAX and TensorFlow define the derivative at 0 as 0. Any value between 0 and 1 is a valid subgradient (the slope of a line touching the corner from below), and convex optimisation theory shows that subgradients are enough to make progress.
- Smooth variants exist when it matters: softplus and GELU are differentiable everywhere and look like ReLU from a distance.
import numpy as np
rng = np.random.default_rng(7)
w = rng.normal(size=(1000, 50))
x = rng.normal(size=50)
z = w @ x # pre-activations of 1000 neurons
relu_grad = (z > 0).astype(float) # the convention: slope 0 at z == 0
print("exact zeros among pre-activations:", int(np.sum(z == 0.0)))
print("share with slope 1:", relu_grad.mean())
softplus_grad = 1 / (1 + np.exp(-z)) # derivative of ln(1 + e^z)
print("softplus slope at 0:", 1 / (1 + np.exp(-0.0)))
print("max |relu' - softplus'| for |z| > 5:",
round(float(np.max(np.abs(relu_grad - softplus_grad)[np.abs(z) > 5])), 4))exact zeros among pre-activations: 0
share with slope 1: 0.5
softplus slope at 0: 0.5
max |relu' - softplus'| for |z| > 5: 0.0063
The same issue appears with abs() in L1 regularisation and in "hinge" losses for SVMs, and is handled the same way.
Exercises
Exercise 1: Spot the trouble
At which is not differentiable, and what are the one-sided slopes there?
Answer
At ; left slope , right slope .
Solution
Exercise 2: Continuous but not differentiable?
Is differentiable at 0? Use the difference quotient.
Answer
Yes, .
Solution
Multiplying the corner by flattens it out.
Exercise 3: Make the pieces join smoothly
Find and so that for and for is differentiable at 1.
Answer
,
Solution
The line is the tangent to at .
Exercise 4: Leaky ReLU
for and for . Estimate both one-sided slopes at 0 in code with h = 1e-6.
Answer
Left , right , so it is not differentiable at 0 (but, unlike ReLU, its slope is never exactly 0).
Solution
leaky = lambda x: x if x >= 0 else 0.01 * x
h = 1e-6
print("left :", round((leaky(0) - leaky(-h)) / h, 6))
print("right:", round((leaky(h) - leaky(0)) / h, 6))left : 0.01
right: 1.0
Test yourself
Which statement is true?
- Every differentiable function is continuous
- Every continuous function is differentiable
- is not continuous at 0
- Differentiability and continuity mean the same
Why does ReLU's corner at 0 not break neural network training?
- ReLU is actually differentiable at 0
- Frameworks use a fixed subgradient there and exact zeros are rare
- Training never uses derivatives
- The corner is removed by normalisation
What happens to the difference quotient of at 0?
- It tends to 0
- It tends to 1
- It grows without bound (vertical tangent)
- It oscillates