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 says get_config is missing while loading a model, the real issue is usually that something inside the saved model is not fully serializable. That can happen even when you think you used only built-in layers, because serialization also includes things like lambda functions, custom losses, custom metrics, and sometimes version-specific wrappers.
Why Keras Needs get_config
When Keras reloads a full saved model, it must reconstruct the architecture from configuration data. For objects that participate in that reconstruction, Keras expects a serializable config or a known built-in implementation.
This works smoothly for ordinary built-in layers:
If loading fails with a get_config-related error, there is usually some object in the model or compile state that Keras does not know how to rebuild.
Common Hidden Causes
Even without a visibly custom layer, these can trigger problems:
- '
Lambdalayers using anonymous Python functions' - custom losses or metrics passed at compile time
- subclassed models with incomplete serialization support
- mismatched TensorFlow or Keras versions between save and load
- legacy HDF5 saves containing objects that newer loaders handle differently
For example, this model looks simple, but the lambda function is a serialization hazard:
A Lambda layer can work during training but still cause trouble when reloading across environments.
Safer Replacement for Lambda
Replace ad hoc lambda logic with a proper serializable layer:
Now Keras has a clear serializable object instead of an anonymous function.
Loading Without Recompiling
Sometimes the model architecture and weights are fine, but the compile-time objects are the problem. In those cases, loading with compile=False can bypass the failing compile state:
This is especially useful if you only need inference.
If you do need training later, you can recompile after loading:
Prefer Modern Save Formats
For current Keras workflows, prefer the native .keras format over older ad hoc patterns when possible:
The legacy HDF5 format still appears in older codebases, but serialization edge cases are easier to hit there, especially across library versions.
Common Pitfalls
The most common mistake is saying "there are no custom layers" while forgetting about lambda-based layers, custom metrics, or custom losses. Keras serialization cares about all serializable objects, not only obvious layer subclasses.
Another issue is loading the model in a different TensorFlow or Keras version than the one that saved it. Even built-in objects can behave differently across serialization formats and versions.
A third pitfall is assuming load_model must always restore compile state. If you only need inference, compile=False is often the simplest fix for a model that otherwise loads correctly.
Finally, if you truly have a custom serializable object, implement get_config properly and register or pass that object during loading instead of relying on implicit behavior.
Summary
- A missing
get_configerror usually means some saved object is not fully serializable. - The cause may be a lambda layer, custom metric, loss, or version mismatch, even without obvious custom layers.
- Prefer explicit serializable layer classes over anonymous
Lambdalogic. - Use
compile=Falsewhen you only need inference and compile state is the problem. - Prefer modern model save formats and consistent library versions when saving and loading.
Related reading
- 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 learning rate of keras model
- 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.