I save a keras model with
model.save("folder/file.keras")
Then I load this model with
model = load_model("folder/file.keras")
Everything loads and runs except I get a warning:
WARNING:absl:Skipping variable loading for optimizer 'Adam', because it has 111 variables whereas the saved optimizer has 1 variables.
Keras docs say that keras model.save saves optimizer state so I would expect to load it up just fine. What am I doing wrong?
When loading a Keras model, this warning indicates a mismatch between saved and current optimizer states.
For inference only: Ignore the warning - model weights load correctly and predictions work fine.
To fix for training:
# Option 1: Save weights only
model.save_weights("weights.h5")
model = create_model()
model.load_weights("weights.h5")
# Option 2: Ensure compiler compatibility
# When saving - explicitly include optimizer state
model.save("model.keras", include_optimizer=True)
# When loading - ensure compile flag is True to rebuild optimizer
model = load_model("model.keras", custom_objects=custom_objects, compile=True)
Common cause are different TensorFlow versions, different optimizer configurations, or changes in how optimizer states are stored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With