UserWarning No training configuration found in save file the model was not compiled. Compile it manually
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with machine learning models, especially in Keras or TensorFlow, you may encounter the warning message:
This article aims to explain the origins of this warning, its implications, and how you can address it. We will dive into the technicalities and provide strategies to handle such warnings effectively.
Understanding the Warning
Origins
This warning typically appears when you attempt to load a saved model in Keras using functions from the keras.models module, such as load_model(), but the saved model lacks the training configuration. This usually occurs because only the model architecture and weights were saved, but not the optimizer configuration that is integral to compiling the model.
Implications
- Model Usability: Your model can still be used for inference because it includes the architecture and weights. However, since it lacks the compilation details, it cannot be directly trained further.
- Missing Components: The absence of the training configuration means that optimizer settings, loss functions, and any configured metrics are not present.
Reasons for Missing Configuration
- Model Saving Mode: The model was saved using an option that does not preserve the training configuration. For instance, saving the model architecture and weights separately using the
model.to_json()ormodel.save_weights()functions. - Custom Objects: If your model uses custom layers, losses, or metrics, and you saved only the architecture and weights without specifying a means to save the custom components.
- Save Format: Using a save format that defaults to excluding training configuration (e.g., some versions of Keras's HDF5 format).
How to Address the Warning
Manual Compilation
To resolve the warning, you must manually compile the model after loading. This can be done using the compile() method. Here’s a generic example:
Use Cases for Manual Compilation
In practice, manually compiling a model can be advantageous when:
- You want to switch the optimizer or adjust its settings (e.g., learning rate).
- You need to modify the loss function or add/remove metrics.
- The training framework settings have changed since the model was created.
Saving Both Architecture and Training Configuration
- Save Entire Model: Save the entire model using
model.save(). This method includes the architecture, weights, and training configuration.
- JSON and Weights Combination: While this separates architecture and weights, ensure that training configuration is also documented separately.
Common Mistakes and Troubleshooting
- Custom Objects: When using custom components, supply the
custom_objectsparameter upon loading:
- Optimizer State: While loading a model without a compiled state, remember that the optimizer state, like momentum terms, is lost, which might impact training continuity.
- Library Versions: Ensure consistency in TensorFlow/Keras library versions when saving and loading models. Significant version changes might lead to incompatibility.
Summary Table
| Aspect | Explanation and Notes |
| Usability | Inference is possible without compilation; training is not. |
| Key Missing Elements | Optimizer settings, loss functions, and metrics. |
| Main Solution | Manually compile after loading. |
| Advantages of Manual Compile | Allows modifications of loss, optimizer, etc., before retraining. |
| Saving Best Practices | Use model.save() for full model persistence including training config. |
| Version Matching | Ensure library version consistency between save/load operations. |
Conclusion
The UserWarning about missing training configuration is not necessarily an error but a reminder of what aspects of a model are absent when it was saved. Understanding how Keras and TensorFlow handle model persistence will equip you to avoid potential pitfalls and know how to remediate them. With this knowledge, you can ensure seamless deployment and further training of your machine learning models.

