Part IV: Neural Networks
Chapter 14
What is the simplest part of a neural network, and what can it do on its own?
- A neuron multiplies each input by a weight, adds them up, and adds a bias. It is a dot product plus a number.
- On its own a neuron can only make a straight-line decision, the same as logistic regression.
- A layer is several neurons reading the same inputs, each with its own weights.
- A whole layer is one matrix calculation: outputs = W x + b.
- A batch passes many examples through at once: rows stay, columns become the number of neurons.
What you should be able to do
- Work out the output of a neuron by hand.
- Say what a single neuron can decide, and give an example of something it cannot.
- Compute the outputs of a whole layer as one matrix calculation.
- Predict the shape of the output when a batch goes through a layer.
Chapter 15
If one neuron can only draw a straight line, how does a network draw anything else?
- Stacking linear layers gains nothing: any number of them collapse into one.
- An activation function between layers prevents the collapse. It must not be a straight line.
- ReLU keeps positives and zeroes negatives. One ReLU neuron makes a hinge with one bend.
- A hidden layer adds up many bends, and with enough of them can approximate any curve.
- Activations are judged by their slopes, because slopes are what training follows.
What you should be able to do
- Show that two linear layers in a row do the work of one.
- Apply a ReLU, and describe the hinge that a single ReLU neuron draws.
- Explain how a hidden layer builds a curve out of straight pieces.
- Say why an activation is judged by its slope.
Chapter 16
A network outputs three numbers. How do we say how wrong they are?
- The output layer gives one raw score per class. Softmax turns them into probabilities that add up to 1.
- Cross-entropy is minus the log of the probability given to the correct class.
- Loss changes smoothly with the weights and is what we train on. Accuracy moves in jumps and is what we report.
- The slope of the loss for each score is probability minus truth.
- An untrained network on k classes should score about logk.
What you should be able to do
- Turn a set of scores into probabilities with softmax, by hand.
- Work out the cross-entropy loss for one example.
- Explain why a network is trained on loss and judged on accuracy.
- Write down the gradient of the loss for each score.
Chapter 17
The network is wrong. Which of its thousands of weights should change, and by how much?
- Training needs the slope of the loss for every weight. Nudging each weight in turn is far too slow.
- Backpropagation gets all the slopes from one forward pass and one backward pass.
- At each step: gradient passed back = gradient received × local slope. That is the chain rule.
- A weight’s gradient is proportional to its input, and a ReLU that was off blocks the gradient.
- The training loop is forward, loss, backward, update. Check a new backward pass against nudging.
What you should be able to do
- Carry out a forward and a backward pass through a small network by hand.
- State the rule that every backward step follows.
- Explain why a ReLU that was off passes no gradient.
- Check a backward pass against nudging, and write the four steps of the training loop.
Chapter 18
Backpropagation gives exact gradients. Why was training still so slow?
- Plain gradient descent uses one learning rate for every direction, and the steepest direction limits it.
- Decay starts with large steps and ends with small ones.
- Momentum carries speed from step to step: consistent directions build up, back-and-forth cancels.
- RMSProp gives each weight its own step size, based on its recent gradients.
- Adam combines momentum and per-weight step sizes, and is the usual default.
What you should be able to do
- Explain why one learning rate cannot suit a lopsided loss surface.
- Work out a decayed learning rate and a momentum update by hand.
- Say what RMSProp and Adam each add, and why Adam is the usual first choice.
- Pick sensible starting settings for an optimizer.
Chapter 19
The network scores 96% on its training data. Why is that not good news?
- A large network on little data memorises: training loss falls while the loss on new data rises.
- A weight penalty pulls every weight toward zero, so sharp, fragile boundaries become expensive.
- Dropout switches off random neurons during training, forcing knowledge to be spread out. It is off at prediction time.
- Early stopping keeps the weights from the point where held-out loss was lowest.
- Every remedy has a “too much” that underfits, and none beats more data.
What you should be able to do
- Spot a memorising network from its two loss curves.
- Add a weight penalty to a network and say what it does to the gradients.
- Explain how dropout works, and why outputs are scaled up during training.
- Use held-out loss to decide when to stop training.
Chapter 20
The network so far picks one class out of three. How does it predict a number, or answer yes or no?
- A network is a body of hidden layers plus a head: the output layer and its loss.
- Yes or no: one sigmoid output with binary cross-entropy.
- One class out of several: softmax with cross-entropy. Several labels at once: one sigmoid per label.
- A number: one output with no activation, with mean squared error.
- With no hidden layers, these heads are linear regression and logistic regression.
What you should be able to do
- Separate a network into its body and its head.
- Choose the output layer and the loss for a given task.
- Work out binary cross-entropy and mean squared error for a small example.
- Explain how linear and logistic regression are networks with a single neuron.
Chapter 21
Does the network we built on toy spirals work on real pictures?
- An image is a grid of numbers. A dense network reads it flattened into one list: 784 inputs for 28 × 28 pixels.
- Scale inputs to a small range and shuffle before every epoch. New inputs must be scaled the same way.
- Large datasets are trained in mini-batches: here 469 steps per epoch.
- A confusion matrix shows which classes are mixed up, which accuracy alone hides.
- A trained model is its architecture plus its weights. Inference is a forward pass.
What you should be able to do
- Describe how an image becomes the input to a dense network.
- Prepare image data for training: scale it, shuffle it and cut it into batches.
- Count the weights in a network and the steps in an epoch.
- Read a ten-class confusion matrix, and save and reload a trained model.
You have not finished a chapter yet. Untick the box to see them all.