Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not interpret optimizer identifier" error in Keras

I got this error when I tried to modify the learning rate parameter of SGD optimizer in Keras. Did I miss something in my codes or my Keras was not installed properly?

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 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:', )

like image 813
Nehemia Avatar asked Apr 27 '18 06:04

Nehemia


Video Answer


2 Answers

The reason is you are using tensorflow.python.keras API for model and layers and keras.optimizers for SGD. They are two different Keras versions of TensorFlow and pure Keras. They could not work together. You have to change everything to one version. Then it should work.

like image 72
Priyanka Chaudhary Avatar answered Sep 25 '22 22:09

Priyanka Chaudhary


I am bit late here, Your issue is you have mixed Tensorflow keras and keras API in your code. The optimizer and the model should come from same layer definition. Use Keras API for everything as below:

from keras.models import Sequential from keras.layers import Dense, Dropout, LSTM, BatchNormalization from keras.callbacks import TensorBoard from keras.callbacks import ModelCheckpoint from keras.optimizers import adam  # Set Model model = Sequential() model.add(LSTM(128, input_shape=(train_x.shape[1:]), return_sequences=True)) model.add(Dropout(0.2)) model.add(BatchNormalization())  # Set Optimizer opt = adam(lr=0.001, decay=1e-6)  # Compile model model.compile(     loss='sparse_categorical_crossentropy',     optimizer=opt,     metrics=['accuracy'] ) 

I have used adam in this example. Please use your relevant optimizer as per above code.

Hope this helps.

like image 38
VoidA313 Avatar answered Sep 25 '22 22:09

VoidA313