S1424 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S14 · Lesson 24 · Neural networks

Feedforward networks as adaptive basis functions, hidden units and their activation functions, a forward pass by hand, softmax outputs for several classes, how ReLU units build piecewise-linear functions and solve XOR, and counting parameters and operations

The linear models of lessons 9 to 17 use fixed basis functions, and lesson 9 showed how fast a fixed basis grows with the input dimension. A neural network lets the data choose its basis: each hidden unit is a basis function with its own adjustable weights, and the output combines them. With enough hidden units, a single hidden layer can approximate any continuous function, and in practice deeper networks do it with far fewer units. The price is that the error is no longer a quadratic in the weights, which is lessons 25 and 26's problem.

  • A one-input network with three ReLU hidden units, ReLU(a)=max⁡(0,a), combined with output weights 1, −2 and 1, builds a "hat" bump of height h centered at c with half-width w.
  • Each hidden unit contributes a hinge. Sums of hinges are piecewise-linear functions, and sums of bumps can approximate any curve.
  • That's the idea behind the universal approximation theorem, made concrete.

Predict

A network computes h1=ReLU(x1+x2), h2=ReLU(x1+x2−1) and y=h1−2h2 for binary inputs. What function is y?

Name it

The two-layer network

yk(𝐱)=f(∑j=1Hwkj(2)h(∑i=1Dwji(1)xi+bj(1))+bk(2))

The hidden units zj=h(aj) apply a nonlinear activation: the logistic sigmoid, tanh, or the ReLU max(0, a), now the default because its gradient doesn't fade for large inputs. Without a nonlinearity, layers collapse to one linear map.

Output units

The output activation f matches the task: identity for regression (with squared error), sigmoid for two classes, and for K classes the softmax

yk=eak∑jeaj

with the cross-entropy loss of lesson 4.

Size and cost

A D–H–K network has (D+1)H+(H+1)K parameters, and a forward pass costs about DH+HK multiply-accumulates. A one-input ReLU layer of H units produces at most H + 1 linear pieces; stacking layers multiplies the pieces, which is one reason depth helps.

Universal approximation

A network with one hidden layer and enough units can approximate any continuous function on a bounded region to any accuracy. The theorem says nothing about how many units are needed or how to find the weights.

Worked example

A network has one input, two tanh hidden units with weights and biases (0.5, 0) and (−1, 0.5), and a linear output with weights 2 and −1 and no bias. What's its output at x = 1?

Show the solution
  1. Hidden units. z1=tanh⁡(0.5)=0.4621z2=tanh⁡(−1+0.5)=−0.4621
  2. Output. y=2×0.4621−1×(−0.4621)=1.386

Answer the output at x = 1 is 1.386.

Worked example

A digit classifier is a 784–100–10 network with a softmax output. Count its parameters and multiply-accumulates, and find the probabilities for output activations 2, 1 and 0.1 in a three-class version.

Show the solution
  1. Parameters. (784+1)×100+(100+1)×10=79,510
  2. Multiply-accumulates. 784×100+100×10=79,400
  3. Softmax. Exponentials normalized to sum to 1: softmax(2,1,0.1)=(0.659,0.242,0.099)

Answer the network has 79,510 parameters and takes 79,400 multiply-accumulates; the probabilities are 0.659, 0.242 and 0.099.

Worked example

A 784–1000–1000–10 network is trained on 60,000 images for 10 epochs. How many parameters does it have, how much memory do they take in 32-bit floats, and roughly how much arithmetic does training take?

Show the solution
  1. Parameters. 785×1000+1001×1000+1001×10=1,796,010
  2. Memory. That's 7.2 MB in 32-bit floats (Adam's two running averages triple that).
  3. Cost per example. Training costs about 6 floating-point operations per parameter per example: 2 for the forward pass's multiply and add, 4 for the backward pass's two products.
  4. Total arithmetic. Over 6×105 examples (60,000 images for 10 epochs): 6×1.8×106×6×105=6.5×1012operations a few seconds on a GPU and about an hour on one CPU core.

Answer 1,796,010 parameters, 7.2 MB in 32-bit floats, and about 6.5×1012 operations to train.

Check

How many parameters does a 64–16–10 network with biases have?

Check

How many multiply-accumulates does a 32–64–100 network need for a batch of 32 inputs?

Check

A one-input network has two tanh hidden units with weights 1 and −1 (no biases) and output weights 2 and 0.5. What's its output at x = 0.5?

Check

A three-class softmax has activations 1, 0 and 0.5. What probability does it give the first class?

Check

A one-input network has a single hidden layer of 10 ReLU units and a linear output. At most how many linear pieces can its output have?

Check

What's the derivative of the logistic sigmoid at a = 0?

Check

A 25-million-parameter network trains for 20 epochs on 0.05 million examples, on hardware sustaining 0.3 of its 100 TFLOP/s peak.

(a)

About how much arithmetic does training take?

(b)

How long does it take?

Check

A network has D inputs, two hidden layers of H units each, and K outputs, all fully connected with biases.

Step 1

How many parameters does it have?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does a network need nonlinear hidden units, and what do they buy compared with a fixed basis?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.