MIT 6.790 — Lecture 13

Neural Networks: Depth and Optimization

§ Neural Network Depth

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∈NL \in \mathbb{N}, we can construct a function f ⁣:[0,1]→[0,1]f \colon [0, 1] \to [0, 1] such that:

Remark. As a reminder, the ReLU activation function is ReLU(t):=max⁡(t,0)\mathrm{ReLU}(t) := \max(t, 0).

To prove this theorem, we'll need the following lemma.

Theorem. (ReLU Breakpoints) If h ⁣:R→Rh \colon \mathbb{R} \to \mathbb{R} is a ReLU network with hidden layer widths m1,m2,…,mLm_1, m_2, \dots, m_L, then hh is a piecewise linear function with at most 2L−1(m1+1)m2…mL2^{L - 1} (m_1 + 1)m_2 \dots m_L linear pieces.

Proof: Induction on LL. The base case of L=1L = 1 states that a linear combination of mm ReLUs is piecewise linear with at most m+1m + 1 linear pieces, which is obviously true.

For the inductive step, the point is the following:

Claim. Passing a piecewise linear function with TT 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 xx-axis.

Each pre-activation in the LthL^{\text{th}} hidden layer is the output of a ReLU network with L−1L - 1 layers and hidden layer widths m1,m2,…,mL−1m_1, m_2, \dots, m_{L - 1}. Thus, each LthL^{\text{th}}-layer pre-activation has at most N:=2L−2(m1+1)m2…mL−1N := 2^{L - 2}(m_1 + 1)m_2\dots m_{L - 1} linear pieces by the inductive hypothesis.

Then the claim above implies that each LthL^{\text{th}}-layer neuron has at most 2N2N linear pieces. And the final output is a linear combination of mLm_L such neurons, hence an upper bound of 2NmL=2L−1(m1+1)m2…mL2Nm_L = 2^{L - 1}(m_1 + 1)m_2\dots m_L, as desired.   ■\blacksquare

Now for the proof of the “Depth Necessity” theorem from earlier.

Proof: Consider the tent map τ ⁣:[0,1]→[0,1]\tau \colon [0, 1] \to [0, 1] given by:

τ(x):=2⋅[ReLU(x)−ReLU(2x−1)]={2xx≤12.2−2xx>12.\tau(x) := 2 \cdot \left[\mathrm{ReLU}(x) - \mathrm{ReLU}(2x - 1)\right] = \begin{cases} 2x & x \leq \frac{1}{2}. \\ 2 - 2x & x > \frac{1}{2}. \end{cases}

Then a depth-O(L2)O(L^2) network can produce the function f(x):=τ(∘)L2(x)f(x) := \tau^{(\circ) L^2}(x) (that is, τ\tau composed with itself L2L^2 times). The result is a function with 2L2−12^{L^2 - 1} oscillations; here's how f(x)f(x) looks for L=2L = 2, for example.

Any output h ⁣:[0,1]→Rh \colon [0, 1] \to \mathbb{R} of a ReLU network of depth LL and at most 2L2^L nodes has (according to the previous theorem):

# of linear pieces≤2L−1⋅[(m1+1)m2…mL]≤2L−1⋅[2LL]L≤2L2−1.\text{\# of linear pieces} \leq 2^{L - 1} \cdot \left[(m_1 + 1)m_2 \dots m_L\right] \leq 2^{L - 1} \cdot \left[\frac{2^L}{L}\right]^{L} \leq 2^{L^2 - 1}.

We used some very crude estimates here, including an AM-GM bound.

The point is that f(x)f(x) forms 2L22^{L^2} triangles with the line y=12y = \frac{1}{2} (see the diagram above). But hh can only change direction at most 2L2−12^{L^2 - 1} times, so hh must be on the “wrong side” of at least half of the triangles.

And the integral of ∣f(x)−h(x)∣|f(x) - h(x)| over just the intervals where hh is on the “wrong side” is already 18\frac{1}{8}, as desired.   ■\blacksquare

Remark. Some of our bounds here are really loose, enough so that the 18\frac{1}{8} 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\theta := \{W^{(i)}, b^{(i)}\}_{i = 1}^{L + 1}. As with any parametric model, we tune the parameter θ\theta by minimizing the empirical risk (ERM). Specifically, we assume the following:

The gradient of the regularizer is easy to compute—the gradient of L(hθ(x),y)\mathcal{L}(h_{\theta}(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)\mathcal{L}(h_{\theta}(x), y). First, a chain rule warm-up.

Problem. Write an expression for ∂f∂w\frac{\partial f}{\partial w} given the computational graph shown below.

Solution: There is one term for each of the four paths from ww to ff in the graph; each term is the product of all of the partial derivatives along that path.

∂f∂w=[∂f∂h1∂h1∂g1∂g1∂w]+[∂f∂h1∂h1∂g2∂g2∂w]+[∂f∂h2∂h2∂g1∂g1∂w]+[∂f∂h2∂h2∂g2∂g2∂w].\frac{\partial f}{\partial w} = \left[ \frac{\partial f}{\partial h_1} \frac{\partial h_1}{\partial g_1} \frac{\partial g_1}{\partial w} \right] + \left[ \frac{\partial f}{\partial h_1} \frac{\partial h_1}{\partial g_2} \frac{\partial g_2}{\partial w} \right] + \left[ \frac{\partial f}{\partial h_2} \frac{\partial h_2}{\partial g_1} \frac{\partial g_1}{\partial w} \right] + \left[ \frac{\partial f}{\partial h_2} \frac{\partial h_2}{\partial g_2} \frac{\partial g_2}{\partial w} \right].

To help concretize this symbol slop, let's actually compute this partial derivative given the following functions:

g1(w)=3w−2   ∥   g2(w)=w2+1   ∥   h1(g1,g2)=g1g2   ∥   h2(g1,g2)=g23−g12   ∥   f(h1,h2)=h12h2g_1(w) = 3w - 2 ~~~ \parallel ~~~ g_2(w) = w^2 + 1 ~~~ \parallel ~~~ h_1(g_1, g_2) = g_1g_2 ~~~ \parallel ~~~ h_2(g_1, g_2) = g_2^3 - g_1^2 ~~~ \parallel ~~~ f(h_1, h_2) = h_1^2h_2

Let's compute ∂f∂w\frac{\partial f}{\partial w} when w=1w = 1. The first step is a forward pass: compute every function at w=1w = 1.

Separately, we may compute all of the partial derivatives:

∂g1∂w=3  ∥  ∂g2∂w=2w  ∥  ∂h1∂g1=g2  ∥  ∂h1∂g2=g1  ∥  ∂h2∂g1=−2g1  ∥  ∂h2∂g2=3g22  ∥  ∂f∂h1=2h1h2  ∥  ∂f∂h2=h12\frac{\partial g_1}{\partial w} = 3 ~~ \parallel ~~ \frac{\partial g_2}{\partial w} = 2w ~~ \parallel ~~ \frac{\partial h_1}{\partial g_1} = g_2 ~~ \parallel ~~ \frac{\partial h_1}{\partial g_2} = g_1 ~~ \parallel ~~ \frac{\partial h_2}{\partial g_1} = -2g_1 ~~ \parallel ~~ \frac{\partial h_2}{\partial g_2} = 3g_2^2 ~~ \parallel ~~ \frac{\partial f}{\partial h_1} = 2h_1h_2 ~~ \parallel ~~ \frac{\partial f}{\partial h_2} = h_1^2

Notice that most of these expressions are not direct functions of ww. In order to evaluate them at w=1w = 1, we must have done the precomputation of every function's value at w=1w = 1 via a forward pass.

Here's how it would look to compute the second of the four terms in ∂f∂w\frac{\partial f}{\partial w}.

∂f∂h1×∂h1∂g2×∂g2∂w=2(h1)(h2)×g1×2w=2(2)(7)×(1)×2(1)=56.\frac{\partial f}{\partial h_1} \times \frac{\partial h_1}{\partial g_2} \times \frac{\partial g_2}{\partial w} = 2(h_1)(h_2) \times g_1 \times 2w = 2(2)(7) \times (1) \times 2(1) = \boxed{56}.

If we were to carry out the full computations for ∂f∂w\frac{\partial f}{\partial w}, we would have to compute:

§ 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)\nabla_{\theta} \mathcal{L}(h_{\theta}(x), y) is computed.

Here, the pre-activation is a ⁣:R→Ra \colon \mathbb{R} \to \mathbb{R} via a(x)=b+wxa(x) = b + wx, and the activation is h(x):=g(a(x))h(x) := g(a(x)). We follow the same approach as before; first calculate each node's value via a forward pass, then compute:

∂L∂w=∂L∂h⋅∂h∂a⋅∂a∂w=2(h(x)−y)⋅g′(a(x))⋅x   ∥   ∂L∂b=∂L∂h⋅∂h∂a⋅∂a∂b=2(h(x)−y)⋅g′(a(x))⋅1\frac{\partial \mathcal{L}}{\partial w} = \frac{\partial \mathcal{L}}{\partial h} \cdot \frac{\partial h}{\partial a} \cdot \frac{\partial a}{\partial w} = 2(h(x) - y) \cdot g'(a(x)) \cdot x ~~~ \parallel ~~~ \frac{\partial \mathcal{L}}{\partial b} = \frac{\partial \mathcal{L}}{\partial h} \cdot \frac{\partial h}{\partial a} \cdot \frac{\partial a}{\partial b} = 2(h(x) - y) \cdot g'(a(x)) \cdot 1

Note that there is only one path from ww to L\mathcal{L} (as well as from bb to L\mathcal{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\{(W^{(i)}, b^{(i)})\}_{i = 1}^{L + 1} as input nodes to each pre-activation a(i)a^{(i)}, though. Instead, the drawing below shows the following:

Importantly, each w[u→v]w[u \to v] edge is an input to the pre-activation it feeds into; the variable w[u→v]w[u \to v] just doesn't get a node.

Remark. The diagram above makes a minor mistake—the superscripts should read {(0),(1),(2),(3)}\{(0), (1), (2), (3)\}, as the leftmost layer shown is the input layer, not a hidden layer.

Our ultimate goal is to compute ∂L∂w[u→v]\frac{\partial \mathcal{L}}{\partial w[u \to v]} for all edges w[u→v]w[u \to v] in the network. However, notice the following:

∂L∂w[u→v]=∂L∂av⋅∂av∂w[u→v]=∂L∂av⋅hu.\frac{\partial \mathcal{L}}{\partial w[u \to v]} = \frac{\partial \mathcal{L}}{\partial a_v} \cdot \frac{\partial a_v}{\partial w[u \to v]} = \frac{\partial \mathcal{L}}{\partial a_v} \cdot h_u.

It's easy to compute all values of huh_u via a forward pass; it remains to compute δ[v]:=∂L∂av\delta[v] := \frac{\partial \mathcal{L}}{\partial a_v} for all vv.

This is the backward pass, done via an induction-flavored approach. The “base case” is δ[8]\delta[8], the pre-activation node closest to L\mathcal{L}.

The green arrows show the only path from a8a_8 to L\mathcal{L}; in general, the base case will only have one such path. This yields:

δ[8]=∂L∂a8=∂L∂h8⋅∂h8∂a8=2(h8−y)⋅g′(a8).\delta[8] = \frac{\partial \mathcal{L}}{\partial a_8} = \frac{\partial \mathcal{L}}{\partial h_8} \cdot \frac{\partial h_8}{\partial a_8} = 2(h_8 - y) \cdot g'(a_8).

Now, if we wish to compute δ[6]\delta[6], the trick is to use our newfound knowledge of δ[8]\delta[8].

By propagating backwards, we minimize computational redundancies by computing earlier values of δ\delta in terms of later ones.

δ[6]=∂L∂a6=∂L∂a8⋅∂a8∂h6⋅∂h6∂a6=δ[8]⋅w[6→8]⋅g′(a6).\delta[6] = \frac{\partial \mathcal{L}}{\partial a_6} = \frac{\partial \mathcal{L}}{\partial a_8} \cdot \frac{\partial a_8}{\partial h_6} \cdot \frac{\partial h_6}{\partial a_6} = \delta[8] \cdot w[6 \to 8] \cdot g'(a_6).

We can compute δ[7]\delta[7] in the same way. With that layer complete, we now move back a layer and tackle δ[3]\delta[3].

In this case, there are two paths from a3a_3 to the 3rd3^{\text{rd}} layer, so our derivative has two terms.

δ[3]=∂L∂a3=[∂L∂a6⋅∂a6∂h3⋅∂h3∂a3]+[∂L∂a7⋅∂a7∂h3⋅∂h3∂a3]=g′(a3)⋅[δ[6]⋅w[3→6]+δ[7]⋅w[3→7]].\delta[3] = \frac{\partial \mathcal{L}}{\partial a_3} = \left[ \frac{\partial \mathcal{L}}{\partial a_6} \cdot \frac{\partial a_6}{\partial h_3} \cdot \frac{\partial h_3}{\partial a_3} \right] + \left[ \frac{\partial \mathcal{L}}{\partial a_7} \cdot \frac{\partial a_7}{\partial h_3} \cdot \frac{\partial h_3}{\partial a_3} \right] = g'(a_3) \cdot \left[ \delta[6] \cdot w[3 \to 6] + \delta[7] \cdot w[3 \to 7] \right].

And we can compute δ[4]\delta[4] and δ[5]\delta[5] in the exact same way.

§ Backpropagation Statement

Using the notation established in the previous example, we can now describe the backpropagation algorithm.

Backpropagation. After reading in a training example (x,y)(x, y)…

(You can check for yourself that the updating formula for b[v]b[v] is also correct.)