Using customized optimizer in Kerass-tf 2.5

339 views
Skip to first unread message

Siamak Sarmady

unread,
May 30, 2021, 3:18:56 AM5/30/21
to Keras-users
Hello

I use adam and sgd optimizer using below code without problem:

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

However when I try to create and pass an optimizer I receive error:

opt = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

I receive the following error:

ValueError: Could not interpret optimizer identifier: <tensorflow.python.keras.optimizer_v2.gradient_descent.SGD object at 0x000001AACFDAE460>

Could someone please help?
Thanks

pushpalatha murthy

unread,
May 30, 2021, 11:35:14 AM5/30/21
to Keras-users
Hi,
you are getting the erro, bcos of the libraries you import
so to correct the error [from kers impot optimizers] and use onle kers mode, pls u can follow the code which i have give in snaps(not full code)
from tensorflow import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten,Conv2D,MaxPooling2D
from __future__ import print_function
from keras import optimizers

opt = optimizers.SGD(learning_rate=0.01, momentum=0.9) 
print(opt)

model.compile(loss=keras.losses.categorical_crossentropy, optimizer=opt,metrics=['accuracy'])#keras.optimizers.Adadelta()

make changes according to your code.

regards,
Pushpalatha M
Reply all
Reply to author
Forward
0 new messages