3

I try to run this code and I have this error, Please any one had the same error in the past:

sgd = optimizers.SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)

Compile model

model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
fit_history = model.fit_generator(
        train_generator,
        steps_per_epoch=STEPS_PER_EPOCH_TRAINING,
        epochs = NUM_EPOCHS,
        validation_data=validation_generator,
        validation_steps=STEPS_PER_EPOCH_VALIDATION,
        callbacks=[cb_checkpointer, cb_early_stopper]
)
model.load_weights("../working/best.hdf5")

Now I have this error:

File "", line 1, in runfile('C:/Users/ResNet50VF72.py', wdir='C:/Users/RESNET')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/RESNET/ResNet50VF72.py", line 110, in model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 96, in compile self.optimizer = optimizers.get(optimizer)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\optimizers.py", line 793, in get str(identifier))
ValueError: Could not interpret optimizer identifier : <tensorflow.python.keras.optimizers.SGD object at 0x0000013887021208>
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
SpEm1822
  • 51
  • 3
  • 8

2 Answers2

8

I had the same issue with another optimizer:

ValueError: Could not interpret optimizer identifier: <tensorflow.python.keras.optimizers.Adam object at 0x7f3fc4575ef0>

This was because I created my model using keras and not tensorflow.keras, the solution was switching from:

from keras.models import Sequential

to

from tensorflow.keras.models import Sequential

Or one could also use only keras and not tensorflow.keras (I was mixing old and new code), it seems it is the mixing of the two which causes issues (which shouldn't be a surprise).

Ixio
  • 517
  • 6
  • 21
0

You should import like this :

from keras.optimizers import gradient_descent_v2 

and set your hyperparameters like this :

opt = gradient_descent_v2.SGD(learning_rate=lr, decay=lr/epochs)

reference: https://programmerah.com/keras-nightly-import-package-error-cannot-import-name-adam-from-keras-optimizers-29815/

ALI Q SAEED
  • 501
  • 5
  • 6