'module 'tensorflow.python.keras.optimizers' has no attribute 'SGD'

I am working with python 3.9, tensorflow 2.7.0 with a modified version of Mask RCNN https://github.com/leekunhee/Mask_RCNN/blob/tensorflow2.0/mrcnn). I am working with tensorflow .keras and specifically import

import tensorflow as tf
from tensorflow.python import keras 
from tensorflow.python.keras import backend as K
from tensorflow.python.keras import layers as KL
from tensorflow.python.keras import utils as KU
from tensorflow.python.eager import context
from tensorflow.python.keras import initializers as KI#nuevo
from tensorflow.python.keras import engine as KE
from tensorflow.python.keras import models as KM
from tensorflow.keras.optimizers import SGD

I call this module in this code

`optimizer = SGD(lr=learning_rate, momentum=momentum, clipnorm=self.config.GRADIENT_CLIP_NORM)`

and get this error

~/work/model.py in compile(self, learning_rate, momentum)
   2234         """
   2235 
-> 2236         # Optimizer object
   2237         optimizer = SGD(
   2238             lr=learning_rate, momentum=momentum,
AttributeError: module 'tensorflow.python.keras.optimizers' has no attribute 'SGD' 

I have tried calling tensforflow.keras.optimizers.SGD or tensorflow.python.keras.optimizers.SGD instead but nothing seems to work.

I am working on a jupyter notebook in IBM Watson Studio

Thanks!



Solution 1:[1]

I ran into this issue and came across your post. I'm running python v3.9.7 on a MacBook Pro with an M1 processor. (I'm not quite sure how to check my tensor flow version, as I'm still learning).

To better understand what was happening, I ran specifically:

from tensorflow.python.keras.optimizers import SGD

did not work.

However, it did work when I ran:

from tensorflow.keras.optimizers import SGD

from tensorflow.keras.optimizers import SGD

It seems as if the tensorflow.python.keras.optimizers has different optimisers than tensorflow.keras.optimizers. However, I can't find any documentation on this and am surprised as something as fundamental as stochastic gradient descent (SGD) would be missing?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 T PERRY