How do I load a keras saved model with custom Optimizer
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
Loading a Keras model that was saved with a custom optimizer usually fails for one simple reason: Keras does not know how to reconstruct your optimizer class unless you provide it during deserialization. The right fix depends on whether you need to continue training with optimizer state or only need the model for inference.
Core Sections
When a custom optimizer matters
If you saved the full model with compile information, Keras tries to recreate:
- the model architecture
- the weights
- the loss
- the metrics
- the optimizer and its config
That is why custom optimizers cause load errors. Keras can rebuild built-in optimizers automatically, but custom classes must be supplied explicitly.
Loading with custom_objects
The standard solution is to pass the optimizer class in the custom_objects dictionary.
The key in custom_objects must match the serialized name that Keras stored. In many cases that is simply the class name.
Make the custom class serializable
If the optimizer has extra constructor arguments, implement get_config() so Keras can save and restore it correctly.
Without get_config(), loading may still fail or silently restore an incomplete optimizer configuration.
If you only need inference, skip compilation
A lot of people are trying to load the model only to call predict(). In that case, you usually do not need the optimizer at all. Tell Keras not to restore compile state:
This is the simplest and most robust option for inference services, evaluation scripts, or model conversion pipelines.
Continuing training with optimizer state
If you want to resume training exactly where it stopped, compile=False is not enough. You need:
- the custom optimizer class available
- a serializable optimizer config
- a compatible TensorFlow and Keras version
Then you can load and keep training:
If version mismatches prevent optimizer restoration, a practical fallback is to load the model weights and then recompile with a freshly created optimizer instance. That loses optimizer momentum state, but training can still continue.
Registering custom objects cleanly
For larger projects, a registration decorator or a shared custom-object registry can reduce repeated boilerplate. The important part is consistency between the saving and loading environments.
Registration makes serialization cleaner, but you still need the class importable when the model is loaded.
Common Pitfalls
- Forgetting to pass the custom optimizer class in
custom_objectswhen loading a compiled model. - Trying to resume training with
compile=False, which discards optimizer state restoration. - Omitting
get_config()on a custom optimizer that has extra constructor parameters. - Expecting saved models to load identically across incompatible TensorFlow or Keras versions.
- Using a custom optimizer for inference-only workflows where loading without compilation would have been simpler.
Summary
- Keras needs access to custom optimizer classes during model deserialization.
- Use
custom_objectsor registered serializable classes when loading a compiled model. - Implement
get_config()so custom optimizer settings can be restored properly. - Use
compile=Falsewhen you only need inference and do not care about optimizer state. - For resumed training, keep the class definition and runtime versions aligned with the environment that saved the model.
Related reading
- How do I load a local model with torch.hub.load?
- How do I load custom image based datasets into Pytorch for use with a CNN?
- How do I make the initial state of an LSTM trainable in Keras?
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I make a ragged batch in Tensorflow 2.0?
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I plot a Keras/Tensorflow subclassing API model?
- How do I print the model summary in PyTorch?
.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.