Models and loss
A model is a function whose output depends on numbers called parameters. For example, the model predicts the price of a taxi ride from its distance in kilometers, and its parameters are and . When and , a ride of 4 km gets the prediction dollars.
The people who design a model choose the form of the function, and the form does not change when the model learns. The training chooses the parameters, using training data: inputs whose correct outputs we know. For example, a list of past rides with their prices is training data for the taxi model.
A language model works in the same way. Its input is a text, its output is a probability for each possible next token, a piece of a word (see Chapter 5), and it has billions of parameters.
1.1Loss
The loss is a number that measures how wrong the predictions of a model are on the training data. A smaller loss means better predictions, and the training looks for the parameters with the smallest loss.
As an example, consider the following three rides and the predictions of the model when and :
| ride | distance | price | prediction | error | squared error |
|---|---|---|---|---|---|
| 1 | 1 | 5 | 5 | 0 | 0 |
| 2 | 2 | 6 | 7 | 1 | 1 |
| 3 | 3 | 9 | 9 | 0 | 0 |
The mean squared error is the average of the squared errors. In this case, it is . The following picture shows the rides and the predictions:
The error of a ride is the vertical distance between its point and the line. For example, the parameters and predict 5, 6 and 7. Their loss is . This choice is worse.
Note that squaring makes large errors count more: an error of 2 counts 4 times as much as an error of 1. For this reason, the best parameters spread the error over all rides.
The parameters and predict two rides exactly, but they are not the best choice. The best choice is and 1. Its loss is .
1.2Choosing the parameters
A straightforward way to choose the parameters is to try many values and keep the pair with the smallest loss. For example, we can try the values 0, 0.5, 1, …, 5 for both and , calculate the loss of each of the pairs, and keep the best pair, which is and with the loss 0.25.
However, a model with parameters needs tries. Even gives 25,937,424,601 tries, and a language model has billions of parameters. Chapter 2 shows a method that changes all parameters at the same time, in the direction that makes the loss smaller.
1.3Loss for probabilities
Many models output a probability instead of a number. For example, a spam filter gives the probability that an email is spam. The cross-entropy loss of one example is , where is the probability that the model gives to the correct answer. The following table shows the loss for some values of :
| 0.9 | 0.5 | 0.1 | 0.01 | |
|---|---|---|---|---|
| 0.11 | 0.69 | 2.30 | 4.61 |
The loss is 0 only when the model gives the probability 1 to the correct answer. A wrong answer with high confidence costs much more than an unsure one. For example, the loss for is about 44 times the loss for .
A language model uses the same loss. For each position of a text, the loss is , where is the probability that the model gives to the token that actually comes next. The pretraining (see Chapter 9) makes the average of this loss small over trillions of tokens.
Note that a small loss on the training data does not mean good predictions for new inputs. A model can learn the training data by heart, and Chapter 4 shows how we can detect this.
Choosing the parameters with the smallest mean squared error is the method of least squares. It was published in 1805 by A.-M. Legendre: Nouvelles méthodes pour la détermination des orbites des comètes.↩︎