73

I am trying to import Keras but I get the following error:

ImportError: cannot import name 'adam' from 'keras.optimizers' (/usr/local/lib/python3.8/dist-packages/keras/optimizers/__init__.py)

The import is invoked here:

from tensorflow import keras
from keras.layers import Conv2D, Input, MaxPool2D,Flatten, Dense, Permute, GlobalAveragePooling2D
from keras.models import Model
from keras.optimizers import adam
import numpy as np
import pickle
import keras
import cv2
import sys
import dlib
import os.path
from keras.models import Sequential
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import Dense
from keras.optimizers import Adam
import pickle
import numpy as np
import cv2
import os
from keras.layers import Dropout

I am sure Keras is installed along with Tensorflow:

python3 -c 'import keras; print(keras.__version__)' // 2.4.3
1
  • 3
    Its Adam, capitalized, not adam
    – Dr. Snoopy
    Commented Jul 3, 2020 at 8:25

11 Answers 11

135

There are two types of modules -

  1. keras
  2. tensorflow.keras

Here we need to use tensorflow.keras

You need to import Adam (With Capital A) from tensorflow - Keras ( Not only Keras).

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.optimizers import Adam # - Works
from tensorflow.keras.optimizers import adam # - Does not work
from keras.optimizers import Adam            # - Does not work
from keras.optimizers import adam            # - Does not work
0
31

recently, in the latest update of Keras API 2.5.0 , importing Adam optimizer shows the following error:

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

instead use the following for importing optimizers (i.e. Adam) :

from keras.optimizers import adam_v2
optimizer = adam_v2.Adam(learning_rate=lr, decay=lr/epochs)
Model.compile(loss='--',  optimizer=optimizer  , metrics=['--'])

for more details, please have a look at : https://programmerah.com/keras-nightly-import-package-error-cannot-import-name-adam-from-keras-optimizers-29815/

hopefully, this helps.

1
  • Glad of this answer has helped many people here. Commented Mar 6, 2022 at 12:42
24

from tensorflow.keras.optimizers import Adam

Try this in the 4th line(or just replace it with the above line). It worked for me.

20

You can do two things:

  1. Go with tensorflow

    from tensorflow.keras.optimizers import Adam

This might cause you to change other keras.* modules to tensorflow.kerar.* modules.

  1. Stay at keras

    from keras.optimizers_v1 import Adam

Adam has been shifted to optimizer_v1 module.

1
  • When I use optimizer from from keras.optimizers_v1 it explicitly tells me to use tf.keras.optimzers. Full error trace is as follows ValueError: tf.compat.v1.keras` Optimizer (<keras.optimizer_v1.Adam object at 0x0000022A4D403048>) is not supported when eager execution is enabled. Use a tf.keras Optimizer instead, or disable eager execution.`
    – haneulkim
    Commented Sep 19, 2023 at 14:57
9

The error came from an older version of keras, in the newer version, you don't have to import Adam, it can be specified using the quote, i.e.:

model.compile(optimizer= "adam", loss='mse', metrics=[psnr, "accuracy"])
2
  • 2
    If you would like to have more variety, such as: model.compile(optimizer = keras.optimizers.Adam(lr=0.001, decay=0.9), loss='mse', metrics=[get_rmse]) Commented Jun 8, 2021 at 2:13
  • Throws error ValueError: Could not interpret optimizer identifier: <tensorflow.python.keras.optimizer_v2.adam.Adam object at 0x000001F91A8BA820>
    – Adi
    Commented Jun 23, 2021 at 19:08
7

In the fourth line, just make from keras.optimizers import Adam. It should work perfectly fine!

1
  • 10
    It doesn't work. cannot import name 'Adam' from 'keras.optimizers'
    – Adi
    Commented Jun 23, 2021 at 19:03
6
from keras.optimizer_v2 import adam

Then call Adam from adam

adam.Adam(...);
1
  • 1
    thank you so much, I spent a day to solve this damn problem!!!
    – Jing
    Commented Jan 18, 2023 at 16:16
5

The following worked for me:

# importing
from keras.optimizers import adam_v2
# to use it
adam_v2.Adam(...)

I am on a virtual environment running tensorflow-2.6.0 (CPU version, pip installed) with Python3.8.7.

0
3

I had a similar problem after a whole day lost on this. I found that just:

from tensorflow.python.keras.optimizers import adam_v2

adam_v2.Adam(learning_rate=0.0001, clipnorm=1.0, clipvalue=0.5)

works for me (i had v2.11.0 of tensorflow).

I also find these other optimizers in tensorflow.python.keras.optimizers:

'adadelta_v2', 'adagrad_v2', 'adam_v2', 'adamax_v2','nadam_v2','rmsprop_v2',

that you can use as

from tensorflow.python.keras.optimizers import rmsprop_v2

rmsprop_v2.RMSprop(learning_rate=0.0001)

1

I am using
Name: keras Version: 2.8.0

from keras.optimizers import adam_v2

Then to use the adam optimizer (use tf.keras.optimizers.Adam):

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
0

Just use this line for import Adam

from tensorflow.keras.optimizers import Adam

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.