Chapter 1
What Is Machine Learning?
Textbooks: Goodfellow ch. 1, §5.1 · Murphy §1.1–1.3
A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.
Murphy (§1.1) defines machine learning as a set of methods that can automatically detect patterns in data, and then use the uncovered patterns to predict future data or to make other kinds of decisions under uncertainty. Instead of writing every rule by hand, you show the machine examples and let it find the rules.
Three kinds of learning
- Supervised learning learns a function that maps inputs x to outputs y from training data made of input–answer pairs. When y is a category (spam or legitimate email), the task is classification. When y is a real number (a house price, a temperature), it is regression.
- Unsupervised learning receives only inputs, with no answers, and finds structure in the data. Clustering similar items and reducing data to a few axes are the classic examples. Nothing says in advance what to look for, so the problem is less clearly defined than supervised learning.
- Reinforcement learning learns how to act from occasional rewards or punishments. Murphy gives the example of a baby learning to walk.
Each item of the input is a feature. The input can be a vector of numbers, such as a person’s height and weight, or a structured object such as an image, a sentence, or a time series (Murphy §1.1.1).
Which kind of learning problem?
Pick the right type of learning for each situation. Each choice reveals the reasoning.
Chapter 2
Probability and Bayes’ Rule
Textbooks: Murphy §2.2–2.4 · Goodfellow §3.5, 3.9, 3.11
Machine learning deals with uncertainty. Data are noisy, and the same input often has no single right answer. That is why a model’s output is often a probability, such as “this email is spam with probability 0.93.”
A conditional probability \(p(A\mid B)\) is the probability of A given that B has happened. Bayes’ rule turns a probability that runs from cause to effect, p(effect | cause), into one that runs back from effect to cause, p(cause | effect).
\[\begin{aligned}p(y\mid x)&=\frac{p(x\mid y)\,p(y)}{p(x)}\\ p(x)&=\sum_{y'} p(x\mid y')\,p(y')\end{aligned}\]\(p(y)\) is the prior, the belief before seeing data; \(p(x\mid y)\) is the likelihood; and \(p(y\mid x)\) is the posterior, the belief after seeing data (Murphy eq. 2.7).
Take Murphy’s example (§2.2.3.1). A woman in her 40s has a mammogram for breast cancer, and the result is positive. The test’s sensitivity is 80%, meaning that if she has cancer, the test is positive with probability 0.8. Is the chance that she has cancer therefore 80%? Many people answer yes, and they are wrong. The prior probability of breast cancer at this age is only 0.4%, and even without cancer the test is positive 10% of the time. Bayes’ rule gives an actual probability of about 3%. Ignoring the prior in this way is the base rate fallacy.
The chance of disease after a positive test
Each cell is one person who took the test. The defaults are Murphy’s mammogram example. Change the prior and watch the same test mean something very different.
The normal distribution
The most widely used distribution for continuous values is the normal (Gaussian) distribution \(\mathcal{N}(x;\mu,\sigma^2)\). It is bell-shaped around the mean \(\mu\), and the standard deviation \(\sigma\) sets its spread. The linear regression of chapter 3 can be read as assuming “answer = straight line + Gaussian noise” (Goodfellow §3.9.3, 5.5.1).
Chapter 3
Linear Regression and Least Squares
Textbooks: Goodfellow §5.1.4, 4.5 · Murphy §1.4.5, 7.1–7.3
Linear regression is the simplest model for predicting a real number y from an input x. With a single input, the prediction is a straight line \(\hat{y}=wx+b\), and the slope \(w\) and intercept \(b\) are the model’s parameters. Learning means finding the \(w\) and \(b\) that explain the data best.
The yardstick for “best” is the cost function. Linear regression usually uses the mean squared error (MSE), the average of the squared differences (residuals) between predictions and answers. Because the errors are squared, points that are far off are penalized much more heavily.
\[\begin{aligned}\mathrm{MSE}(w,b)&=\frac{1}{m}\sum_{i}\left(wx_i+b-y_i\right)^2\\ \mathbf{w}^{*}&=\left(X^{\top}X\right)^{-1}X^{\top}\mathbf{y}\end{aligned}\]\(m\) is the number of training examples. Setting the gradient to zero yields the normal equations (Goodfellow eq. 5.12, Murphy §7.3.1). \(X\) is the matrix whose rows are \([x_i,\ 1]\).
Seeing the squared error
Move the line with the sliders or drag the points. Each point gets a square whose side is its residual. The MSE is the average area of these squares.
Drag one point far from the others. That single point pulls the whole line toward it, which is why squared error is sensitive to outliers (Murphy §7.4).
Chapter 4
Gradient Descent
Textbooks: Goodfellow §4.3, 5.9 · Murphy §8.3.2, 8.5.2
Linear regression has a closed-form solution, but most models do not. Instead we lower the cost step by step. The gradient \(\nabla f\) points in the direction in which the function rises most steeply. A small step in the opposite direction lowers the cost. This is gradient descent.
\[\mathbf{x}'=\mathbf{x}-\epsilon\,\nabla_{\mathbf{x}}f(\mathbf{x})\]\(\epsilon\) is the learning rate, the size of each step (Goodfellow eq. 4.5). It is set by hand before training, so it is called a hyperparameter.
A learning rate that is too small converges slowly, and one that is too large can jump across the valley and make the cost grow (Goodfellow §4.3.1). When the cost surface is stretched in one direction, that is, when the Hessian has a large condition number, gradient descent zigzags between the valley walls and slows down.
With large datasets, computing the gradient over all the data at every step is expensive. Stochastic gradient descent (SGD) estimates the gradient from a small random batch of examples, a minibatch. The path wobbles, but each step is much cheaper, and SGD has become the workhorse of deep learning (Goodfellow §5.9).
Walking down the cost surface
The background is the linear-regression cost MSE(w, b). Brighter means higher cost. Tap the surface to set a starting point, then run. Change the learning rate, and switch on minibatches to see the path wobble.
Chapter 5
Overfitting and Generalization
Textbooks: Goodfellow §5.2–5.4 · Murphy §1.4.7–1.4.8, 6.4.4, 7.5
The goal of machine learning is to do well on data the model has never seen, not to memorize the training data. This ability is generalization. The error on the training data is the training error, and the expected error on new data is the generalization error (test error).
The variety of functions a model can represent is its capacity. With too little capacity the model cannot even explain the training data, which is underfitting. With too much, it memorizes the noise in the training data and fails on new data, which is overfitting. Goodfellow’s figure 5.2 fits polynomials of degree 1, 2, and 9 to data generated from a quadratic. The line misses the curvature, and the degree-9 polynomial passes through every point but swings wildly between them.
Regularization is another way to rein in capacity. Weight decay adds the sum of squared weights to the cost and suppresses large coefficients the data do not require. With a suitable \(\lambda\), even a degree-9 polynomial recovers a shape close to the quadratic (Goodfellow fig. 5.5; ridge regression in Murphy §7.5).
\[J(\mathbf{w})=\mathrm{MSE}_{\text{train}}+\lambda\,\mathbf{w}^{\top}\mathbf{w}\]A larger \(\lambda\) favors simpler functions, and \(\lambda=0\) means no regularization (Goodfellow eq. 5.18).
Polynomial degree and generalization
The true function (dashed) is a quadratic, and the training points (filled) add noise to it. Raise the degree to overfit, then rescue the fit with more points or a larger λ. The chart on the right shows training and test error for every degree.
Validation sets and cross-validation
If you choose hyperparameters such as the degree or \(\lambda\) using the test data, the test data are no longer “unseen.” So you set aside part of the training data as a validation set. Goodfellow (§5.3.1) notes that one typically trains on about 80 percent of the training data and validates on 20 percent. With little data, k-fold cross-validation splits the data into k parts and takes turns validating on each.
No single model is best for every problem. This is the no free lunch theorem (Murphy §1.4.9). Each problem needs its own choice of model and hyperparameters, made on validation data.
Chapter 6
Classification: k-Nearest Neighbors and Logistic Regression
Textbooks: Murphy §1.4.2, 1.4.6, 8.1–8.3 · Goodfellow §5.7.1, 5.7.3, 6.2.2
k-nearest neighbors (k-NN)
The simplest classifier looks at the K training points closest to a new point and picks the majority class among them (Murphy §1.4.2). It has no parameters to learn; the training data themselves are the model. Models like this are nonparametric. A small K lets single training points bend the boundary, and a large K smooths it.
\[p(y=c\mid \mathbf{x},\mathcal{D},K)=\frac{1}{K}\sum_{i\in N_K(\mathbf{x})}\mathbb{I}(y_i=c)\]\(N_K(\mathbf{x})\) is the set of the \(K\) training points nearest to \(\mathbf{x}\), and \(\mathbb{I}\) is the indicator function, which is 1 when its condition is true (Murphy eq. 1.2).
How K shapes the decision boundary
The background shows the class k-NN assigns to each location. Start at K = 1 and increase it. The chart on the right shows training and test accuracy for each K.
Logistic regression
Logistic regression feeds the linear output \(\mathbf{w}^{\top}\mathbf{x}+b\) into the sigmoid function \(\sigma(z)=1/(1+e^{-z})\), turning it into a probability between 0 and 1. Despite its name, it is a classification model. The decision boundary is where \(p(y=1\mid\mathbf{x})=0.5\), the line \(\mathbf{w}^{\top}\mathbf{x}+b=0\) (Murphy §8.2).
Its cost function is the cross-entropy (negative log-likelihood), the average negative log of the probability given to the correct class. Assigning high probability to the right answer gives a small cost; assigning it low probability makes the cost climb steeply. There is no closed-form solution like the normal equations, so it is solved by gradient descent (Murphy §8.3, Goodfellow §5.7.1).
\[\begin{aligned}J(\mathbf{w},b)=-\frac{1}{m}\sum_{i}\Big[\,&y_i\log\sigma(\mathbf{w}^{\top}\mathbf{x}_i+b)\\ &+(1-y_i)\log\big(1-\sigma(\mathbf{w}^{\top}\mathbf{x}_i+b)\big)\Big]\end{aligned}\]The gradient is \(\nabla_{\mathbf{w}}J=\frac{1}{m}\sum_i\big(\sigma(\mathbf{w}^{\top}\mathbf{x}_i+b)-y_i\big)\mathbf{x}_i\), the same form as the linear-regression gradient (Murphy eq. 8.5).
Watching logistic regression learn
Press Train to run gradient descent step by step. The shading shows the probability and the solid line is the p = 0.5 boundary. Try making the classes overlap more.
Chapter 7
Model Evaluation: Accuracy Is Not Enough
Textbooks: Goodfellow §5.3, 11.1 · Murphy §5.7.2
Here is Goodfellow’s example (§11.1). Suppose we build a test for a rare disease that affects one person in a million. A classifier that always answers “no disease” scores 99.9999 percent accuracy. A model can have very high accuracy and still be useless.
So we look at the positive class separately. Precision is the fraction of the model’s positive calls that are truly positive, and recall is the fraction of true positives that the model finds. Their harmonic mean, the F-score, combines the two. Precision is the same quantity as the posterior p(disease | positive) computed with Bayes’ rule in chapter 2.
A classifier usually outputs a score or probability and reports a positive when that value exceeds a threshold. Lowering the threshold finds more cases (higher recall) but raises more false alarms (lower precision), and raising it does the opposite. The precision–recall curve (PR curve), traced as the threshold moves, shows this trade-off. Which kind of error costs more depends on the problem (Murphy §5.7.2).
\[\begin{aligned}\text{precision}&=\frac{TP}{TP+FP}\\ \text{recall}&=\frac{TP}{TP+FN}\\ F&=\frac{2pr}{p+r}\end{aligned}\]\(TP\) counts correctly found positives, \(FP\) false alarms, and \(FN\) missed positives (Goodfellow §11.1).
Metrics as threshold and prevalence change
The top chart shows the classifier’s score distributions for negative (blue) and positive (orange) cases, and the vertical line is the threshold. Move the threshold and the prevalence. Lower the prevalence and the same classifier’s precision collapses.
Chapter 8
Unsupervised Learning: k-Means and PCA
Textbooks: Goodfellow §2.12, 5.8 · Murphy §1.3, 11.4.2.5, 12.2, 25.1
k-means clustering
k-means splits the data into K groups. After placing K centers, it alternates two steps: assign each point to the group of its nearest center, then move each center to the mean of its group’s points. It stops when no assignment changes (Goodfellow §5.8.2, Murphy algorithm 11.1).
Each iteration reduces the sum of squared distances from points to their centers (the distortion), but the answer it reaches is not guaranteed to be the best one. The result depends on where the centers start. Choosing K is hard too, because the distortion keeps falling as K grows. Murphy (§11.4.2.5) presents k-means as a simplified form of the EM algorithm for Gaussian mixture models.
k-means one step at a time
Step through assignment and update yourself. The crosses are the centers. Draw new initial centers a few times and the final grouping may change.
Principal components analysis (PCA)
PCA summarizes data with fewer axes. It finds the axis that loses the least information (the reconstruction error) when the data are projected onto it and mapped back. That axis is also the direction in which the projected data have the largest variance, the eigenvector of the covariance matrix with the largest eigenvalue (Goodfellow §2.12, Murphy §12.2.1). The new coordinates PCA produces are uncorrelated with one another (Goodfellow §5.8.1).
Maximizing variance is minimizing reconstruction error
Rotate the axis. The thin lines from each point to the axis are the reconstruction errors. The projected variance and the reconstruction error always add up to the same total.
Chapter 9
Neural Networks: From XOR to the Multilayer Perceptron
Textbooks: Goodfellow §6.1–6.4 · Murphy §16.5, 28.3
The smallest problem a linear model cannot solve is XOR, which outputs 1 exactly when one of its two inputs is 1. Goodfellow (§6.1) fits a linear model to the four points by least squares and gets \(w=0\), \(b=0.5\), an answer of 0.5 for every input. The effect of \(x_2\) must flip depending on whether \(x_1\) is 0 or 1, but a linear model can give \(x_2\) only one coefficient.
The fix is a hidden layer that turns the input into a new representation. The hidden layer computes \(\mathbf{h}=g(W^{\top}\mathbf{x}+\mathbf{c})\), and the output layer applies a linear model on top of \(\mathbf{h}\), \(y=\mathbf{w}^{\top}\mathbf{h}+b\). Here \(g\) is the activation function. Without \(g\), any stack of layers is still one linear function, so the extra layers buy nothing. The default recommendation today is the ReLU, \(g(z)=\max(0,z)\).
\[W=\begin{pmatrix}1&1\\1&1\end{pmatrix},\quad \mathbf{c}=\begin{pmatrix}0\\-1\end{pmatrix},\quad \mathbf{w}=\begin{pmatrix}1\\-2\end{pmatrix},\quad b=0\]This is the XOR solution given in Goodfellow eqs. 6.4–6.6. After the ReLU, (0,1) and (1,0) collapse onto the single hidden point (1,0), and one straight line separates the four points.
How a hidden layer unfolds XOR
The left panel is the original input space and the right panel is the space the hidden layer creates. Turn the ReLU off and the four points line up on one line, with no way to separate them. Turn it on and the two points with output 1 merge into one.
Universal approximation and depth
The universal approximation theorem says that a network with even a single hidden layer can approximate a continuous function as accurately as desired, given enough hidden units (Goodfellow §6.4.1). Goodfellow adds that the layer may be infeasibly large and may fail to learn and generalize correctly. In many cases, deeper models represent the same function with fewer units and reduce generalization error.
Neural network playground
Choose a dataset and an architecture, then press Train. The network learns with Adam (chapter 11). With only one or two hidden neurons it cannot untangle the spirals; add layers and neurons and it can.
Chapter 10
Back-Propagation
Textbooks: Goodfellow §6.5, 6.3.2, 8.2.5, 11.5 · Murphy §16.5.4
Gradient descent needs the gradient of the cost with respect to every parameter. A neural network is a composition of many functions, so the gradient comes from the chain rule of calculus: if \(y=g(x),\ z=f(y)\), then \(\dfrac{dz}{dx}=\dfrac{dz}{dy}\dfrac{dy}{dx}\) (Goodfellow eq. 6.44).
It helps to draw the computation as a computational graph, with one node per operation. The forward pass computes values from the inputs toward the output, and back-propagation runs from the output back toward the inputs, computing the gradient at each node. Back-propagation stores intermediate results and reuses them, which avoids recomputing the same subexpressions over and over (Goodfellow §6.5).
Hand-written back-propagation code is easy to get wrong. Goodfellow (§11.5) recommends comparing back-propagated derivatives with numerical derivatives computed by finite differences. The experiment below shows that comparison.
Forward and backward pass for one neuron
A single neuron computes \(a=\sigma(wx+b)\), and the cost is \(L=(a-y)^2\). Change the values and watch the numbers above the nodes (forward values) and the red numbers below them (back-propagated gradients) change together.
Vanishing and exploding gradients
In a deep network the chain-rule product gets long. If each layer multiplies by a value below 1, the gradient shrinks exponentially (vanishes), and if each multiplies by a value above 1, it grows exponentially (explodes) (Goodfellow §8.2.5). The sigmoid saturates over much of its domain, where its gradient is nearly zero, and its derivative never exceeds 0.25. That is why it is rarely used in the hidden layers of deep networks (Goodfellow §6.3.2).
The gradient shrinks layer by layer
In this simplification, each layer multiplies the gradient by a weight w and by the derivative of the activation function. The vertical axis is logarithmic.
Chapter 11
Training Deep Networks: Optimization and Regularization
Textbooks: Goodfellow §7.8, 7.12, 8.1–8.5 · Murphy §8.5, 16.5.6
Momentum and Adam
In a long, narrow valley, plain gradient descent zigzags slowly downhill. Momentum accumulates the direction of past steps in a velocity v. It speeds up along consistent directions, and directions that keep reversing cancel out. The update is \(\mathbf{v}\leftarrow\alpha\mathbf{v}-\epsilon\nabla J,\ \boldsymbol{\theta}\leftarrow\boldsymbol{\theta}+\mathbf{v}\), and common values of \(\alpha\) are 0.5, 0.9, and 0.99 (Goodfellow §8.3.2).
Adam is an adaptive method that sets a separate learning rate for each parameter. It tracks both the mean of the gradient (the first moment) and the mean of its square (the second moment). The suggested defaults in Goodfellow’s algorithm 8.7 are a step size of 0.001, decay rates \(\rho_1=0.9\) and \(\rho_2=0.999\), and a stabilizing constant of \(10^{-8}\) (§8.5.3). It is fairly robust to its hyperparameters and widely used.
A race between three optimizers
All three start from the same point in a long, tilted valley (condition number 25). Tap the surface to move the start. Adam’s step size is set to 0.1 to suit the scale of this surface.
Early stopping and dropout
Train a large network for long enough and the training loss keeps falling while the validation loss eventually starts to rise again. Goodfellow’s figure 7.3 shows this asymmetric U-shaped curve. Early stopping saves the parameters from the point where the validation loss was lowest and returns them as the result (§7.8). It is the most commonly used form of regularization.
Dropout randomly switches off some neurons at every training step. Typically an input unit is kept with probability 0.8 and a hidden unit with probability 0.5 (§7.12). The network can no longer rely on any particular neuron, and the effect resembles training an ensemble of many smaller networks. At evaluation time all neurons are on.
Learning curves and early stopping
A large network (2 hidden layers × 20 neurons) trains on a small, noisy dataset of 40 points. The vertical line marks the epoch with the lowest validation loss. Turn on dropout and see how the curves and the boundary change.
Chapter 12
Convolutional Networks
Textbooks: Goodfellow §9.1–9.3 · Murphy §16.5.1
An image is data laid out on a grid of pixels. Convolution slides a small grid of weights, the kernel, across the image and sums the products of the kernel and the pixels it covers. The result is a feature map. Mathematical convolution flips the kernel, but most machine learning libraries implement the related cross-correlation, without the flip, and call it convolution (Goodfellow §9.1).
Goodfellow (§9.2) names three ideas behind convolution: sparse interactions, where each output connects to a small region of the input; parameter sharing, where the same kernel is reused at every position; and equivariance to translation, where shifting the input shifts the output by the same amount. In figure 9.6, finding the vertical edges of a 280 × 320 image takes a kernel with just two elements. Writing the same transformation as a matrix multiplication would need over eight billion matrix entries.
A typical convolutional layer has three stages: several convolutions in parallel, a nonlinearity such as the ReLU (the detector stage), and pooling (§9.3). Max pooling keeps only the largest value in each neighborhood, so the output barely changes when the input shifts slightly.
Changing the kernel and watching the feature map
Pick a kernel or edit its numbers. In the feature map on the right, orange is positive and blue is negative. Tap a cell of the feature map to see the input region and the calculation that produced it.
Tap a cell of the feature map to see its calculation here.
Chapter 13
Recurrent Networks and the Rise of Attention
Textbooks and papers: Goodfellow §10.1–10.2, 10.7, 10.10, 12.4.5.1 · Bahdanau et al. (2015)
Ordered data such as sentences, speech, and time series come in many lengths. A recurrent neural network (RNN) reuses the same weights at every time step and carries a summary of everything it has seen so far in its hidden state \(\mathbf{h}\), passing it on to the next step. Unfolded along time, it looks like a deep network with one layer per time step (Goodfellow §10.1).
\[\begin{aligned}\mathbf{a}^{(t)}&=\mathbf{b}+W\mathbf{h}^{(t-1)}+U\mathbf{x}^{(t)}\\ \mathbf{h}^{(t)}&=\tanh\!\left(\mathbf{a}^{(t)}\right)\end{aligned}\]\(\mathbf{x}^{(t)}\) is the input at time \(t\), \(W\) the weights that connect one step to the next, and \(U\) the input weights. Every time step uses the same \(W\) and \(U\) (Goodfellow eqs. 10.8–10.9).
The problem of long-term dependencies
An RNN composes the same function again and again, once per time step. Gradients arriving from the distant past therefore tend to vanish, as in chapter 10, and occasionally explode (Goodfellow §10.7). Dropping the nonlinearity and simplifying to \(\mathbf{h}^{(t)}=W^{\top}\mathbf{h}^{(t-1)}\), the influence after \(t\) steps shrinks or grows with the eigenvalues of \(W\) raised to the power \(t\). The LSTM greatly eased this problem by learning, through input, forget, and output gates, how much information to keep (§10.10).
The fixed-length-vector bottleneck
An encoder–decoder RNN for translation compresses the whole source sentence into one fixed-length vector and generates the translation from that vector. Bahdanau et al. (2015) conjectured that this fixed-length vector is a bottleneck. They let the decoder, each time it produces a word, softly search the source sentence for the relevant parts and take a weighted average of them. This is attention. In Goodfellow’s words (§12.4.5.1), the model reads the whole sentence to get the gist and then produces the translated words one at a time, each time focusing on a different part of the input.
Vaswani et al. (2017) went one step further with the Transformer, which processes sentences with attention alone and no recurrence. A recurrent layer must compute its time steps in order, but self-attention computes every position at once. The path between two distant words is \(O(n)\) steps long in an RNN but \(O(1)\) in self-attention (Vaswani et al., table 1).
Recurrent layers versus self-attention
The left chart shows how much of the first word’s gradient survives to the n-th word, and the right chart shows the computation per layer (the orders of growth in Vaswani et al., table 1). Self-attention connects any two positions in a single step, so only the RNN curve falls on the left.
Chapter 14
The Transformer
Paper: Vaswani et al., “Attention Is All You Need” (NeurIPS 2017) · Goodfellow §12.4.5.1
The Transformer is an encoder–decoder model built from attention alone, with no recurrence and no convolution. The encoder stacks six identical layers (\(N=6\)), each made of multi-head self-attention followed by a position-wise feed-forward network. The decoder also has six layers, plus one more attention sub-layer that looks at the encoder’s output. Each sub-layer is wrapped in a residual connection and layer normalization, so its output is \(\mathrm{LayerNorm}(x+\mathrm{Sublayer}(x))\), and every layer outputs vectors of dimension \(d_{\text{model}}=512\) (Vaswani et al. §3.1).
Queries, keys, and values
Attention takes one query and a set of key–value pairs and returns a weighted sum of the values. The weights depend on how well the query matches each key. In a library, the query is the topic you are looking for, the keys are the titles on the spines, and the values are the contents of the books. The Transformer divides the dot product of query and key by \(\sqrt{d_k}\) and applies a softmax. This is scaled dot-product attention.
\[\mathrm{Attention}(Q,K,V)=\mathrm{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d_k}}\right)V\]\(Q, K, V\) stack the queries, keys, and values as rows, and \(d_k\) is the dimension of the keys (Vaswani et al. eq. 1). In self-attention all three come from the same sentence.
Computing attention weights by hand
Pick a query word. The weights it gives to each word of the sentence are computed below. The two heads are hand-made examples to show the principle; the heads of a real model are learned. Turn on the mask and, as in a decoder, no word can see the words after it.
Why divide by \(\sqrt{d_k}\)?
If the components of the query and key are independent with mean 0 and variance 1, their dot product has mean 0 and variance \(d_k\). With large \(d_k\), the dot products spread far apart and the softmax gives almost all of the weight to the largest one. In that region the softmax has extremely small gradients and learning stalls. Dividing by \(\sqrt{d_k}\) brings the variance back to 1 (Vaswani et al. §3.2.1, footnote 4).
Without scaling, the softmax tips to one side
One random query and eight random keys are drawn, with components of mean 0 and variance 1. Increase the dimension \(d_k\) and compare the weights before and after scaling.
Multi-head attention and masking
A single attention function blurs several relationships into one average. The Transformer instead projects the queries, keys, and values \(h\) times with different learned matrices, computes attention for each projection, concatenates the results, and projects them once more. This is multi-head attention. The original paper uses \(h=8\) heads with \(d_k=d_v=512/8=64\) each. Because each head is smaller, the total computational cost is similar to that of a single head with full dimensionality (§3.2.2). Like the two heads of experiment 14-1, different heads can attend to different relationships.
The decoder produces the translation one word at a time, so position \(i\) must not peek at later words. The decoder’s self-attention therefore sets the scores of later positions to \(-\infty\) before the softmax, which makes their weights zero. This mask, together with shifting the output embeddings by one position, ensures that the prediction for position \(i\) depends only on the words before it (§3.1, 3.2.3).
Positional encoding
Attention treats the words like a set, so on its own it cannot tell their order. The Transformer therefore adds a positional encoding of the same dimension to each word embedding. The original paper uses sines and cosines with a different frequency in each dimension, with wavelengths forming a geometric progression from \(2\pi\) to \(10000\cdot 2\pi\). The authors expected this to make relative positions easy to learn and to extend to sentences longer than those seen in training. Learned positional embeddings performed nearly the same (§3.5).
\[\begin{aligned}PE_{(pos,\,2i)}&=\sin\!\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)\\ PE_{(pos,\,2i+1)}&=\cos\!\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)\end{aligned}\]\(pos\) is the position and \(i\) is the dimension index (Vaswani et al. §3.5).
The pattern of positional encodings
The left panel colors the positional-encoding vector (across) for each position (down). Early dimensions oscillate quickly and later ones slowly. The right panel shows the dot product between the chosen position’s vector and every other position’s. Nearby positions score higher.
The feed-forward layer and model size
After attention comes a two-layer network, \(\mathrm{FFN}(x)=\max(0,\,xW_1+b_1)W_2+b_2\), applied to each position separately and identically. Its inner dimension is \(d_{\text{ff}}=2048\) (§3.3). Words become vectors through learned embeddings, and the two embedding layers share one weight matrix with the linear transformation just before the softmax (§3.4).
The paper’s base model has 65 million parameters and trained for 100,000 steps, about 12 hours, on eight P100 GPUs. The big model has 213 million parameters and trained for 3.5 days. On WMT 2014 English-to-German translation, the big model reached 28.4 BLEU, more than 2.0 BLEU above the best previously reported models, including ensembles (§5.2, 6.1, tables 2–3).
Transformer parameter calculator
Choose the paper’s base or big configuration, or change the values. The bar shows where the parameters live. Within each layer, the FFN holds nearly twice as many parameters as attention.
One attention block has \(W^Q, W^K, W^V, W^O\) and biases, \(4d^2+4d\) parameters, and an FFN has \(2d\,d_{\text{ff}}+d_{\text{ff}}+d\). An encoder layer uses one attention block and a decoder layer uses two. The number of heads \(h\) only splits the computation and does not change the parameter count. The totals differ slightly from the paper’s table 3 (base 65M, big 213M), since the vocabulary is entered as “about 37,000” and the count depends on how biases and normalization terms are tallied.
After the Transformer
- BERT (Devlin et al. 2019) pre-trains a Transformer encoder on bidirectional context, then fine-tunes it for each task.
- GPT (Radford et al. 2018) generatively pre-trains a 12-layer masked Transformer decoder on unlabeled text, then fine-tunes it. It is the starting point of today’s large language models.
- The Vision Transformer (Dosovitskiy et al. 2021) cuts an image into 16×16 patches and feeds them in like words, showing that a pure Transformer can classify images very well.
- Autoencoders and representation learning (Goodfellow ch. 14–15), deep generative models (ch. 20), and the probabilistic view of models (Murphy ch. 5, 10, 11, 15) are good next topics.
Appendix A
Glossary
No matching terms. Try an abbreviation or a different word.
Appendix B
Textbook Map
This table matches the chapters of this guide to the chapters and sections of the two textbooks. Both textbooks predate the Transformer (2017), so chapters 13–14 also rest on the papers listed below.
| Chapter | Goodfellow et al. 2016 | Murphy 2012 |
|---|---|---|
| 1 What is ML | 1, 5.1 | 1.1–1.3 |
| 2 Probability and Bayes | 3.5, 3.9, 3.11 | 2.2–2.4 |
| 3 Linear regression | 4.5, 5.1.4 | 1.4.5, 7.1–7.4 |
| 4 Gradient descent | 4.3, 5.9 | 8.3.2, 8.5.2 |
| 5 Overfitting and generalization | 5.2–5.4 | 1.4.7–1.4.9, 6.4.4, 7.5 |
| 6 Classification | 5.7.1, 5.7.3, 6.2.2 | 1.4.2, 1.4.6, 8.1–8.3 |
| 7 Model evaluation | 5.3, 11.1 | 5.7.2 |
| 8 Unsupervised learning | 2.12, 5.8 | 1.3, 11.4.2.5, 12.2, 25.1 |
| 9 Neural networks | 6.1–6.4 | 16.5, 28.3 |
| 10 Back-propagation | 6.3.2, 6.5, 8.2.5, 11.5 | 16.5.4 |
| 11 Optimization and regularization | 7.8, 7.12, 8.1–8.5 | 8.5, 16.5.6 |
| 12 Convolutional networks | 9.1–9.3 | 16.5.1 |
| 13 RNNs and attention | 10.1–10.2, 10.7, 10.10, 12.4.5.1 | – (Bahdanau et al. 2015) |
| 14 The Transformer | 12.4.5.1 | – (Vaswani et al. 2017) |
Textbooks
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.
- Murphy, K. P. (2012). Machine Learning: A Probabilistic Perspective. MIT Press.
Papers
- Bahdanau, D., Cho, K., & Bengio, Y. (2015). Neural machine translation by jointly learning to align and translate. ICLR. arXiv:1409.0473
- Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention is all you need. NeurIPS. arXiv:1706.03762
- Radford, A., Narasimhan, K., Salimans, T., & Sutskever, I. (2018). Improving language understanding by generative pre-training. OpenAI. PDF
- Devlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2019). BERT: Pre-training of deep bidirectional transformers for language understanding. NAACL. arXiv:1810.04805
- Dosovitskiy, A., et al. (2021). An image is worth 16×16 words: Transformers for image recognition at scale. ICLR. arXiv:2010.11929