'Could not interpret optimizer identifier:' error when try to modify the optimizer

5,408 views
Skip to first unread message

sugianto...@gmail.com

unread,
Apr 25, 2018, 9:00:15 PM4/25/18
to Keras-users
Hi everyone,

My SGD optimizer works well if I use the default parameters but I had an error when I try to modify the parameters. Could you please help me to fix it? Is it my code or my Keras installation? 
Here is my code:

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation
import keras
from keras.optimizers import SGD

model = Sequential()
model.add(Dense(64, kernel_initializer='uniform', input_shape=(10,)))
model.add(Activation('softmax'))

model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01), metrics=['accuracy'])

and here is the error message:

Traceback (most recent call last):
  File "C:\TensorFlow\Keras\ResNet-50\test_sgd.py", line 10, in <module>
    model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01), metrics=['accuracy'])
  File "C:\Users\nsugiant\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\keras\_impl\keras\models.py", line 787, in compile
    **kwargs)
  File "C:\Users\nsugiant\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 632, in compile
    self.optimizer = optimizers.get(optimizer)
  File "C:\Users\nsugiant\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\keras\_impl\keras\optimizers.py", line 788, in get
    raise ValueError('Could not interpret optimizer identifier:', identifier)
ValueError: ('Could not interpret optimizer identifier:', <keras.optimizers.SGD object at 0x000002039B152FD0>)


roe...@gmail.com

unread,
Dec 3, 2018, 9:34:14 AM12/3/18
to Keras-users
Answering in case any future users have the same problem
The solution is explained here: https://stackoverflow.com/a/50066670
You should either direct all imports to tensorflow.keras.[...] or to keras.[...] , do not switch between them for different imports in the same code 

In this case, changing the import of SGD to:
from tensorflow.python.keras.optimizers import SGD

Should do the trick
Reply all
Reply to author
Forward
0 new messages