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.

Model persistence

In the previous chapter, we gave the GPT model the ability of dropout, and the three major engineering optimization techniques for pre-training are complete.

Now let us consider the last important problem of model pre-training: how to save the training results.


Training a GPT model takes hours or even days. Obviously, we cannot retrain it every time we use it. We need to save the trained model so that we can load and use it directly next time.

What to save

All the knowledge of the model is in its trainable parameters: the weights and biases of every layer. We only need to save these values, not the code, the computational graph, or the gradients.

Save format

NumPy provides the np.savez function, which can save multiple arrays into a single file (an archive file in the .npz format).

All the trainable parameters of the GPT model are stored in the model’s parameter list model.parameters, and we only need to save each of them to the file in turn.

When to save

During training, we can evaluate the performance of the model on the test set after each epoch. If the loss of the current epoch on the test set reaches a new low, we save the model. This way, we make sure that the best training result is kept.

How to load

Next time we use the model, we can read the parameters from the .npz file and assign them in turn to the model parameters model.parameters.

Dataset

Character-encoding dataset

Lower triangular matrix layer

Normalization layer

Dropout layer

Activation functions

Gaussian error linear unit (GELU)

Loss function

GPT cross-entropy loss function

Model

GPT embedding layer

GPT attention layer

GPT feed-forward network layer

GPT Transformer block

GPT output layer

GPT model

GPT trainer

We add the functions to save and load model parameters to the GPT trainer:

  • save (the save function): number all the parameters of the model in order and save them to an .npz file;

  • load (the load function): load the data from the .npz file and assign it in order to the parameters of the model;

  • train (the training function):

    • Shuffle: before each epoch, we added data shuffling. It randomizes the order of the samples and adds randomness to the training;

    • Log: track the changes of the loss and the learning rate during training;

    • Save the best model parameters: after each epoch, evaluate the training result on the test set. If the result is better, replace the saved parameters.

Hyperparameters

In this chapter, we scale up the hyperparameters of the model and try to train a relatively more effective model. The final model has about 10.8M parameters.

💡 Tip: training takes about 1-2 hours on a Mac mini M4 with 16 GB of memory.

Learning rate

Batch size

Number of epochs

Context size

Embedding size

Number of heads

Number of Transformer blocks

Modeling

Training

During training we pass in MODEL_FILE, and the best model parameters are saved automatically after each epoch.

epoch 1 done, test loss 2.3235
epoch 1 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 2 done, test loss 2.0444
epoch 2 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 3 done, test loss 1.8580
epoch 3 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 4 done, test loss 1.7599
epoch 4 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 5 done, test loss 1.6474
epoch 5 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 6 done, test loss 1.5968
epoch 6 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 7 done, test loss 1.5635
epoch 7 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 8 done, test loss 1.5383
epoch 8 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 9 done, test loss 1.5339
epoch 9 saved model to ../../../../data/tinyshakespeare-gpt.npz
epoch 10 done, test loss 1.5331
epoch 10 saved model to ../../../../data/tinyshakespeare-gpt.npz
GPT-training completed

Evaluation

prediction:	54 steps, each (16, 256, 65)
loss:	1.5330633212018896

Inference

After training is completed, the best model parameters have been saved to the MODEL_FILE file. Next time we use the model, we can load it directly without retraining.

ROMEO:
As have done!

LEONTES:
Ho! when be letimest gentleman learness?

PAULINA:
Preventage straight the understand; such nor
Your victory fallse to dare.

DUKE VINCENTIO:
There, by the crown.

GLOUCESTER:
Indeed: thou art uncle and Verona wilt
Of thy sun-she s

In this chapter, we added the functions to save and load model parameters to the GPT trainer: the parameters of the best model are saved automatically during training, and can be loaded and used directly next time.

With this, the pre-training part of the GPT model is complete. In the next stage, we will enter the inference stage: how to use the pre-trained model to generate text.

Exercises

  1. Try changing the number of epochs EPOCHS to 20 and train for longer. Observe how the size of the saved model parameter file and the quality of the generated text change.