Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Dropout

In the previous chapter, we added layer normalization to the GPT model, which keeps the values and the training stable after the residual connections. We also mentioned a possible problem of large-scale training: overfitting.

The model performs well on the training set but may perform poorly on the test set. It has memorized too many details of the training data instead of truly learning the patterns of language. It is like a student who just memorizes all the practice problems by rote and is at a loss when facing new problems in the exam.


Dropout is a classic technique against overfitting: during training, randomly drop the outputs of a part of the neurons (set them to 0), so that the model cannot depend too much on any single neuron:

  • During training: randomly set the outputs of a certain proportion pp (for example 0.1) of the neurons to 0, and divide the remaining outputs by 1−p1 - p to keep the expected value unchanged;

  • During inference: do not drop any neuron, and use all the outputs.

GPT basic modules with dropout: dropout comes after the embedding layer and before the residual connections of the attention layer and the feed-forward network layer
Figure: GPT basic modules with dropout

Dataset

Character-encoding dataset

Lower triangular matrix layer

Normalization layer

Dropout layer

The dropout layer (the Dropout layer) randomly drops the outputs of a part of the neurons during training:

  • Forward pass:

    • During training: generate a random mask with the same shape as the input, keep each value with probability 1−p1 - p (with a scaling factor of 1/(1−p)1 / (1 - p)) and drop it with probability pp (the value becomes 0);

    • During inference: return the input directly;

  • Backward pass: the gradient flows back only through the neurons that were kept, and the gradient of the dropped neurons is 0.

💡 Tip: dividing the kept input values by 1−p1 - p keeps the overall expected value of the output unchanged, so that the output scale is the same in training and in inference.

Activation functions

Gaussian error linear unit (GELU)

Loss function

GPT cross-entropy loss function

Model

GPT embedding layer

After the word vector and the position vector are combined, we add dropout.

GPT attention layer

In the attention layer, dropout is added before the residual connection.

GPT feed-forward network layer

Likewise, in the feed-forward network layer, dropout is also added before the residual connection.

GPT Transformer block

GPT output layer

GPT model

GPT trainer

Hyperparameters

Learning rate

Batch size

Number of epochs

Context size

Embedding size

Number of heads

Number of Transformer blocks

Modeling

Training

Evaluation

prediction:	1742 steps, each (4, 32, 65)
loss:	1.8636507043608568

Inference

ROMEO:
O:
O deseep would my lethis this they-anio, you hale should, gentis,
I'll I
Boy, this I have of del mogriess.

Secondself faic, not lifect were Clains; i' be So thour man I complo, and do hand
Mistrue such to which a city his old vice frieunts not man,
An

In this chapter, we added dropout to the GPT model: during training, it randomly drops the outputs of a part of the neurons to prevent overfitting.

With this, the three major engineering optimization techniques for pre-training are all in place:

  • residual connection: lets the gradient flow smoothly and prevents vanishing gradients;

  • layer normalization: keeps the values stable, and training stable;

  • dropout: prevents overfitting.

With these engineering optimization techniques, the model can be stacked higher, trained more stably, and generalize better.

Exercises

  1. Try changing the dropout probability pp from 0.1 to 0.3 or even 0.5 and observe how the training results change. Is a larger probability always better?