Reference

Recap.

The key points of every chapter, for revising. Read a card and ask yourself whether you could explain each line.

Part 0: Toolkit

Toolkit 0.1

Vectors and Matrices

How do you do arithmetic on a whole table of data at once?

  • A vector is an ordered list of numbers. It describes one example by its features.
  • Vectors are added entry by entry, and stretched by multiplying every entry by a scalar.
  • The dot product multiplies matching entries and adds them up. A linear model’s prediction is a dot product.
  • A matrix is a table with one row per example. Matrix times vector is one dot product per row.
  • Shapes must match on the inside: (n × d) times (d) gives (n).
What you should be able to do
  • Add two vectors and stretch one by a number, by hand.
  • Work out a dot product and say what its sign tells you.
  • Multiply a matrix by a vector and predict the shape of the answer before computing it.
Toolkit 0.2

Derivatives and Gradients

If I change the input a little, how much does the output change?

  • A derivative is the slope of a function at a point: how much the output changes per unit change in the input.
  • It can be found by nudging the input and dividing, or by a few simple rules.
  • At a lowest or highest point the derivative is zero.
  • With several inputs there is one partial derivative each. Together they form the gradient, which points uphill.
  • The chain rule: when calculations are chained, their rates multiply.
What you should be able to do
  • Estimate a slope by nudging the input and dividing.
  • Find the derivative of a simple formula with the rules, and use it to find a lowest point.
  • Work out a gradient and say which direction it points.
  • Apply the chain rule to a calculation done in two steps.
Toolkit 0.3

Probability

How do you reason carefully about things you cannot be sure of?

  • A probability is a long-run share between 0 and 1. Small samples are ragged; large ones settle.
  • The expected value says where a random quantity is centred; the standard deviation says how spread out it is.
  • The normal distribution is fixed by those two numbers. About 68% of values fall within one standard deviation.
  • A conditional probability narrows attention to the cases where something is known to be true.
  • “Chance of A given B” and “chance of B given A” are different numbers.
What you should be able to do
  • Work out the average and standard deviation of a small set of numbers.
  • Use the 68% and 95% rules to say where values from a bell curve will fall.
  • Read a conditional probability off a table of counts, in either direction.

Part I: Learning from Data

Chapter 1

What Is Learning?

How can a program get better at a task without anyone telling it the rules?

  • Machine learning is for tasks where nobody can write down the rule, but examples are available.
  • A dataset is a table of examples. Features are what you predict from; the label is what you predict.
  • A model is a rule with adjustable numbers. Training means choosing those numbers to fit the examples.
  • A model is only as good as its results on new data it has never seen.
  • Predicting a number is regression; predicting a category is classification.
What you should be able to do
  • Tell whether a task suits machine learning or is better solved with rules written by hand.
  • Point to the examples, the features and the label in a dataset.
  • Say whether a problem is regression or classification.
  • Explain why a model must be tested on examples it has not seen.
Chapter 2

Linear Regression

If a student studies for four and a half hours, what score should we expect?

  • A linear model predicts with a straight line, y^=wx+b\hat{y} = w x + b. Learning means choosing ww and bb.
  • A residual is one miss. The mean squared error combines all the misses into a single loss.
  • For a line and squared error, the best ww and bb come from a formula. No searching is needed.
  • Report RMSE for a typical miss in real units, and R² for how much better the line is than guessing the average.
  • Squared error is sensitive to outliers, and no line should be trusted outside the range of its data.
What you should be able to do
  • Turn an input into a prediction with a line, and say what the slope and bias mean.
  • Work out residuals and the mean squared error by hand.
  • Find the best line for a small dataset with the formula.
  • Report how good a fit is with RMSE and R², and say what each number means.
Chapter 3

Gradient Descent

If there were no formula for the best line, how could a computer still find it?

  • When there is no formula for the best model, follow the slope of the loss downhill instead of guessing.
  • The update is always the same: parameter minus learning rate times the steepness of the loss.
  • The gradient is the list of steepnesses, one per parameter. All parameters step at once.
  • Too small a learning rate is slow. Too large and the run diverges.
  • Mini-batches make each step cheap by estimating the gradient from a few random examples.
What you should be able to do
  • Carry out a gradient descent update by hand.
  • Say what the gradient is and why the step goes against it.
  • Tell from a loss curve whether the learning rate is too small, too large or about right.
  • Explain what mini-batches save, and what they cost.
Chapter 4

Generalization

If one model fits the data better than another, is it the better model?

  • Training error always improves with a more flexible model. It cannot tell you when to stop.
  • Underfitting is being too simple for the pattern. Overfitting is fitting the noise.
  • Judge models on data they have not seen: tune on a validation set, report on a test set used once.
  • Cross-validation rotates the hold-out so that small datasets are used fully.
  • Flexibility trades bias for variance. The best model keeps both small.
What you should be able to do
  • Recognise underfitting and overfitting from a pair of errors.
  • Split data into training, validation and test sets, and say what each is for.
  • Describe what happens in each round of cross-validation.
  • Explain bias and variance in plain words.
Chapter 5

Regularization

Can a flexible model be stopped from overfitting without making it less flexible?

  • Overfitted models have very large weights. Regularization makes large weights cost something.
  • The new loss is the error plus λ times the size of the weights. λ sets how much simplicity is worth.
  • Ridge (squared weights) shrinks all weights smoothly. Lasso (absolute weights) sets some to exactly zero.
  • Too little penalty overfits, too much underfits. Choose λ by cross-validation.
  • Put features on a common scale before penalising, or the penalty treats them unfairly.
What you should be able to do
  • Explain why large weights are a sign of overfitting.
  • Write down the ridge and lasso losses and say how they differ.
  • Predict what happens to the weights, the training error and the error on new data as λ grows.
  • Choose λ without touching the test set.

Part II: Classification

Chapter 6

Logistic Regression

A student studied for four and a half hours. How likely are they to pass?

  • For a yes/no question, predict a probability. A straight line cannot do that; a line passed through the sigmoid can.
  • Log loss scores the probability given to what actually happened, and punishes confident mistakes hardest.
  • There is no formula for the best weights, so the model is fitted by gradient descent. The gradient has the same form as for linear regression.
  • A threshold turns the probability into a decision. 50% is a default, not a law.
  • The decision boundary of logistic regression is always straight.
What you should be able to do
  • Turn a score into a probability with the sigmoid.
  • Work out the log loss of a prediction and say why confident mistakes cost so much.
  • Find where a model’s decision boundary lies for a given threshold.
  • Read a weight as a multiplier on the odds.
Chapter 7

Measuring Classifiers

A classifier is right 90% of the time. Is it any good?

  • Accuracy misleads when one class is rare. A model can score well by ignoring the cases that matter.
  • The confusion matrix separates the two kinds of mistake: false alarms and misses.
  • Precision asks how trustworthy a flag is. Recall asks how many real cases were found. Raising one lowers the other.
  • The ROC curve shows every threshold at once, and AUC scores the model without choosing one.
  • The right threshold depends on what each mistake costs. 0.50 is rarely it.
What you should be able to do
  • Explain why accuracy misleads when one class is rare.
  • Fill in a confusion matrix and work out precision, recall and F1 from it.
  • Read an ROC curve and say what AUC measures.
  • Choose a threshold from the costs of the two kinds of mistake.
Chapter 8

Nearest Neighbours

Can you classify something without ever fitting a model?

  • k-NN classifies a new point by a vote among the k closest known points. It has no weights and no training.
  • Its boundary can take any shape, and adding data changes it immediately.
  • k sets the flexibility: 1 memorises noise, very large values blur everything. Choose it by cross-validation.
  • Distance depends on units, so rescale features first.
  • With many features all points are far apart, and nearness stops being informative.
What you should be able to do
  • Classify a new point by hand with k nearest neighbours.
  • Say what small and large values of k do to the boundary.
  • Explain why features must be rescaled before measuring distance.
  • Explain why nearness stops meaning much when there are many features.
Chapter 9

Naive Bayes

A message contains the word "free". How much more likely is it to be spam?

  • Bayes’ rule turns “how likely is this evidence if the answer is yes” into “how likely is yes given this evidence”. It needs the prior.
  • Rare things stay unlikely even after strong evidence. Ignoring how rare they are is the classic mistake.
  • Naive Bayes treats every feature as independent evidence and multiplies, or adds logs.
  • Smoothing stops one unseen word from vetoing everything else.
  • It is fast, needs little data and ranks well, but its probabilities are overconfident.
What you should be able to do
  • Use Bayes’ rule to turn “how likely is the evidence” into “how likely is the cause”.
  • Classify a short message by hand with Naive Bayes.
  • Explain what smoothing fixes.
  • Say why the method works despite its assumption being false, and where it should not be trusted.
Chapter 10

Support Vector Machines

When many lines separate two groups perfectly, which one should you pick?

  • Of all the boundaries that separate two classes, an SVM picks the one with the widest margin.
  • The boundary is fixed by the support vectors, the few points on the edge of the margin. The rest could be deleted.
  • A soft margin lets points cross, at a price set by C. Small C means a wider, more tolerant street.
  • A kernel replaces the dot product with another measure of similarity, giving curved boundaries from the same algorithm.
  • C and γ control flexibility, and are tuned together by cross-validation.
What you should be able to do
  • Explain why the widest margin is a sensible choice of boundary.
  • Tell which points are support vectors and what would happen if the others were removed.
  • Say what C trades off, and what small and large values do.
  • Describe what a kernel changes and how γ affects the boundary.

Part IV: Neural Networks

Chapter 14

A Single Neuron

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

Layers and Activations

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

Scoring a Classifier

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 kk classes should score about log⁡k\log k.
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

Backpropagation

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

Optimizers

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

Keeping Networks Honest

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

One Network, Many Jobs

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

A Real Dataset

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.
Search

    The Book of Machine Learning
    Search
    Reading settings
    Text size
    Colours

    Saved in this browser only.

    Keyboard shortcuts
    /
    Search the book
    ← →
    Previous and next chapter
    g
    Glossary
    c
    Table of contents
    e
    Jump to the exercises
    t
    Reading settings
    ?
    Show this list
    Esc
    Close a window or pop-in