get_config missing while loading previously saved model without custom layers
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
If Keras complains about get_config while loading a model that you believe uses only built-in layers, the failure is usually caused by something around the model rather than the obvious layer list. Common causes include Lambda layers, custom metrics or losses saved with the model, version mismatches, subclassed models, or trying to deserialize training state that you do not actually need for inference.
Why get_config Matters
Keras reconstructs many objects by serializing their configuration and then recreating them later. Built-in layers already know how to do this. Custom or partially custom objects often do not, unless they implement the expected serialization hooks.
That is why a model can look "standard" at first glance and still fail at load time. The non-serializable part may be:
- a
Lambdalayer wrapping a Python function - a custom loss passed to
compile - a custom metric
- a subclassed
ModelorLayer - optimizer state saved from a different environment
A Model Can Be Non-Portable Without Obvious Custom Layers
This model uses only standard dense layers and is easy to save and load:
Now compare that to a model with a Lambda layer:
That Lambda function may be the real reason deserialization fails, even though the rest of the layer stack looks standard.
First Debugging Step: Load Without Compiling
If you only need the model for inference, try:
This skips deserializing training configuration such as losses and metrics. If the model now loads correctly, the problem was likely in the compile-time objects rather than the forward-pass architecture.
That is a very common fix when the saved model architecture is usable but the training metadata is not portable.
Replace Lambda with a Real Layer
If you used Lambda, replace it with a serializable layer or a built-in preprocessing layer when possible.
Example:
Built-in layers such as Rescaling are much easier to serialize reliably than arbitrary Python lambdas.
If You Truly Have a Custom Object
When a custom layer or model is real, implement get_config and optionally from_config.
Then load with custom_objects if needed:
Even if your current model is supposed to be standard, this example shows what Keras expects from anything outside its built-in registry.
Version Mismatch Is a Real Cause
A model saved in one Keras or TensorFlow environment can fail in another if serialization rules changed or a class path no longer matches. If a previously working file suddenly fails to load:
- check the save-time package versions
- check the load-time package versions
- try loading in the original environment
If the model loads there, the file is probably fine and the environment mismatch is the issue.
When Saving Weights Is Safer
If your architecture is defined in code and you do not need full object serialization, saving weights can be more robust:
This avoids some serialization complexity, but it requires the model code to be rebuilt exactly before loading.
Common Pitfalls
- Assuming "no custom layers" means "nothing custom at all." Losses, metrics, lambdas, and wrappers can still break serialization.
- Trying to load a model for inference with full compile state when
compile=Falsewould avoid the failing training metadata. - Using
Lambdafor behavior that could be expressed with a built-in serializable layer. - Ignoring TensorFlow or Keras version differences between the environment that saved the model and the one loading it.
- Reaching for
get_configfixes immediately when the real problem is that saving weights only would have matched the deployment need better.
Summary
- A
get_configloading error often comes from non-obvious custom objects, not just explicit custom layers. - '
compile=Falseis the first thing to try when you only need inference.' - Replace
Lambdalayers with serializable built-in layers whenever possible. - If you do own a custom layer or model, implement
get_configproperly and register it during loading. - When portability is more important than full serialization, saving weights plus rebuilding the architecture can be the safer path.
Related reading
- get_config missing while loading previously saved model without custom layers
- Get Gradients with Keras Tensorflow 2.0
- Get info of exposed models in Tensorflow Serving
- Get Keras model input from inside a custom callback
- Get labels from dataset when using tensorflow image_dataset_from_directory
- Get last output of dynamic_rnn in tensorflow?
- Get exception description and stack trace which caused an exception, all as a string
- Get name of currently executing test in JUnit 4
.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.