How to continue training model using ModelCheckpoint of Keras
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
To continue training from a Keras checkpoint, you need to know what was saved. If the checkpoint stores only weights, rebuild the model and load the weights; if it stores the full model, load the model object and continue calling fit.
Save Weights Only and Resume Later
This is a common pattern when you want control over model construction in code.
Later, to continue training:
The important requirement is that build_model() must recreate the same architecture. If the layer shapes or names do not match, weight loading will fail.
Save the Full Model If You Want More State Back
If you save the full model instead of weights only, Keras can restore the architecture and optimizer state too.
Resume like this:
This is often the simplest way to continue training because you do not need to rebuild the model manually.
Understand initial_epoch
initial_epoch does not load anything by itself. It only tells fit which epoch number to treat as the starting point for logs and callbacks.
If you previously trained through epoch 3, then a continuation call such as this is appropriate:
That means Keras continues from epoch index 3 up to 9. Without initial_epoch, the training still runs, but the epoch numbering and some callback behaviors may be misleading.
Best Checkpoint Versus Last Checkpoint
Be careful with save_best_only=True. That setting saves the best checkpoint according to the monitored metric, not necessarily the most recent training state.
If you resume from this file, you are continuing from the best saved model so far, not automatically from the interrupted last epoch. That is fine when you want the best weights, but it is different from exact crash recovery.
For exact training recovery in long jobs, many teams also use callbacks designed for fault tolerance, not just model selection.
A Practical Rule
Choose one of these patterns:
- weights only if you prefer explicit model construction in code
- full model if you want the easiest continuation path
In either case, keep the architecture, preprocessing, and label encoding consistent between the original run and the resumed run. A checkpoint cannot fix a changed input pipeline.
Common Pitfalls
- Calling
load_weightson a model whose architecture no longer matches the saved weights. - Expecting
initial_epochto restore training state. It only affects fit bookkeeping. - Using
save_best_only=Trueand assuming the file is the latest epoch rather than the best monitored checkpoint. - Forgetting to compile the rebuilt model before further training when using weights-only restoration.
- Changing preprocessing or class order between runs and blaming the checkpoint when resumed metrics look wrong.
Summary
- To continue training, first identify whether the checkpoint saved weights only or the full model.
- With weights-only checkpoints, rebuild the same model and call
load_weights. - With full-model checkpoints, use
keras.models.load_modeland continuefit. - '
initial_epochis for correct epoch numbering, not for loading the checkpoint itself.' - '
save_best_only=Trueresumes from the best saved checkpoint, not necessarily the most recent one.'
Related reading
- How to control GPU memory size with tf.estimator
- How to control memory while using Keras with tensorflow backend?
- How to convert a list of tensors of dim N to a tensor of dim N1
- How to convert a PyTorch nn.Module into a HuggingFace PreTrainedModel object?
- how to control frequency of loss logging messages when using tf.Estimator?
- How to control tensorflow's VLOG?
- How to control when to compute evaluation vs training using the Estimator API of tensorflow?
- How to convert a Python data generator to a Tensorflow tensor?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.