Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading keras model issues warning: skipping variable loading for optimizer 'Adam'

Tags:

keras

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?

like image 865
user145453 Avatar asked May 03 '26 06:05

user145453


1 Answers

When loading a Keras model, this warning indicates a mismatch between saved and current optimizer states.

  1. For inference only: Ignore the warning - model weights load correctly and predictions work fine.

  2. 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.

like image 104
EuanG Avatar answered May 04 '26 19:05

EuanG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!