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, , is the default nonlinearity in modern deep learning. It has a kink at 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:
- 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.
- 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 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 is differentiable at if there is a row vector such that
When the limit holds, the unique such is the Jacobian from session 0.2 (numerator convention). Differentiability at 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,
Computing one-sided derivatives at :
These disagree, so is not differentiable at . Everywhere else,
The exceptional set is the single point .
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 ".
Definition (measure zero). A set has Lebesgue measure zero if for every there exists a countable collection of open boxes whose union covers and whose total volume satisfies .
Two facts to internalise.
- Any finite or countably infinite set in has measure zero. (Cover the -th point with a box of volume .)
- Any countable union of measure-zero sets has measure zero.
Definition (almost everywhere). A property holds almost everywhere (a.e.) on if the set has measure zero.
ReLU is differentiable a.e. — the failure set is .
More generally, two deep theorems we'll cite without proof:
- (Rademacher[1].) Every Lipschitz function is differentiable almost everywhere.
- (Convex case.) Every convex function on an open convex set is differentiable a.e. on and even continuously differentiable on a dense open subset.
ReLU is convex and Lipschitz with constant , so it satisfies both. The same is true of , 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 satisfies for all . The natural generalisation is to drop the assumption of differentiability and demand the existence of some vector that plays the role of .
Definition. Let be convex on an open convex set . A vector is a subgradient of at if
The set of all such is the subdifferential of at , denoted .
Three facts to keep handy.
- Reduction to the smooth case. If is differentiable at , then . (The first-order condition forces ; uniqueness is from differentiability.)
- Existence. For convex on an open convex domain, at every interior . (Proof uses the supporting-hyperplane theorem for the epigraph; we won't reprove it here.)
- Shape. 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 at every .
Case . is differentiable in a neighbourhood with , so .
Case . Symmetrically, .
Case . We need every such that , i.e.
Split by sign of :
- : the inequality reads , equivalently .
- : the inequality reads . Since , this requires .
- : trivial .
Both constraints together give . Hence
The subdifferential is a singleton off the kink and the interval 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 , output . The kink matters only at , i.e. on the hyperplane in parameter space (or, for fixed parameters, the hyperplane in input space).
A hyperplane in has Lebesgue measure zero. So:
- Under any continuous initialisation of weights (Gaussian, Glorot, He, etc.), the event has probability .
- Under any continuous input distribution , the event has probability .
Therefore, with probability one over a fresh mini-batch and a fresh init, autograd never has to choose anything at all — is strictly positive or strictly negative and is well defined.
In finite precision (, , ) the situation is slightly different because the floating-point grid is discrete, so can actually equal . Frameworks resolve this by picking a subgradient: PyTorch and TensorFlow both define in their backward pass. By the boxed result above, any value in would be a valid choice; the literature uses by convention.
8. Beyond ReLU
The same machinery applies to most non-smooth pieces of modern networks.
- Leaky ReLU with . Same kink at ; subdifferential there is .
- Absolute value . Differentiable off ; .
- Max-pooling . Convex, a.e. differentiable; subgradient is the convex hull of the winners.
- GELU, . Fully smooth (). 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
- ReLU fails to be differentiable on the single point — a set of Lebesgue measure zero.
- For convex non-smooth , the subdifferential replaces . It is single-valued where is differentiable, multi-valued at kinks, and never empty in the interior of the domain.
- SGD with continuous inits and continuous data hits a ReLU kink with probability zero in exact arithmetic; in finite precision, frameworks define by convention. Any choice in is a valid subgradient.
- The gradient-descent story from 0.8 §6 extends to non-smooth convex objectives as the subgradient method: at each step pick any and update . Convergence is slower than smooth gradient descent ( instead of for typical step sizes), but it works.
(a) Redo the §6 computation for and show that .
(b) Let . Show that is convex on , find the unique minimiser, and compute . (Hint: subdifferentials of convex functions add — for finite convex on .)
(c) Suppose pre-activation with drawn from any continuous distribution independent of and . Argue that . Why does this not immediately let us ignore the kink in finite-precision arithmetic?
(d) For Leaky ReLU with slope , prove that is convex on and compute .
References
- [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.
↩