MIT 6.790 — Lecture 12

Neural Networks

§ Neurons

Recall from Lecture 11 that the hypothesis class specified by regression models does not contain a hypothesis that can correctly model two-variable XOR; the data pictured below is not linearly separable.

We need more general models than regression models. In come neural networks.

Definition. A single neuron is a function h ⁣:Rd→Rh \colon \mathbb{R}^d \to \mathbb{R}, made up of two parts:

Some common choices of activation functions include:

(sigmoid σ)   g(t):=11+e−t   ∥   g(t):=tanh⁡(t)=et−e−tet+e−t   ∥   g(t):=sgn(t)   ∥   (ReLU)   g(t):=max⁡(0,t)\text{(sigmoid $\sigma$)} ~~~ g(t) := \frac{1}{1 + e^{-t}} ~~~ \parallel ~~~ g(t) := \tanh(t) = \frac{e^t - e^{-t}}{e^t + e^{-t}} ~~~ \parallel ~~~ g(t) := \mathrm{sgn}(t) ~~~ \parallel ~~~ \text{(ReLU)} ~~~ g(t) := \max(0, t)

Remark. Note that tanh⁡(x)=2σ(2x)−1\tanh(x) = 2 \sigma(2x) - 1, so the sigmoid and tanh activations are roughly the same.

§ Neural Networks: XOR Example

Note that a single neuron with sigmoid activation is identical to logistic regression. This means that a single neuron isn't enough to model two-variable XOR. One fix, however, is to use a linear classifier over a hardcoded feature space.

As shown above, the following R2→R2\mathbb{R}^2 \to \mathbb{R}^2 feature map allows us to compute XOR(x1,x2)\mathrm{XOR}(x_1, x_2) via a linear classifier.

Φ ⁣:(x1,x2)↦(AND(x1,x‾2),AND(x‾1,x2)).\Phi \colon (x_1, x_2) \mapsto \left(\mathrm{AND}(x_1, \overline{x}_2), \mathrm{AND}(\overline{x}_1, x_2)\right).

In the diagram below, we represent the process of first converting our inputs (x1,x2)(x_1, x_2) into features (AND(x1,x‾2),AND(x‾1,x2))\left(\mathrm{AND}(x_1, \overline{x}_2), \mathrm{AND}(\overline{x}_1, x_2)\right), then using the features as inputs to the output neuron.

Each input (x1,x2)(x_1, x_2) is a node, each feature (AND(x1,x‾2),AND(x‾1,x2))\left(\mathrm{AND}(x_1, \overline{x}_2), \mathrm{AND}(\overline{x}_1, x_2)\right) is a node, and the output XOR(x1,x2)\mathrm{XOR}(x_1, x_2) is a node. Furthermore, an edge u→vu \to v is drawn if uu is an input to the neuron whose output is vv.

Here's the important point: not only is the final output XOR(x1,x2)\mathrm{XOR}(x_1, x_2) a neuron, but the intermediate feature nodes AND(x1,x‾2)\mathrm{AND}(x_1, \overline{x}_2) and AND(x‾1,x2)\mathrm{AND}(\overline{x}_1, x_2) are neurons too! Note that their activation function is g(z):=1[z≥0]g(z) := \mathds{1}[z \geq 0].

The diagram above is an example of a neural network. Let's generalize this to a definition.

§ Neural Networks: Definition

Definition. A neural network consists of one input layer, some number LL of hidden layers, and one output layer.

That's a whole bunch of jargon to explain the relatively simple idea of chaining LL layers of neurons together.

Here's how this looks in the example of the XOR neural network. Every single parameter in the neural network—that is, every value of b(k)b^{(k)} and every entry of a matrix W(k)W^{(k)}—is written in purple text.

The upshot of neural networks comes from their hidden layers. Previously, we would just have to have known, somehow, that Φ(x):=(AND(x1,x‾2),AND(x‾1,x2))\Phi(x) := \left(\mathrm{AND}(x_1, \overline{x}_2), \mathrm{AND}(\overline{x}_1, x_2)\right) would be a good set of features for encoding XOR(x1,x2)\mathrm{XOR}(x_1, x_2).

With neural networks, however, instead of hardcoding the best features Φ(x)\Phi(x) (or, in the neural network analogy, the best h(L)(x)h^{(L)}(x)) to use, we can instead learn the best features by optimizing the parameters {b(k),W(k)}k=1L+1\{b^{(k)}, W^{(k)}\}_{k = 1}^{L + 1}.

Hidden layers encode features. That raises the question: are hidden layers capable of encoding any feature?

§ Neural Network Universal Approximation

The answer is yes, mostly.

Theorem. (Universal Approximation) For every continuous f ⁣:[0,1]d→Rf \colon [0, 1]^d \to \mathbb{R} and ϵ>0\epsilon > 0, there is a function f^\hat{f} that is a linear combination of ReLUs such that ∫[0,1]d∣f(x)−f^(x)∣ dx<ϵ\int_{[0, 1]^d} |f(x) - \hat{f}(x)| \, \mathrm{d}x < \epsilon.

Furthermore, we can upper-bound the complexity of such a function f^\hat{f}, somewhat.

Theorem. (Universal Approximation Bound) Suppose f ⁣:[0,1]d→Rf \colon [0, 1]^d \to \mathbb{R} is 1-Lipschitz continuous—that is, ∣f(x)−f(y)∣≤∥x−y∥|f(x) - f(y)| \leq \|x - y\| for all x,y∈[0,1]dx, y \in [0, 1]^d. Then for all ϵ>0\epsilon > 0, there is a function f^\hat{f} that is a linear combination of order (1/ϵ)d(1/\epsilon)^d ReLUs such that ∫[0,1]d∣f(x)−f^(x)∣ dx<ϵ\int_{[0, 1]^d} |f(x) - \hat{f}(x)| \, \mathrm{d}x < \epsilon.

Interestingly, both of these theorems state that only one hidden layer is technically necessary—each ReLU is a single node in that layer, and the output layer is the linear combination of those nodes.

Still, though, as we'll see in future lectures, it is often useful to use more than one hidden layer, both from a computation perspective and a representation perspective.