'how to solve AttributeError: module 'keras.optimizers' has no attribute 'Adam'
I make a lstm model and wanna do parameter tunings by using BayesianOptimization, but when I write the code like the below code, the error about AttributeError: module 'keras.optimizers' has no attribute 'Adam' is shown up. When I wrote last time, it worked well, but this time, it is errors. Can you tell me how to solve this problem?
def build(hp):
activation = hp.Choice('activation', ['relu', 'tanh', 'linear', 'selu', 'elu'])
# num_rnn_layers = hp.Choice('num_rnn_layers', min_value=1, max_value=20)
recurrent_dropout = hp.Float('recurrent_dropout', min_value=0.0, max_value=0.99)
num_units = hp.Int('num_units', min_value=0, max_value=512)
model = keras.models.Sequential()
model.add(LSTM(units=num_units, activation=activation, recurrent_dropout=recurrent_dropout, input_shape=(80, 50)))
model.add(keras.layers.Dense(1))
model.compile(loss='mse', metrics=['mse'], optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])))
model.compile(
optimizer= keras.optimizers.Adam(hp.Float(
'learning_rate',
min_value=1e-10,
max_value=1e-2,
sampling='LOG',
default=1e-6
),
),
loss=tf.losses.MeanSquaredError(),
metrics=[tf.metrics.MeanAbsoluteError()]
)
# model.compile(loss='mse')
return model
model.compile(loss='mse', metrics=['mse'], optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])))
AttributeError: module 'keras.optimizers' has no attribute 'Adam'
Invalid model 2/5
WARNING:tensorflow:Layer lstm will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/keras_tuner/engine/hypermodel.py", line 127, in build
model = self.hypermodel.build(hp)
File "<ipython-input-12-3f3e21babeb7>", line 9, in build
model.compile(loss='mse', metrics=['mse'], optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])))
AttributeError: module 'keras.optimizers' has no attribute 'Adam'
Invalid model 3/5
WARNING:tensorflow:Layer lstm will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/keras_tuner/engine/hypermodel.py", line 127, in build
model = self.hypermodel.build(hp)
File "<ipython-input-12-3f3e21babeb7>", line 9, in build
model.compile(loss='mse', metrics=['mse'], optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])))
AttributeError: module 'keras.optimizers' has no attribute 'Adam'
Invalid model 4/5
WARNING:tensorflow:Layer lstm will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/keras_tuner/engine/hypermodel.py", line 127, in build
model = self.hypermodel.build(hp)
File "<ipython-input-12-3f3e21babeb7>", line 9, in build
model.compile(loss='mse', metrics=['mse'], optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])))
AttributeError: module 'keras.optimizers' has no attribute 'Adam'
Solution 1:[1]
you should import keras from tensorflow like this,
import tensorflow as tf
from tensorflow import keras
Solution 2:[2]
I think so you need to import the module or use try except condition which will handle with your errors
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tevfik oguz |
| Solution 2 | Pavithran G |
