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>
3
  • Try without any parameters to sgd like this sgd = optimizers.SGD() Commented Oct 7, 2019 at 15:10
  • Thanks for the answer. I try this : sgd = optimizers.SGD() ###### Compile model ############ model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS) """ But I have same error! Commented Oct 7, 2019 at 15:17
  • what is the version of tf you are using ? Commented Oct 7, 2019 at 15:31

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.