Simple Algorithm
Machine Learning · Convergence · 28 min

The Geometry of ReLU Networks

Piecewise-linear functions and counting linear regions.

A ReLU network is, exactly, a continuous piecewise-linear function: it chops its input space into convex polyhedra and computes one affine function on each. That single fact turns questions about networks into questions about polyhedra. This lesson proves the correspondence in both directions, counts the pieces a network can produce and the far fewer it typically does, and shows how the same geometry makes exact verification possible, and NP-hard.

What you’ll learn

  • Prove that a ReLU network is affine on each activation region, a convex polyhedron, and that every continuous piecewise-linear function on Rn\R^n is a ReLU network with at most ⌈log⁡2(n+1)⌉\lceil\log_2(n+1)\rceil hidden layers.
  • Derive Zaslavsky's bound ∑i≤n(mi)\sum_{i\le n}\binom mi for one hidden layer, the product upper bound for deep networks, and the exponential lower bound of Montúfar, Pascanu, Cho & Bengio.
  • Explain why networks at initialization, and in practice after training, have far fewer regions than the maximum (Hanin & Rolnick).
  • Encode a ReLU network exactly as a mixed-integer linear program, derive its LP relaxation, and explain why complete verification is NP-hard.

Activation patterns and polyhedra

Fix an input x\mathbf x. Every hidden ReLU unit is either on (z>0z>0) or off (z≤0z\le0); the list of on/off states of all NN hidden units is the activation pattern s(x)∈{0,1}N\mathbf s(\mathbf x)\in\{0,1\}^N. Write Ds(l)D^{(l)}_{\mathbf s} for the diagonal 0/1 matrix of layer ll's states. Wherever the pattern is constant, every ReLU acts as a fixed linear map, either the identity or zero, so the network is a product of matrices:

f(x)=W(L+1)Ds(L)(W(L)⋯Ds(1)(W(1)x+b(1))⋯+b(L))+b(L+1)=Asx+csf(\mathbf x)=W^{(L+1)}D^{(L)}_{\mathbf s}\Big(W^{(L)}\cdots D^{(1)}_{\mathbf s}\big(W^{(1)}\mathbf x+\mathbf b^{(1)}\big)\cdots+\mathbf b^{(L)}\Big)+\mathbf b^{(L+1)}=A_{\mathbf s}\mathbf x+\mathbf c_{\mathbf s}
On the set Rs={x:s(x)=s}R_{\mathbf s}=\{\mathbf x:\mathbf s(\mathbf x)=\mathbf s\} the network is the affine map Asx+csA_{\mathbf s}\mathbf x+\mathbf c_{\mathbf s}. Its gradient AsA_{\mathbf s} is constant there, which is why a ReLU network's input gradient jumps across region boundaries.
DerivationActivation regions are convex polyhedra
  1. zj(l)(x)=as,l,j⊤x+cs,l,jfor all x∈Rsz^{(l)}_j(\mathbf x)=\mathbf a_{\mathbf s,l,j}^\top\mathbf x+c_{\mathbf s,l,j}\qquad\text{for all }\mathbf x\in R_{\mathbf s}

    By induction over layers, with the pattern held at s\mathbf s, every pre-activation is an affine function of x\mathbf x: if z(l)\mathbf z^{(l)} is affine, so is h(l)=Ds(l)z(l)\mathbf h^{(l)}=D^{(l)}_{\mathbf s}\mathbf z^{(l)}, and hence z(l+1)=W(l+1)h(l)+b(l+1)\mathbf z^{(l+1)}=W^{(l+1)}\mathbf h^{(l)}+\mathbf b^{(l+1)}.

  2. Rs=⋂(l,j): slj=1{as,l,j⊤x+cs,l,j>0} ∩⋂(l,j): slj=0{as,l,j⊤x+cs,l,j≤0}R_{\mathbf s}=\bigcap_{(l,j):\,s_{lj}=1}\{\mathbf a_{\mathbf s,l,j}^\top\mathbf x+c_{\mathbf s,l,j}>0\}\ \cap\bigcap_{(l,j):\,s_{lj}=0}\{\mathbf a_{\mathbf s,l,j}^\top\mathbf x+c_{\mathbf s,l,j}\le0\}

    The pattern is defined by sign conditions on those pre-activations, each an affine inequality in x\mathbf x, so RsR_{\mathbf s} is an intersection of half-spaces: a convex polyhedron (possibly empty).

  3. #{linear regions of f} ≤ #{s:Rs≠∅} ≤ 2N\#\{\text{linear regions of }f\}\ \le\ \#\{\mathbf s:R_{\mathbf s}\neq\emptyset\}\ \le\ 2^N

    So ff is a continuous piecewise-linear (CPWL) function: continuous as a composition of continuous maps, and affine on each of finitely many convex polyhedra that cover Rn\R^n. Neighboring regions may carry the same affine map, so the number of linear regions of ff is at most the number of non-empty activation regions, itself at most 2N2^N.

DerivationProof sketch
  1. f=∑j=1pϵj max⁡i∈Sjℓi,ϵj∈{−1,+1},∣Sj∣≤n+1,ℓi affinef=\sum_{j=1}^p\epsilon_j\,\max_{i\in S_j}\ell_i,\qquad\epsilon_j\in\{-1,+1\},\quad\abs{S_j}\le n+1,\quad\ell_i\ \text{affine}

    Wang & Sun (2005) showed that every CPWL function on Rn\R^n is a signed sum of maxima of at most n+1n+1 affine functions:

  2. max⁡(a,b)=ReLU(a−b)+ReLU(b)−ReLU(−b)\max(a,b)=\mathrm{ReLU}(a-b)+\mathrm{ReLU}(b)-\mathrm{ReLU}(-b)

    A maximum of two numbers costs one hidden layer of three ReLUs, since b=ReLU(b)−ReLU(−b)b=\mathrm{ReLU}(b)-\mathrm{ReLU}(-b):

  3. max⁡(ℓ1,…,ℓn+1) : ⌈log⁡2(n+1)⌉ hidden layers\max(\ell_1,\dots,\ell_{n+1})\ :\ \lceil\log_2(n+1)\rceil\ \text{hidden layers}

    A binary tree of pairwise maxima computes the maximum of n+1n+1 affine functions in ⌈log⁡2(n+1)⌉\lceil\log_2(n+1)\rceil layers (shorter branches are carried along with the same identity trick), and the output layer forms the signed sum. The size can be large; only the depth is logarithmic.

Is that depth necessary? For n=1n=1 one hidden layer suffices, and any one-hidden-layer network representing a function with pp pieces needs at least p−1p-1 units. Hertrich, Basu, Di Summa & Skutella (2021) conjectured that ⌈log⁡2(n+1)⌉\lceil\log_2(n+1)\rceil hidden layers are necessary in general, and Haase, Hertrich & Loho (2023) proved this for networks with integer weights: they need ⌈log⁡2n⌉\lceil\log_2n\rceil hidden layers to compute the maximum of nn numbers. With real weights the conjecture is false: Bakaev, Brunck, Hertrich, Stade & Yehudayoff (2025) showed that two hidden layers compute the maximum of five numbers, and that ⌈log⁡3(n−1)⌉+1\lceil\log_3(n-1)\rceil+1 hidden layers suffice for every CPWL function on Rn\R^n when n≥3n\ge3. Whether real weights need any depth that grows with nn at all is open.

Counting regions: one hidden layer

With one hidden layer of mm units on Rn\R^n, unit jj switches on one side of the hyperplane Hj={x:wj⊤x+bj=0}H_j=\{\mathbf x:\mathbf w_j^\top\mathbf x+b_j=0\}, so the activation regions are exactly the regions of the arrangement of mm hyperplanes. Counting them is a classical problem.

DerivationProof of the upper bound
  1. r(m,n) ≤ r(m−1,n)+r(m−1,n−1),r(0,n)=r(m,0)=1r(m,n)\ \le\ r(m-1,n)+r(m-1,n-1),\qquad r(0,n)=r(m,0)=1

    Add the hyperplanes one at a time. When HmH_m is added, each old region it passes through is split in two, so the number of new regions equals the number of pieces into which the other m−1m-1 hyperplanes cut HmH_m. Those cuts form an arrangement of at most m−1m-1 hyperplanes inside Hm≅Rn−1H_m\cong\R^{n-1}:

  2. ∑i=0n(m−1i)+∑i=0n−1(m−1i)=∑i=0n[(m−1i)+(m−1i−1)]=∑i=0n(mi)\sum_{i=0}^n\binom{m-1}i+\sum_{i=0}^{n-1}\binom{m-1}i=\sum_{i=0}^n\Big[\binom{m-1}i+\binom{m-1}{i-1}\Big]=\sum_{i=0}^n\binom mi

    Pascal's rule (m−1i)+(m−1i−1)=(mi)\binom{m-1}i+\binom{m-1}{i-1}=\binom mi shows that ∑i≤n(mi)\sum_{i\le n}\binom mi satisfies this recursion with equality and the same boundary values, so it bounds r(m,n)r(m,n) by induction. In general position every inequality is an equality.

Predict before you look

A ReLU network on R2\R^2 has one hidden layer of 8 units in general position. At most how many linear regions does it have?

Show the answer

37 regions

(80)+(81)+(82)=1+8+28=37\binom80+\binom81+\binom82=1+8+28=37. Only 37 of the 28=2562^8=256 activation patterns can occur: in the plane, 8 lines cannot realize every combination of sides. In one dimension the count would be m+1=9m+1=9.

Depth multiplies regions

An upper bound. Inside one region of the first l−1l-1 layers, layer ll's pre-activations are affine in x\mathbf x, with a linear part of rank at most dl=min⁡(n0,n1,…,nl−1)d_l=\min(n_0,n_1,\dots,n_{l-1}). Their zero sets form an arrangement of nln_l hyperplanes whose normals span at most dld_l dimensions, so by Zaslavsky each region splits into at most ∑j≤dl(nlj)\sum_{j\le d_l}\binom{n_l}j pieces. Multiplying over layers:

#{regions} ≤ ∏l=1L ∑j=0dl(nlj).\#\{\text{regions}\}\ \le\ \prod_{l=1}^L\ \sum_{j=0}^{d_l}\binom{n_l}j .

Serra, Tjandraatmadja & Ramalingam (2018) sharpened this, with bounds that are exact for one-dimensional inputs, and showed that a deep network can have more regions than every one-hidden-layer network with the same number of units only if that number exceeds the input dimension. They also counted regions exactly by mixed-integer programming.

DerivationWhy: layers that fold space
  1. F: [0,1]n0→[0,1]n0,each of pn0 cells is mapped onto the whole cubeF:\ [0,1]^{n_0}\to[0,1]^{n_0},\qquad\text{each of }p^{n_0}\ \text{cells is mapped onto the whole cube}

    In one dimension, pp ReLUs can form a zigzag that maps each of pp consecutive intervals onto [0,1][0,1], alternately increasing and decreasing (for p=2p=2 this is the tent map TT). Use p=⌊n/n0⌋p=\lfloor n/n_0\rfloor units on each of the n0n_0 coordinates: the layer maps pn0p^{n_0} cells of the input cube onto the same cube [0,1]n0[0,1]^{n_0}.

  2. (pn0)L−1⋅∑j=0n0(nLj)regions\big(p^{n_0}\big)^{L-1}\cdot\sum_{j=0}^{n_0}\binom{n_L}j\quad\text{regions}

    Everything computed after FF is computed identically, mirrored, in every cell, so each region drawn by the later layers appears pn0p^{n_0} times in the input. Fold L−1L-1 times and let the last layer draw an arrangement of nLn_L hyperplanes in general position on the cube:

Architecture on R2\R^2 (32 hidden units)Regions
One hidden layer of 32 units: maximum (Zaslavsky)1+32+496=5291+32+496=529
Four layers of 8: guaranteed by Montúfar et al.'s construction(42)3⋅37=151,552(4^2)^3\cdot37=151{,}552
Four layers of 8: product upper bound374=1,874,16137^4=1{,}874{,}161
The same 32 units, arranged in depth, can produce over 280 times as many regions as the best single layer. All three numbers concern the best possible weights (an exact maximum, a construction and an upper bound); the next step asks what typical weights produce.

Typical networks have few regions

Exponential counts require carefully arranged weights, folding the space into precise mirror images. Hanin & Rolnick asked what happens for networks people actually train:

  • Along lines (Hanin & Rolnick, 2019a): for networks with random weights and biases at initialization, the expected number of regions crossed by a one-dimensional segment grows linearly in the total number of neurons, not exponentially in depth, and the average distance from a point to the nearest region boundary scales like the inverse of the number of neurons.
  • In volume (Hanin & Rolnick, 2019b): the expected number of activation regions per unit volume of input space at initialization is bounded by roughly the total number of neurons raised to the input dimension, O(Nn0)\order{N^{n_0}}, independent of depth. Experiments found this scaling tight at initialization and throughout training, even on memorization tasks that should reward many regions.

So, as measured by regions, gradient-trained networks use a tiny fraction of the capacity that depth makes available. Region counts are also a coarse measure of complexity: many regions can carry nearly the same affine map, and a network with few regions can still represent sharp, intricate boundaries where it needs them.

Think first A network with N=1,000N=1{,}000 hidden units in 10 layers receives two-dimensional inputs. Compare the maximum region counts with the typical count at initialization.

The maximum is astronomically large: with 10 layers of 100 units, Montúfar et al.'s construction alone gives (502)9⋅(1+100+(1002))≈1.9×1034(50^2)^9\cdot\big(1+100+\binom{100}2\big)\approx1.9\times10^{34} regions. The typical density at initialization scales like Nn0=106N^{n_0}=10^6 per unit area, up to constants. The gap of about 28 orders of magnitude is the difference between what a network can express and what random or trained weights actually produce.

Exact verification

Because a ReLU network is piecewise linear, questions such as "is there an input within ℓ∞\ell_\infty distance ε\varepsilon of x0\mathbf x_0 that the network classifies differently?" can be answered exactly by mixed-integer linear programming. The objective and the input set are linear; each ReLU y=max⁡(0,z)y=\max(0,z) is encoded with linear constraints and one binary variable, given bounds l≤z≤ul\le z\le u computed beforehand (for example by propagating intervals through the network).

DerivationThe big-M encoding is exact, and its relaxation is a triangle
  1. y≥0,y≥z,y≤u a,y≤z−l (1−a),a∈{0,1}y\ge0,\qquad y\ge z,\qquad y\le u\,a,\qquad y\le z-l\,(1-a),\qquad a\in\{0,1\}

    For an unstable unit, l<0<ul<0<u, introduce a∈{0,1}a\in\{0,1\} and require:

  2. {(z,y):∃a∈{0,1}}={(z,max⁡(0,z)): l≤z≤u}\{(z,y):\exists a\in\{0,1\}\}=\{(z,\max(0,z)):\ l\le z\le u\}

    If a=1a=1, the second and fourth constraints give y=zy=z, and y≥0y\ge0 forces z≥0z\ge0. If a=0a=0, the first and third give y=0y=0, and y≥zy\ge z forces z≤0z\le0. So the feasible (z,y)(z,y) are exactly the graph of the ReLU on [l,u][l,u]. Stable units need no binary variable: y=0y=0 if u≤0u\le0 and y=zy=z if l≥0l\ge0.

  3. 0≤y,z≤y,y≤u (z−l)u−l0\le y,\qquad z\le y,\qquad y\le\frac{u\,(z-l)}{u-l}

    Relax aa to [0,1][0,1]. For fixed zz the largest feasible yy is max⁡amin⁡{ua, z−l+la}\max_a\min\{ua,\ z-l+la\}, attained where the two bounds meet, at a=(z−l)/(u−l)a=(z-l)/(u-l). The relaxation is the triangle with vertices (l,0)(l,0), (0,0)(0,0), (u,u)(u,u): the convex hull of the ReLU's graph.

Solving the relaxed linear program is cheap and sound: if it certifies that no adversarial input exists, none does. It is incomplete: a failed certificate may come from the relaxation's slack rather than a real counterexample. The exact MILP is complete but exponential in the worst case, since branching on binary variables can explore up to 2(#unstable units)2^{(\#\text{unstable units})} cases. Tight bounds l,ul,u help twice, by making more units stable and by shrinking every triangle. With such tight formulations and presolving, Tjeng, Xiao & Tedrake (2019) verified networks with an order of magnitude more ReLUs than earlier complete verifiers; for one MNIST classifier at ε=0.1\varepsilon=0.1 they found adversarial examples for 4.38% of the test samples and certified robustness for all the rest.

No complete verifier runs in polynomial time on every network unless P = NP: Katz, Barrett, Dill, Julian & Kochenderfer (2017) showed that verifying properties of this kind for ReLU networks is NP-complete, by a reduction from 3-SAT, while introducing their Reluplex solver.

Check your understanding

You check a classifier's robustness at an input using the LP relaxation (every ReLU replaced by its triangle). The LP finds a "counterexample" whose relaxed variables are not all on the ReLU graph. What can you conclude?

  1. The network is not robust at that input
  2. The network is robust at that input
  3. Nothing yet: the relaxation is sound but incomplete, so you must tighten bounds or branch (or solve the MILP) to decide
  4. The interval bounds l,ul,u must be wrong
Show the answer

C. Nothing yet: the relaxation is sound but incomplete, so you must tighten bounds or branch (or solve the MILP) to decide

A relaxed point that is not on the ReLU graph is not an input the network can actually receive, so it proves nothing about the network. The LP can only certify robustness (when its optimum shows no violation); when it fails, the truth is undecided until tighter bounds, branching on unstable units, or the exact MILP settle it.

Key takeaways

  • On each activation region, a convex polyhedron, a ReLU network is affine: f(x)=Asx+csf(\mathbf x)=A_{\mathbf s}\mathbf x+\mathbf c_{\mathbf s}. Conversely, every CPWL function on Rn\R^n is a ReLU network with at most ⌈log⁡2(n+1)⌉\lceil\log_2(n+1)\rceil hidden layers (Arora et al.); integer weights need that much depth, real weights need less (⌈log⁡3(n−1)⌉+1\lceil\log_3(n-1)\rceil+1 layers suffice for n≥3n\ge3), and whether they need any growing depth is open.
  • One hidden layer of mm units on Rn\R^n has at most ∑i≤n(mi)\sum_{i\le n}\binom mi regions (Zaslavsky); deep networks satisfy ∏l∑j≤dl(nlj)\prod_l\sum_{j\le d_l}\binom{n_l}j and can reach Ω((n/n0)(L−1)n0nn0)\Omega((n/n_0)^{(L-1)n_0}n^{n_0}) by folding space (Montúfar et al.).
  • Typical networks have far fewer regions: linear in the number of neurons along a line and about Nn0N^{n_0} per unit volume, independent of depth, at initialization and in practice after training (Hanin & Rolnick).
  • A ReLU with bounds l<0<ul<0<u is encoded exactly by a big-M constraint with one binary; its LP relaxation is the triangle hull. Relaxations are sound but incomplete; exact verification is NP-complete.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Why is the set of inputs that share one activation pattern of a ReLU network convex?

  1. Because ReLU is a convex function
  2. Because with the pattern fixed, every pre-activation is an affine function of the input, and the pattern is a list of sign conditions on them: an intersection of half-spaces
  3. Because the network's output is convex in its input
  4. It is not convex in general; only the first layer's regions are
Show the answer

B. Because with the pattern fixed, every pre-activation is an affine function of the input, and the pattern is a list of sign conditions on them: an intersection of half-spaces

Holding the pattern fixed replaces every ReLU by the identity or zero, so each pre-activation zj(l)(x)z^{(l)}_j(\mathbf x) is affine on the region. The region is {x: zj(l)(x)>0\{\mathbf x:\ z^{(l)}_j(\mathbf x)>0 for on-units, ≤0\le0 for off-units}\}, an intersection of half-spaces.

Question 2

A one-hidden-layer ReLU network on R3\R^3 has 5 hidden units in general position. How many linear regions can it have at most?

  1. 32
  2. 16
  3. 26
  4. 6
Show the answer

C. 26

∑i=03(5i)=1+5+10+10=26\sum_{i=0}^3\binom5i=1+5+10+10=26. Five planes in R3\R^3 cannot realize all 25=322^5=32 on/off combinations.

Question 3

What did Hanin & Rolnick find about the number of regions of randomly initialized deep ReLU networks?

  1. It grows exponentially with depth, matching the maximal constructions
  2. It is always exactly one region
  3. Along a line it grows linearly in the total number of neurons, and per unit volume roughly like (neurons)input dim^{\text{input dim}}, independent of depth
  4. It decreases as networks get deeper
Show the answer

C. Along a line it grows linearly in the total number of neurons, and per unit volume roughly like (neurons)input dim^{\text{input dim}}, independent of depth

Typical counts are governed by the total number of neurons, not by depth, far below the exponential maxima, and experiments found the same scaling during training. Depth's exponential capacity is mostly unused by gradient-trained networks, as measured by regions.

Question 4

In the MILP encoding of a ReLU network for verification, which units need a binary variable?

  1. Every unit in the network
  2. Only units in the last hidden layer
  3. Only units whose output is used by the next layer
  4. Only unstable units, whose pre-activation bounds satisfy l<0<ul<0<u
Show the answer

D. Only unstable units, whose pre-activation bounds satisfy l<0<ul<0<u

If u≤0u\le0 the unit is always off (y=0y=0), and if l≥0l\ge0 it is always on (y=zy=z): both are linear. Only units that can be either need the binary switch, so tighter bounds mean fewer binaries and a smaller search.

Further reading

  • R. Arora, A. Basu, P. Mianjy & A. Mukherjee, "Understanding deep neural networks with rectified linear units," International Conference on Learning Representations (ICLR) (2018).
  • G. Montúfar, R. Pascanu, K. Cho & Y. Bengio, "On the number of linear regions of deep neural networks," Advances in Neural Information Processing Systems 27 (2014).
  • T. Serra, C. Tjandraatmadja & S. Ramalingam, "Bounding and counting linear regions of deep neural networks," International Conference on Machine Learning (ICML) (2018).
  • B. Hanin & D. Rolnick, "Complexity of linear regions in deep networks," ICML (2019); and "Deep ReLU networks have surprisingly few activation patterns," NeurIPS (2019).
  • V. Tjeng, K. Xiao & R. Tedrake, "Evaluating robustness of neural networks with mixed integer programming," ICLR (2019).