TExcelTAD
Home › Calculus Essentials › Differentiability

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 (a,f(a))(a, f(a)) and (a+h,f(a+h))(a + h, f(a + h)) is the difference quotient

f(a+h)−f(a)h\frac{f(a + h) - f(a)}{h}

ff is differentiable at aa when this quotient has a limit as h→0h \to 0. That limit is the derivative f′(a)f'(a), 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 (h<0h < 0) and from the right (h>0h > 0) have to match.

Where it fails

Failure Example at 0 What the slopes do
Corner ∣x∣\lvert x \rvert left slope −1-1, right slope +1+1
Cusp x2/3x^{2/3} slopes →−∞\to -\infty on the left, +∞+\infty on the right
Vertical tangent x3\sqrt[3]{x} slopes →+∞\to +\infty from both sides
Discontinuity step function quotient blows up on one side

Let's measure the one-sided slopes of ∣x∣\lvert x \rvert and of x2x^2 at 0 numerically:

python · try it yourself
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 x2x^2 both sides tend to 0: the derivative exists and equals 0. For ∣x∣\lvert x \rvert the sides stay at −1-1 and +1+1 forever, so no single tangent exists. SymPy agrees:

python · try it yourself
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 f′(a)f'(a) exists then ff is continuous at aa:

f(a+h)−f(a)=[f(a+h)−f(a)h]⋅h→f′(a)⋅0=0f(a + h) - f(a) = \left[\frac{f(a + h) - f(a)}{h}\right] \cdot h \to f'(a) \cdot 0 = 0

The converse is false: ∣x∣\lvert x \rvert 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 (C1C^1) derivative exists and is itself continuous

In software: ReLU and its corner

The most popular activation in deep learning is

ReLU⁡(x)=max⁡(0,x)slope 0 for x<0, slope 1 for x>0\operatorname{ReLU}(x) = \max(0, x) \qquad \text{slope 0 for } x < 0,\ \text{slope 1 for } x > 0

It has a corner at x=0x = 0, exactly like ∣x∣\lvert x \rvert, so it is not differentiable there. Training uses gradients, so why does nobody care?

  1. Exact zeros are rare. Inputs to a ReLU are sums of many products of floats. Hitting exactly 0.0 is very unlikely, and a single point has no influence on the average gradient.
  2. 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.
  3. Smooth variants exist when it matters: softplus ln⁡(1+ex)\ln(1 + e^x) and GELU are differentiable everywhere and look like ReLU from a distance.
python · try it yourself
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 xx is f(x)=∣x−2∣+3f(x) = \lvert x - 2 \rvert + 3 not differentiable, and what are the one-sided slopes there?

Answer

At x=2x = 2; left slope −1-1, right slope +1+1.

Solution
for x<2:f(x)=−(x−2)+3=5−xslope −1for x>2:f(x)=(x−2)+3=x+1slope +1the slopes disagree at x=2, so there is a corner there; f is continuous there (f(2)=3)\begin{aligned} &\text{for } x < 2{:} && f(x) = -(x - 2) + 3 = 5 - x && \text{slope } -1 \\ &\text{for } x > 2{:} && f(x) = (x - 2) + 3 = x + 1 && \text{slope } +1 \\ &\text{the slopes disagree at } x = 2, \text{ so there is a corner there; } f \text{ is continuous there } (f(2) = 3) \end{aligned}

Exercise 2: Continuous but not differentiable?

Is g(x)=x∣x∣g(x) = x\lvert x \rvert differentiable at 0? Use the difference quotient.

Answer

Yes, g′(0)=0g'(0) = 0.

Solution
g(0+h)−g(0)h=h∣h∣h=∣h∣→0as h→0 (from both sides)\frac{g(0 + h) - g(0)}{h} = \frac{h\lvert h \rvert}{h} = \lvert h \rvert \to 0 \quad \text{as } h \to 0 \text{ (from both sides)}

Multiplying the corner by xx flattens it out.

Exercise 3: Make the pieces join smoothly

Find aa and bb so that f(x)=x2f(x) = x^2 for x≤1x \le 1 and f(x)=ax+bf(x) = ax + b for x>1x > 1 is differentiable at 1.

Answer

a=2a = 2, b=−1b = -1

Solution
continuity at 1: 12=a+bequal slopes: 2⋅1=a(slope of x2 at 1 is 2)so a=2, b=1−2=−1\begin{aligned} &\text{continuity at 1: } && 1^2 = a + b \\ &\text{equal slopes: } && 2 \cdot 1 = a && \text{(slope of } x^2 \text{ at 1 is 2)} \\ &\text{so } && a = 2,\ b = 1 - 2 = -1 \end{aligned}

The line y=2x−1y = 2x - 1 is the tangent to x2x^2 at x=1x = 1.

Exercise 4: Leaky ReLU

LeakyReLU(x)=x\text{LeakyReLU}(x) = x for x≥0x \ge 0 and 0.01x0.01x for x<0x < 0. Estimate both one-sided slopes at 0 in code with h = 1e-6.

Answer

Left ≈0.01\approx 0.01, right ≈1\approx 1, so it is not differentiable at 0 (but, unlike ReLU, its slope is never exactly 0).

Solution
python · try it yourself
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?

  1. Every differentiable function is continuous
  2. Every continuous function is differentiable
  3. ∣x∣\lvert x \rvert is not continuous at 0
  4. Differentiability and continuity mean the same

Why does ReLU's corner at 0 not break neural network training?

  1. ReLU is actually differentiable at 0
  2. Frameworks use a fixed subgradient there and exact zeros are rare
  3. Training never uses derivatives
  4. The corner is removed by normalisation

What happens to the difference quotient of x3\sqrt[3]{x} at 0?

  1. It tends to 0
  2. It tends to 1
  3. It grows without bound (vertical tangent)
  4. It oscillates