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, , 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 , and for binary inputs. What function is y?
(0, 0) gives 0 − 0 = 0; (1, 0) and (0, 1) give 1 − 0 = 1; (1, 1) gives 2 − 2 = 0. That's XOR, which no single linear threshold can compute. The hidden layer remaps the inputs so a linear output can separate them.
Name it
The two-layer network
The hidden units 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
with the cross-entropy loss of lesson 4.
Size and cost
A D–H–K network has parameters, and a forward pass costs about 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
- Hidden units.
- Output.
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
- Parameters.
- Multiply-accumulates.
- Softmax. Exponentials normalized to sum to 1:
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
- Parameters.
- Memory. That's 7.2 MB in 32-bit floats (Adam's two running averages triple that).
- 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.
- Total arithmetic. Over examples (60,000 images for 10 epochs): 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 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°.
Step 2
How many multiply-accumulates does one input take?
Explain why
Why does a network need nonlinear hidden units, and what do they buy compared with a fixed basis?
Without a nonlinearity, each layer is a matrix multiplication, and a product of matrices is just another matrix, so any depth of linear layers computes only a linear function of the input. A nonlinear activation lets the hidden layer remap the input into features in which the task becomes linear, as the XOR network shows. Compared with a fixed basis such as polynomials, whose size explodes with input dimension, the hidden units' weights are learned, so the network places its basis functions where the data need them and uses far fewer of them for high-dimensional inputs.
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.