AI / ML Theory
Chapter 0 · 22 min

0.9 — Almost-everywhere differentiability and the ReLU

Goal: Understand why ReLU is fine for gradient descent even though it isn't differentiable at zero, via the language of subgradients and 'almost everywhere'.

1. Why this session exists

The ReLU, σ(x)=max⁡(0,x)\sigma(x) = \max(0, x), is the default nonlinearity in modern deep learning. It has a kink at x=0x = 0 where the left and right derivatives disagree, so strictly speaking the derivative does not exist there. Yet we train trillion-parameter models full of ReLUs every day with vanilla gradient descent, and nobody loses sleep over this.

This session explains why. Two ingredients:

  1. The bad set — points where the derivative fails — is negligible in the precise sense of having Lebesgue measure zero. Functions like ReLU are differentiable almost everywhere.
  2. For convex non-smooth functions there is a clean generalisation of the gradient called the subgradient, which exists everywhere in the interior of the domain, is single-valued where ff is differentiable, and is multi-valued exactly at the kinks.

Together: with probability one, autograd computes a perfectly good gradient; at the measure-zero anomalies, any value in the subdifferential set works.


2. Differentiability recap (one paragraph)

A function f:Rn→Rf : \mathbb{R}^n \to \mathbb{R} is differentiable at xx if there is a row vector A∈R1×nA \in \mathbb{R}^{1 \times n} such that

lim⁡h→0∣f(x+h)−f(x)−Ah∣∥h∥=0.\lim_{h \to 0} \frac{\bigl|f(x + h) - f(x) - A h\bigr|}{\lVert h \rVert} = 0.

When the limit holds, the unique such AA is the Jacobian ∂f/∂x\partial f / \partial x from session 0.2 (numerator convention). Differentiability at xx implies all directional derivatives exist and depend linearly on the direction. Existence of one-sided derivatives that disagree is therefore a failure of differentiability.


3. The bend at zero

For ReLU,

σ(x)={0,x≤0,x,x≥0.\sigma(x) = \begin{cases} 0, & x \le 0, \\ x, & x \ge 0. \end{cases}

Computing one-sided derivatives at x=0x = 0:

lim⁡h→0+σ(0+h)−σ(0)h=lim⁡h→0+hh=1,lim⁡h→0−σ(0+h)−σ(0)h=lim⁡h→0−0h=0.\lim_{h \to 0^+} \frac{\sigma(0+h) - \sigma(0)}{h} = \lim_{h \to 0^+} \frac{h}{h} = 1, \qquad \lim_{h \to 0^-} \frac{\sigma(0+h) - \sigma(0)}{h} = \lim_{h \to 0^-} \frac{0}{h} = 0.

These disagree, so σ\sigma is not differentiable at x=0x = 0. Everywhere else,

σ′(x)={0x<0,1x>0.\sigma'(x) = \begin{cases} 0 & x < 0, \\ 1 & x > 0. \end{cases}

The exceptional set is the single point {0}\{0\}.


4. "Almost everywhere": what measure theory requires

We don't need the full Lebesgue machinery; we just need a robust notion of "negligibly small subset of Rn\mathbb{R}^n".

Definition (measure zero). A set N⊆RnN \subseteq \mathbb{R}^n has Lebesgue measure zero if for every ε>0\varepsilon > 0 there exists a countable collection of open boxes {Bk}\{B_k\} whose union covers NN and whose total volume satisfies ∑kvol⁡(Bk)<ε\sum_k \operatorname{vol}(B_k) < \varepsilon.

Two facts to internalise.

  1. Any finite or countably infinite set in Rn\mathbb{R}^n has measure zero. (Cover the kk-th point with a box of volume ε/2k\varepsilon / 2^k.)
  2. Any countable union of measure-zero sets has measure zero.

Definition (almost everywhere). A property P(x)P(x) holds almost everywhere (a.e.) on Rn\mathbb{R}^n if the set {x:P(x) fails}\{x : P(x) \text{ fails}\} has measure zero.

ReLU is differentiable a.e. — the failure set is {0}\{0\}.

More generally, two deep theorems we'll cite without proof:

  • (Rademacher[1].) Every Lipschitz function f:Rn→Rf : \mathbb{R}^n \to \mathbb{R} is differentiable almost everywhere.
  • (Convex case.) Every convex function ff on an open convex set C⊆RnC \subseteq \mathbb{R}^n is differentiable a.e. on CC and even continuously differentiable on a dense open subset.

ReLU is convex and Lipschitz with constant 11, so it satisfies both. The same is true of ∣x∣|x|, max-pooling, and the entire piecewise-linear menagerie used in deep nets.


5. Subgradients: derivatives for convex non-smooth functions

Recall from 0.8 §3 the first-order characterisation of convexity: a differentiable convex ff satisfies f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top (y - x) for all yy. The natural generalisation is to drop the assumption of differentiability and demand the existence of some vector that plays the role of ∇f(x)\nabla f(x).

Definition. Let f:C→Rf : C \to \mathbb{R} be convex on an open convex set C⊆RnC \subseteq \mathbb{R}^n. A vector g∈Rng \in \mathbb{R}^n is a subgradient of ff at x∈Cx \in C if

 f(y)≥f(x)+g⊤(y−x)for all y∈C. \boxed{\ f(y) \ge f(x) + g^\top (y - x) \quad \text{for all } y \in C.\ }

The set of all such gg is the subdifferential of ff at xx, denoted ∂f(x)⊆Rn\partial f(x) \subseteq \mathbb{R}^n.

Three facts to keep handy.

  1. Reduction to the smooth case. If ff is differentiable at xx, then ∂f(x)={∇f(x)}\partial f(x) = \{\nabla f(x)\}. (The first-order condition forces g=∇f(x)g = \nabla f(x); uniqueness is from differentiability.)
  2. Existence. For convex ff on an open convex domain, ∂f(x)≠∅\partial f(x) \ne \emptyset at every interior xx. (Proof uses the supporting-hyperplane theorem for the epigraph; we won't reprove it here.)
  3. Shape. ∂f(x)\partial f(x) is always a closed convex set.

The intuition: at a smooth point, exactly one tangent line lower-bounds the graph; at a kink, an entire fan of lines can be wedged underneath.


6. The subdifferential of ReLU

We compute ∂σ(x)\partial \sigma(x) at every x∈Rx \in \mathbb{R}.

Case x>0x > 0. σ\sigma is differentiable in a neighbourhood with σ′(x)=1\sigma'(x) = 1, so ∂σ(x)={1}\partial \sigma(x) = \{1\}.

Case x<0x < 0. Symmetrically, ∂σ(x)={0}\partial \sigma(x) = \{0\}.

Case x=0x = 0. We need every g∈Rg \in \mathbb{R} such that σ(y)≥σ(0)+g (y−0)\sigma(y) \ge \sigma(0) + g\,(y - 0), i.e.

max⁡(0,y)≥g yfor all y∈R.\max(0, y) \ge g\, y \quad \text{for all } y \in \mathbb{R}.

Split by sign of yy:

  • y>0y > 0: the inequality reads y≥g yy \ge g\, y, equivalently g≤1g \le 1.
  • y<0y < 0: the inequality reads 0≥g y0 \ge g\, y. Since y<0y < 0, this requires g≥0g \ge 0.
  • y=0y = 0: trivial 0≥00 \ge 0.

Both constraints together give g∈[0,1]g \in [0, 1]. Hence

 ∂σ(x)={{0},x<0,[0,1],x=0,{1},x>0. \boxed{\ \partial \sigma(x) = \begin{cases} \{0\}, & x < 0, \\ [0, 1], & x = 0, \\ \{1\}, & x > 0. \end{cases} \ }

The subdifferential is a singleton off the kink and the interval [0,1][0, 1] at the kink — the linear interpolation between the two one-sided slopes.


7. SGD doesn't actually visit the kink (and the framework convention)

Consider a single ReLU unit inside a network: pre-activation z=w⊤x+bz = w^\top x + b, output σ(z)\sigma(z). The kink matters only at z=0z = 0, i.e. on the hyperplane {(w,b):w⊤x+b=0}\{(w, b) : w^\top x + b = 0\} in parameter space (or, for fixed parameters, the hyperplane {x:w⊤x+b=0}\{x : w^\top x + b = 0\} in input space).

A hyperplane in Rn+1\mathbb{R}^{n+1} has Lebesgue measure zero. So:

  • Under any continuous initialisation of weights (Gaussian, Glorot, He, etc.), the event z=0z = 0 has probability 00.
  • Under any continuous input distribution xx, the event z=0z = 0 has probability 00.

Therefore, with probability one over a fresh mini-batch and a fresh init, autograd never has to choose anything at all — zz is strictly positive or strictly negative and σ′(z)\sigma'(z) is well defined.

In finite precision (fp16\text{fp16}, bf16\text{bf16}, fp32\text{fp32}) the situation is slightly different because the floating-point grid is discrete, so zz can actually equal 00. Frameworks resolve this by picking a subgradient: PyTorch and TensorFlow both define σ′(0):=0\sigma'(0) := 0 in their backward pass. By the boxed result above, any value in [0,1][0, 1] would be a valid choice; the literature uses 00 by convention.


8. Beyond ReLU

The same machinery applies to most non-smooth pieces of modern networks.

  • Leaky ReLU LReLUα(x)=max⁡(αx, x)\text{LReLU}_\alpha(x) = \max(\alpha x,\, x) with α∈(0,1)\alpha \in (0, 1). Same kink at 00; subdifferential there is [α,1][\alpha, 1].
  • Absolute value ∣x∣|x|. Differentiable off 00; ∂∣x∣x=0=[−1,1]\partial |x|_{x=0} = [-1, 1].
  • Max-pooling max⁡(x1,…,xk)\max(x_1, \dots, x_k). Convex, a.e. differentiable; subgradient is the convex hull conv⁡{ei:xi=max⁡jxj}\operatorname{conv}\{e_i : x_i = \max_j x_j\} of the winners.
  • GELU, x⋅Φ(x)x \cdot \Phi(x). Fully smooth (C∞C^\infty). Trades the piecewise-linearity (and exact zero-firing) of ReLU for everywhere-non-zero gradients. We'll see this comparison in Chapter 5.

9. Takeaways

  1. ReLU fails to be differentiable on the single point {0}\{0\} — a set of Lebesgue measure zero.
  2. For convex non-smooth ff, the subdifferential ∂f(x)\partial f(x) replaces ∇f(x)\nabla f(x). It is single-valued where ff is differentiable, multi-valued at kinks, and never empty in the interior of the domain.
  3. SGD with continuous inits and continuous data hits a ReLU kink with probability zero in exact arithmetic; in finite precision, frameworks define σ′(0):=0\sigma'(0) := 0 by convention. Any choice in [0,1][0, 1] is a valid subgradient.
  4. The gradient-descent story from 0.8 §6 extends to non-smooth convex objectives as the subgradient method: at each step pick any gk∈∂L(θk)g_k \in \partial L(\theta_k) and update θk+1=θk−ηkgk\theta_{k+1} = \theta_k - \eta_k g_k. Convergence is slower than smooth gradient descent (O(1/T)\mathcal{O}(1/\sqrt{T}) instead of O(1/T)\mathcal{O}(1/T) for typical step sizes), but it works.

Self-test

(a) Redo the §6 computation for ∣x∣|x| and show that ∂∣x∣x=0=[−1,1]\partial |x|_{x=0} = [-1, 1].

(b) Let f(x)=max⁡(0,x)+12x2f(x) = \max(0, x) + \tfrac{1}{2} x^2. Show that ff is convex on R\mathbb{R}, find the unique minimiser, and compute ∂f(0)\partial f(0). (Hint: subdifferentials of convex functions add — ∂(f+g)(x)=∂f(x)+∂g(x)\partial(f + g)(x) = \partial f(x) + \partial g(x) for finite convex f,gf, g on Rn\mathbb{R}^n.)

(c) Suppose pre-activation z=w⊤x+bz = w^\top x + b with w∈Rnw \in \mathbb{R}^n drawn from any continuous distribution independent of xx and bb. Argue that P(z=0)=0\mathbb{P}(z = 0) = 0. Why does this not immediately let us ignore the kink in finite-precision arithmetic?

(d) For Leaky ReLU with slope α∈(0,1)\alpha \in (0, 1), prove that LReLUα\text{LReLU}_\alpha is convex on R\mathbb{R} and compute ∂LReLUα(0)\partial \text{LReLU}_\alpha(0).


References

  1. [1]

    Rademacher, H. (1919). "Über partielle und totale Differenzierbarkeit von Funktionen mehrerer Variabeln und über die Transformation der Doppelintegrale". Math. Annalen, 79, 340–359. — The almost-everywhere differentiability theorem stated in 0.9.

    ↩