Module 'keras.optimizers' has no attribute 'SGD'. Google Collab

Viewed 7579

I have notebook created on Google Collab. For work I need some functions of keras library.

I imported them

from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.callbacks import Callback
from keras import regularizers
from keras import optimizers

After running occurred this problem.

     54     model.compile(
---> 55      optimizer = optimizers.SGD(lr=lr),
     56      loss = loss_func,
     57      metrics = ["acc"]

AttributeError: module 'keras.optimizers' has no attribute 'SGD'

I tried this way

model.compile(
     optimizer = tf.keras.optimizers.SGD(lr=lr),
     loss = loss_func,
     metrics = ["accuracy"]
    )

I haven't issues with SGD but I have the same issue with Sequential.

What will be the right solution in this case? Why doesn't first way work?

2 Answers

Use tf.keras.optimizers.SGD instaed of optimizers.SGD

In my case, I had to use tensorflow.keras.optimizers instead of just keras.optimizers.

Related