Loading Pretrained GPT-2 Weights into GPTModel
A saved model can be resumed: load the model and optimizer in a new Python session or notebook, then continue pretraining for one more epoch with train_model_simple.
Saving Is Not the Same as Resuming
Training a language model takes a long time, and nobody wants to repeat hours of work just because a session ended. A saved model turns training into something you can pause and resume. The key idea in this article is that a saved checkpoint is not a finished product by default. It is a snapshot of a training run that can be restored later and pushed further along.
What do you think happens?
Is loading the saved model alone enough to resume training?
Reveal answer
No, the optimizer must be loaded as well
The model carries the learned weight values, but the optimizer is the component that drives the update process during training. Restoring both lets training continue as a single run instead of a fresh start with a partly trained model.
What Has to Come Back
The source describes the resume workflow directly. After the weights are saved, you load the model and the optimizer again, possibly in a completely new Python session or in a Jupyter notebook file, and then continue pretraining for one more epoch using the train_model_simple function. Each step has a purpose, and the two loaded components play different roles.
Read the restoration from left to right. The saved weights have no effect until they are loaded back into a model, and training can only continue once the model and optimizer are both in place. The model supplies the learned values, and the optimizer supplies the machinery that keeps updating those values. If you restore only one of the two, you have not restored the training run; you have restored either a static set of values or a machine with nothing to work on.
Where the Weights Live
A GPTModel is assembled from smaller PyTorch layers. Some of those layers are Linear layers and some are Embedding layers. Each of these layers stores its learned values in a .weight attribute. The weight values therefore do not sit loose on the model object; they live inside the layers that the model contains.
You reach these parameters through model.parameters(). The source notes that this is the same method used earlier in the chapter to inspect the model during training. Because the weights are spread across nested layers, you do not need to know the layer structure to read them all: model.parameters() walks through the model and hands back the weight parameters it contains. That is why it is the natural tool for checking what a restored model holds.
Running One More Epoch
Once the model and optimizer are restored, the final stage is the train_model_simple call you used for the original training, now applied for one additional epoch. An epoch is one full pass over the training data. The source frames this as continuing a single run, so the restored model is not treated as a new model: it resumes from the weights it already holds.
Read the diagram as a single progression. The source describes the call only at the level of running one more epoch, so the diagram marks the stages at that same level. What matters for a learner is the starting state (restored model plus restored optimizer) and the ending state (the same model, with weights updated by the additional pass).
Why Pretrained Weights Save Effort
The earlier training in this chapter used a small GPT-2 style model on a limited dataset, a short story book. That kept the focus on fundamentals, but the model had seen very little text. The source points out that OpenAI openly shared the weights of its GPT-2 models. Using those shared weights means you do not have to train a model on a large corpus yourself, which would require a very large investment of money and computing resources.
The plan is to load these shared weights into the GPTModel class you built, and then use that model for text generation. Chapter 6 reuses the same pretrained weights to fine-tune the model for a text classification task and to follow instructions in a way similar to ChatGPT. Loading pretrained weights is therefore the shared starting point for both generation and later fine-tuning.
Worked Example: Resuming a Session
Resuming training in a new session
You saved a GPTModel and its optimizer at the end of a training run. The next day you open a new Python session in a Jupyter notebook. Describe what you restore and what you run, in the order the source describes.
- 1Start a new session
The source allows a completely new Python session or a Jupyter notebook file. Nothing from yesterday's session is still in memory, so every object you need must be rebuilt or loaded.
- 2Restore the model
Rebuild the GPTModel and load the saved weight values into it. Those values are the parameters stored in the .weight attributes of its Linear and Embedding layers.
- 3Restore the optimizer
Load the optimizer as well. The model holds the learned values, but the optimizer drives the update process, so training cannot continue as one run without it.
- 4Check the weights
Use
model.parameters()to inspect the weight parameters the restored model contains before training further. - 5Run one more epoch
Call
train_model_simple, the same function used for the original training, for one additional epoch.
Training resumes from the saved state instead of starting over, and the model has one more epoch of pretraining applied.
Mistakes When Resuming Training
Common mistake Assuming that saving the model is enough to resume training
What it looks likeLoading only the GPTModel in a new session and calling train_model_simple right away.
Why it failsThe model carries the learned weight values, but the optimizer drives the update process. Without it, training does not continue as a single run.
Do this insteadLoad both the model and the optimizer before continuing pretraining.
Common mistake Treating a saved checkpoint as a finished product
Why it failsA saved checkpoint is a snapshot of a training run that can be pushed further along, not a final result by default.
Do this insteadThink of the checkpoint as a point you can restore from and continue.
Common mistake Expecting the model to be trained from scratch in each session
Why it failsRestoring the saved weights means the new session starts from the trained state, so repeating the original training wastes the work already done.
Do this insteadRestore the saved state, then run one more epoch on top of it.
Common mistake Confusing the weights with the whole model file, when the weights are specifically the parameter values held in .weight attributes
Trace the Restored State
A colleague says: I saved my GPTModel yesterday, so today I only need to load the model and call train_model_simple for one more epoch. Using what this article describes, explain what is missing from that plan, and name the two components a complete resume must restore.
Hints
- Think about what the model holds and what the optimizer does.
- The source describes restoring both the model and the optimizer before continuing.
In a new session, after restoring a GPTModel, how would you confirm that the weight values are present before continuing training? Name the method and describe what it returns in general terms.
Hints
- The source names this method as the way to reach the model's weight parameters.
- Consider which parts of the model hold the .weight attributes.
Key Takeaways
- A saved checkpoint is a snapshot of a training run that can be restored and continued, not a finished product by default.
- To resume training in a new session or notebook, restore both the model and the optimizer, because the model holds the learned weights and the optimizer drives the updates.
- The weights are the parameter values in the .weight attributes of Linear and Embedding layers inside a GPTModel, and
model.parameters()exposes them. - Running
train_model_simplefor one more epoch continues the same training run from the restored state. - OpenAI's shared GPT-2 weights can be loaded into a GPTModel, avoiding the cost of training on a large corpus yourself, and the same weights are reused in chapter 6 for fine-tuning.