55

After a training procedure, I wanted to check the accuracy by loading the created model.h5 and executing an evaluation procedure. However, I am getting a following warning:

/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py:269: UserWarning: No training configuration found in save file: the model was not compiled. Compile it manually. warnings.warn('No training configuration found in save file:

enter image description here

This dist-packages/keras/engine/saving.py file

so the problem in loading created model -> this line of code

train_model = load_model('model.h5')

Problem indicates that the model was not compiled, however, I did it.

optimizer = Adam(lr=lr, clipnorm=0.001)
train_model.compile(loss=dummy_loss, optimizer=optimizer)

I can't understand what I am doing wrong . . . Please help me! SOS :-(

2
  • 6
    The warning doesn't prevent you from evaluating the model.
    – Dr. Snoopy
    Commented Nov 14, 2018 at 9:05
  • 5
    I have the same problem (used model.save(..., include_optimizer=True)) (tensorflow's keras implementation)
    – olejorgenb
    Commented Jan 14, 2019 at 11:04

3 Answers 3

87

Intro

I'd like to add to olejorgenb's answer - for a specific scenario, where you don't want to train the model, just use it (e.g. in production).

"Compile" means "prepare for training", which includes mainly setting up the optimizer. It could also have been saved before, and then you can continue the "same" training after loading the saved model.

The fix

But, what about the scenario - I want to just run the model? Well, use the compile=False argument to load_model like that:

trained_model = load_model('model.h5', compile=False)

You won't be able to .fit() this model without using trained_model.compile(...) first, but most importantly - the warning will go away.

Misc Notes

Btw, in my Keras version, the argument include_optimizer has a default of True. This should work also for trainig callbacks like Checkpoint. This means, when loading a model saved by Keras, you can usually count on the optimizer being included (except for the situation: see Hull Gasper's answer).

But, when you have a model which was not trained by Keras (e.g. when converting a model trained by Darknet), the model is saved un-compiled. This produces the warning, and you can get rid of it in the way described above.

2
  • 2
    compile=False is not working for me. I have lots of models that were saved during the training. I need to load them just to make some inferences and evaluate the different model versions, ant the warning is thrown with both versions. I'm running Keras==2.2.4, tensorflow-gpu==1.14.0and python 3.6.6 Commented Oct 9, 2019 at 16:25
  • 1
    Beautiful solution! Thanks! Worked swimmingly for me :)
    – yeamusic21
    Commented Oct 28, 2021 at 15:28
3

Do you get this warning when saving the model?

WARNING:tensorflow:TensorFlow optimizers do not make it possible to access 
optimizer attributes or optimizer state after instantiation. As a result, we 
cannot save the optimizer as part of the model save file.You will have to 
compile your model again after loading it. Prefer using a Keras optimizer 
instead (see keras.io/optimizers).

Seems tensorflow optimizers can't be preserved by keras :/

1
  • Do you know if this is still the case in 2019?
    – M.R.
    Commented Mar 25, 2019 at 2:57
3

As mentioned keras can't save Tensorflow optimizers. Use the keras one:

optimizer = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)

model.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(...)
model.save('...')

This way works for me without manual compiling after calling load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.