This note covers Keras's model persistence โ a meaningfully different default philosophy from PyTorch's state_dict-focused approach in Saving PyTorch Models.
Saving the Complete Model
model.save('my_model.keras') # saves architecture, weights, AND optimizer state, all together
# Loading requires no prior model definition -- everything needed is in the file
from tensorflow.keras.models import load_model
loaded_model = load_model('my_model.keras')
This is the key difference from PyTorch's recommended pattern: Keras's default model.save() bundles the architecture definition itself alongside the weights and optimizer state, into one self-contained file โ unlike PyTorch's recommended state_dict approach, which requires the model class already be defined in code before loading (see Loading PyTorch Models).
Saving Only the Weights
model.save_weights('my_model.weights.h5')
# Requires the architecture to already be defined, exactly like PyTorch's state_dict pattern
new_model = build_the_same_architecture()
new_model.load_weights('my_model.weights.h5')
The Tradeoff, Explicitly
| Full model save (Keras default) | Weights-only save | |
|---|---|---|
| File contains | Architecture + weights + optimizer state | Just the weight values |
| Loading requires | Nothing extra โ fully self-contained | The matching model architecture already defined in code |
| Robustness to code changes | Can break if the custom layer/model class definitions change | More portable across code refactors, similar to PyTorch's recommended approach |
This mirrors the exact same tradeoff discussed for PyTorch's whole-model-object saving (via pickle) versus state_dict saving in Model Saving and Loading โ convenience and self-containment versus long-term portability and robustness to code changes.
Common Mistakes
- Relying on full-model saves for long-term storage across custom model classes that might change over time โ this can break loading if the custom class definitions evolve, similar to PyTorch's whole-object pickle-saving caveat.
- Forgetting that
load_weights()requires the exact same architecture already built โ mismatched architectures produce a shape/key error, just like PyTorch'sstate_dictloading.
Interview Relevance
Q: "What's the key difference between Keras's default model.save() and PyTorch's recommended state_dict saving approach?" Keras's model.save() bundles the full architecture definition, weights, and optimizer state into one self-contained file, requiring no prior model definition to load. PyTorch's recommended state_dict approach saves only the parameter values, requiring the exact model class to already be defined in code before loading โ trading Keras's greater convenience for PyTorch's greater long-term portability and robustness to code changes.
Practice Question
Why might weights-only saving be more robust across code changes than a full-model save, in both Keras and PyTorch?