In this section, we provide an example of a deep ReLU-activated network that cannot be accurately represented by a shallow ReLU-activated network (without using an excessive number of nodes). Formally, we show:
Theorem. (Depth Necessity) For every L∈N, we can construct a function f:[0,1]→[0,1] such that:
It is exactly representable by a ReLU network of depth O(L2) with O(L2) nodes.
For any ReLU network h:[0,1]→R of depth L with at most 2L nodes, we have ∫01∣f(x)−h(x)∣dx≥81.
Remark. As a reminder, the ReLU activation function is ReLU(t):=max(t,0).
To prove this theorem, we'll need the following lemma.
Theorem. (ReLU Breakpoints) If h:R→R is a ReLU network with hidden layer widths m1,m2,…,mL, then h is a piecewise linear function with at most 2L−1(m1+1)m2…mL linear pieces.
Proof: Induction on L. The base case of L=1 states that a linear combination of m ReLUs is piecewise linear with at most m+1 linear pieces, which is obviously true.
For the inductive step, the point is the following:
Claim. Passing a piecewise linear function with T linear pieces through a ReLU at most doubles the number of linear pieces.
Justification: Each linear piece either stays a single piece or gets split in two, depending on if it crosses the x-axis.
Each pre-activation in the Lth hidden layer is the output of a ReLU network with L−1 layers and hidden layer widths m1,m2,…,mL−1. Thus, each Lth-layer pre-activation has at most N:=2L−2(m1+1)m2…mL−1 linear pieces by the inductive hypothesis.
Then the claim above implies that each Lth-layer neuron has at most 2N linear pieces. And the final output is a linear combination of mL such neurons, hence an upper bound of 2NmL=2L−1(m1+1)m2…mL, as desired. ■
Now for the proof of the “Depth Necessity” theorem from earlier.
Proof: Consider the tent map τ:[0,1]→[0,1] given by:
Then a depth-O(L2) network can produce the function f(x):=τ(∘)L2(x) (that is, τ composed with itself L2 times). The result is a function with 2L2−1 oscillations; here's how f(x) looks for L=2, for example.
Any output h:[0,1]→R of a ReLU network of depth L and at most 2L nodes has (according to the previous theorem):
# of linear pieces≤2L−1⋅[(m1+1)m2…mL]≤2L−1⋅[L2L]L≤2L2−1.
We used some very crude estimates here, including an AM-GM bound.
The point is that f(x) forms 2L2 triangles with the line y=21 (see the diagram above). But h can only change direction at most 2L2−1 times, so h must be on the “wrong side” of at least half of the triangles.
And the integral of ∣f(x)−h(x)∣ over just the intervals where h is on the “wrong side” is already 81, as desired. ■
Remark. Some of our bounds here are really loose, enough so that the 81 bound doesn't fully hold water as presented. But the holes are easy to fix if we settle for a smaller constant, say; the gist of the proof is still fully present.
§ Neural Network Optimization
Neural networks are a parametric model with parameters θ:={W(i),b(i)}i=1L+1. As with any parametric model, we tune the parameter θ by minimizing the empirical risk (ERM). Specifically, we assume the following:
Our loss function is L(hθ(x(t)),y(t)):=(hθ(x(t))−y(t))2 (that is, squared loss).
We employ a variant of (stochastic) gradient descent: for each training example (x(t),y(t)), we perform the update:
θ′←θ−α⋅∇θ[L(hθ(x(t)),y(t))+λ⋅Ω(θ)].
Here, α and λ are hyperparameters, and Ω(θ):=∑i=1L+1∥W(i)∥F2 is a Frobenius regularizer.
Note that ∥M∥F2 denotes the sum of the squares of all of the entries in M.
Recall from Lecture 6 that regularizers (e.g. ridge regression terms) penalize large parameters to discourage overfitting.
The gradient of the regularizer is easy to compute—the gradient of L(hθ(x),y), less so.
§ Chain Rule Warm-Up
The rest of this lecture is dedicated to the computation of the gradient of L(hθ(x),y). First, a chain rule warm-up.
Problem. Write an expression for ∂w∂f given the computational graph shown below.
Solution: There is one term for each of the four paths from w to f in the graph; each term is the product of all of the partial derivatives along that path.
Notice that most of these expressions are not direct functions of w. In order to evaluate them at w=1, we must have done the precomputation of every function's value at w=1 via a forward pass.
Here's how it would look to compute the second of the four terms in ∂w∂f.
If we were to carry out the full computations for ∂w∂f, we would have to compute:
All 6 node values in the graph. (The forward pass.)
All 8 partial derivative functions, and all 8 of their evaluations at w=1.
A total of 8 multiplications of partial derivatives and 3 additions of products of partial derivatives.
§ Neuron Warm-Up
Again, as a warm-up, we'll start by investigating the gradients in the simplest possible neural network.
Warm-Up. In a single-neuron one-dimensional neural network, here's how ∇θL(hθ(x),y) is computed.
Here, the pre-activation is a:R→R via a(x)=b+wx, and the activation is h(x):=g(a(x)). We follow the same approach as before; first calculate each node's value via a forward pass, then compute:
Note that there is only one path from w to L (as well as from b to L), so there are no sums yet.
§ Backpropagation Example
Now suppose we have several layers, each with several neurons. We don't draw out each of the parameters {(W(i),b(i))}i=1L+1 as input nodes to each pre-activation a(i), though. Instead, the drawing below shows the following:
The only nodes are the R-valued pre-activations a(i) and their activations h(i).
Each pre-activation and its corresponding activation is given a unique index, written as a purple subscript.
For each entry of each matrix W(i), that entry represents the weight of a directed edge from an activation hu(i−1) to a pre-activation av(i). That weight is hence denoted w[u→v].
We ignore the biases {b(i)}i=1L+1 for now, though they operate almost identically.
Importantly, each w[u→v] edge is an input to the pre-activation it feeds into; the variable w[u→v] just doesn't get a node.
Remark. The diagram above makes a minor mistake—the superscripts should read {(0),(1),(2),(3)}, as the leftmost layer shown is the input layer, not a hidden layer.
Our ultimate goal is to compute ∂w[u→v]∂L for all edges w[u→v] in the network. However, notice the following:
∂w[u→v]∂L=∂av∂L⋅∂w[u→v]∂av=∂av∂L⋅hu.
It's easy to compute all values of hu via a forward pass; it remains to compute δ[v]:=∂av∂L for all v.
This is the backward pass, done via an induction-flavored approach. The “base case” is δ[8], the pre-activation node closest to L.
The green arrows show the only path from a8 to L; in general, the base case will only have one such path. This yields:
δ[8]=∂a8∂L=∂h8∂L⋅∂a8∂h8=2(h8−y)⋅g′(a8).
Now, if we wish to compute δ[6], the trick is to use our newfound knowledge of δ[8].
By propagating backwards, we minimize computational redundancies by computing earlier values of δ in terms of later ones.