Keras 3 .keras save format: preserving optimizer state during zero‑downtime migration
0 reputation · 06 Oct 2020, 02:00 UTC
0 reputation · 06 Oct 2020, 02:00 UTC
Goal: migrate a small inference service from Keras 2 HDF5 checkpoints to the Keras 3 .keras zip format without taking the service offline.
Constraints: the migration must keep the optimizer state and training history intact so that fine‑tuning can resume immediately after the switch, and the new format must remain loadable under the same backend (TensorFlow) used originally.
Uncertainty: it is unclear whether the .keras format automatically serializes optimizer variables and whether any additional registration of custom objects is required when reloading the model in the same process version.
Questions:
model.save('model.keras') in Keras 3 retain the optimizer’s variables and training history?keras.saving.register_keras_serializable or supplied to load_model when loading the .keras file in Keras 3?35275 reputation · 06 Oct 2020, 07:57 UTC
Yes – model.save('model.keras') in Keras 3 stores the full optimizer state (config and variable values) but it does not embed the training history (epoch‑level loss/metrics). Custom layers, metrics, or losses that were defined in Keras 2 must be re‑registered with keras.saving.register_keras_serializable or supplied via the custom_objects argument when loading the .keras file in the same TensorFlow/Keras version.
optimizer/variables and optimizer/config.history object is not part of the file; it must be saved separately (e.g., via pickle or a JSON export).ValueError.# In a temporary script or interactive shell
import tensorflow as tf
from tensorflow import keras
model = keras.models.load_model('model.h5') # Keras 2 checkpoint
model.save('model.keras') # Keras 3 format
# In the same Python process
from tensorflow import keras
orig = keras.models.load_model('model.h5')
new = keras.models.load_model('model.keras')
orig_opt = orig.optimizer.get_weights()
new_opt = new.optimizer.get_weights()
# Simple element‑wise check
import numpy as np
print(np.allclose(orig_opt, new_opt, atol=1e-6))
from tensorflow import keras
@keras.saving.register_keras_serializable(package='Custom', name='MyLayer')
class MyLayer(keras.layers.Layer):
...
Alternatively, pass them as custom_objects to load_model.
# Save history separately before migration
import json
with open('history.json', 'w') as f:
json.dump(history.history, f)
np.allclose).
model.evaluate(test_data) on both models and confirm that the loss and metrics match the last checkpoint values within a small tolerance.
Did the original training use a custom training loop (e.g., tf.GradientTape) instead of model.fit? If so, the optimizer state may not be fully captured by .keras, and you might need to manually export and import the optimizer weights.
Converting a Keras 2 HDF5 checkpoint to the Keras 3 .keras ZIP format preserves the optimizer variables, enabling immediate resumption of fine‑tuning. The training history must be saved separately. Custom objects require re‑registration for successful deserialization.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.