Tag Archives: Answering questions

Keras-nightly Import package Error: cannot import name ‘Adam‘ from ‘keras.optimizers‘

Version keras nightly = 2.5.0.dev2021032900

Error information

    from keras.optimizers import Adam
ImportError: cannot import name 'Adam' from 'keras.optimizers' 

Solution

error code

from keras.optimizers import Adam
opt = Adam(lr=lr, decay=lr/epochs)

modify

from keras.optimizers import adam_v2
opt = adam_v2.Adam(learning_rate=lr, decay=lr/epochs)

reason

After the keras library is updated, the package cannot be imported in the original way. Open the optimizers.py source code and find the following two key codes. You can see that Adam import has changed, so it is modified as above.

from keras.optimizer_v2 import adam as adam_v2
'adam': adam_v2.Adam,