''module' object is not callable sgd

Here is my code.

from keras.optimizers import gradient_descent_v2 as SGD

sgd=SGD(lr=0.01,momentum=0.9,decay=(0.01/25),nesterov=False)
 

I get the following error when I try to run it.

----> 1 sgd=SGD(lr=0.01,momentum=0.9,decay=(0.01/25),nesterov=False)
       

TypeError: 'module' object is not callable


Solution 1:[1]

Try with tensorflow tf.keras.optimizers.SGD(lr=0.01, momentum=0.9, decay=1e-6, nesterov=False).

Solution 2:[2]

You do not need to use the gradient_descent_v2 to import SGD optimizer.

Now, SGD(Stochastic Gradient Descent) optimizer inherits from tf.keras.optimzers, which you can import by giving it's exact alias name as below:

from tensorflow.keras.optimizers import SGD

sgd=SGD(learning_rate=0.01,momentum=0.9,decay=(0.01/25),nesterov=False)

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 RiveN
Solution 2 TFer2