Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Unexpected keyword argument passed to optimizer: learning_rate

I am trying to load a Keras model which was trained on an Azure VM (NC promo). But I am getting the following error.

TypeError: Unexpected keyword argument passed to optimizer:learning_rate

EDIT:

Here is the code snippet that I am using to load my model:

from keras.models import load_model
model = load_model('my_model_name.h5')
like image 970
Chayan Bansal Avatar asked Sep 20 '19 13:09

Chayan Bansal


5 Answers

This happened to me too. Most likely because the learning_rate was renamed from version 2.2.* to 2.3.0 in September 2018. (see release notes: https://github.com/keras-team/keras/releases : Rename lr to learning_rate for all optimizers. )

This worked for me:

sudo pip install keras --upgrade 
like image 88
MichaelF Avatar answered Nov 13 '22 19:11

MichaelF


Did you use a custom optimizer?

If so, you can load like this:

model = load_model('my_model_name.h5', custom_objects={
    'Adam': lambda **kwargs: hvd.DistributedOptimizer(keras.optimizers.Adam(**kwargs))
})

Alternatively you can load your model with model = load_model('my_model_name.h5', compile=False) and then add an optimizer and recompile, but that will lose your saved weights.

like image 15
Rob Bricheno Avatar answered Nov 13 '22 20:11

Rob Bricheno


In my case I found the best solution is to use h5py to change name of the variable from "learning_rate" -> "lr" as suggested in the previous posts.

import h5py
data_p = f.attrs['training_config']
data_p = data_p.decode().replace("learning_rate","lr").encode()
f.attrs['training_config'] = data_p
f.close()
like image 13
felipk101 Avatar answered Nov 13 '22 18:11

felipk101


That issue usual on dependencies difference between the kernel where that model has been trained and the dependencies versions where the model is being loaded.

If you have installed the latest version of Tensorflow now (2.1) try to load the model like this:

import tensorflow as tf
print(tf.__version__)
print("Num GPUs Available: ", 
       len(tf.config.experimental.list_physical_devices('GPU')))
# Checking the version for incompatibilities and GPU list devices 
# for a fast check on GPU drivers installation. 

model_filepath = './your_model_path.h5'

model = tf.keras.models.load_model(
    model_filepath,
    custom_objects=None,
    compile=False
)

Compile=False only if the model has already compiled.

like image 8
Victor Cid Avatar answered Nov 13 '22 20:11

Victor Cid


i got the same error while i was working in two different PC. in some versions of tensorflow is tf.keras.optimizers.SGD(lr = x) while in other verions istf.keras.optimizers.SGD(learning rate = x).

like image 7
Walter Jacquet Avatar answered Nov 13 '22 18:11

Walter Jacquet