ContentsChapter 8 · Neural networks1 of 3

8.1

From logistic regression to an MLP

In this chapter
  1. 8.1From logistic regression to an MLP
  2. 8.2Backpropagation
  3. 8.3Training
Before you readExactly one of two switches must be on for a lamp to light. Plot the four switch settings at the corners of a square. Can a straight line separate the on cases from the off cases?
Watch: But what is a neural network? · 3Blue1Brown · Neural networks, ch. 1

Logistic regression takes a weighted sum z=wTx+bz=w^Tx+b and passes it through a sigmoid. The sigmoid bends the score into a probability, but the 0.50.5 cutoff is still where z=0z=0 a straight line in two dimensions. It can’t separate the two opposite corners of the switch problem, called exclusive OR (XOR).

A neuron takes a weighted sum with a bias and applies an activation. A hidden neuron often uses a rectified linear unit (ReLU), ReLU⁡(z)=max⁡(0,z)\operatorname{ReLU}(z)=\max(0,z) negative inputs become zero; positive inputs pass through. A multilayer perceptron (MLP) feeds the outputs of one layer of neurons into the next.

For XOR, let s=x1+x2s=x_1+x_2 One hidden neuron outputs h1=ReLU⁡(s)h_1=\operatorname{ReLU}(s) the other outputs h2=ReLU⁡(s−1)h_2=\operatorname{ReLU}(s-1) The output score is z=h1−2h2−12z=h_1-2h_2-\tfrac12 The first neuron makes the score rise. Past s=1s=1 the second pulls it down twice as fast as the first raises it. So the score is positive at s=1s=1 and negative at s=0s=0 and s=2s=2

-0.500.5012
h1=1h_1=1h2=0h_2=0z=0.50z=0.50: lamp on
Try all four switch settings. Only one switch on puts the score above zero, between the two crossings.
Recall
Why can't a sigmoid applied to one weighted sum solve XOR?

A layer is a matrix and an activation

Stack the hidden weight vectors as rows of WW For a column input xx the forward pass is

h=ReLU⁡(Wx+b),p^=σ(vTh+c).h = \operatorname{ReLU}(Wx+b), \qquad \hat p=\sigma(v^Th+c).

ReLU acts on each entry separately. In XOR, both rows of WW are (1,1)(1,1) b=(0,−1)Tb=(0,-1)^T v=(1,−2)Tv=(1,-2)^T and c=−12c=-\tfrac12 With dd inputs and mm hidden neurons, WW is m×dm\times d and hh has mm entries. The next layer takes those mm entries as its input.

Recall
Why not just stack more layers of weighted sums without activations?
Exercise
For the XOR network, take (x1,x2)=(1,1)(x_1,x_2)=(1,1) Find h1h_1 h2h_2 and the output score zz in that order.
Exercise
A layer has a 3×23\times 2 weight matrix WW Its activation h=ReLU⁡(Wx+b)h=\operatorname{ReLU}(Wx+b) feeds a second layer with four neurons. Give four numbers in order: the input length, the bias length, and the number of rows and columns in the second weight matrix.