Cannot Import Name ‘SGD’ from ‘keras.optimizers’

The error “Cannot Import Name ‘SGD’ from ‘keras.optimizers'” is an ImportError which may arise while working with Python machine learning projects. In our article, we will unwind the nuances of this matter, tracing their origin and offering thorough solutions toward smooth settlement.

What is the “Cannot Import Name ‘SGD’ from ‘keras.optimizers'” Error in Python?

The error “Cannot Import Name ‘SGD’ from ‘keras.optimizers'” illustrates the issue of failing to import the SGD optimizer from the keras optimizer module in the current programmed language, Python. This type of error occurs in machine learning projects in which SGD — a standard optimization algorithm is applied.

Why does this Cannot Import Name ‘SGD ‘from ‘keras. optimizer’ error occur?

This error could be due to several reasons. A typo or an incorrect import statement can be one of them. In developing an application, a developer might wrongly spell ‘SGD’ or incorrectly use an importation command from the ‘Keras.Optimizers’ API. 

This error is also caused by incompatibilities between various versions of Keras that come together with Tensorflow and, in many cases.

Let’s look at a code snippet that might trigger this: Cannot Import Name ‘SGD’ from ‘keras.optimizers’:

# Incorrect import statement
from keras.optimizers import sgd

# Rest of the code

This example shows that the developer wants to load the ‘SGD’ optimizer from the keras.optimizers module. Unfortunately, because of either an erroneous typo or invalid syntax, the interpreter yields the “cannot import name ‘SGD’ from ‘keras. optimizers'” error.

How do you resolve the Cannot Import Name ‘SGD’ from ‘keras.optimizers’ error?

Resolving this: Cannot Import Name ‘SGD’ from ‘keras.optimizers’ error involves a step-by-step process, addressing potential issues with the import statement and ensuring compatibility with your machine learning libraries.

Correct Import Statement:

Double-check your import statement to reflect the correct syntax and capitalization accurately. The correct import statement for the ‘SGD’ optimizer is:

from keras.optimizers import SGD

Ensure that ‘SGD’ is in uppercase, as it is the class name within the ‘keras.optimizers’ module.

Update Keras

Outdated versions of Keras might lack certain optimizers or have bugs that are addressed in newer releases. To upgrade Keras, use the following command:

pip install --upgrade keras

The above step ensures you have the latest version of Keras with all the necessary optimizers, including ‘SGD.’

Use TensorFlow Keras

Another way to resolve the Cannot Import Name ‘SGD’ from ‘keras.optimizers’ is to use TensorFlow Keras. If you are using TensorFlow as your backend for Keras, it’s advisable to import the optimizer from `tensorflow.keras.optimizers` instead of just `keras.optimizers`:

from tensorflow.keras.optimizers import SGD

TensorFlow often includes its version of Keras, and there might be slight differences in the import statements.

Restart Kernel or Interpreter

Also restarting the kernel or interpreter can help to reolve the Cannot Import Name ‘SGD’ from ‘keras.optimizers’ by clearing out any lingering issues if you are working in a Jupyter Notebook or interactive Python environment. The above step ensures that the changes, like upgrading libraries, take effect.

Consider Virtual Environments

Virtual environments can help isolate your project’s dependencies from the system-wide Python installation if you are working on a project with multiple dependencies. This step will help to prevent conflicts between different versions of libraries.


Would SGD work without importing it from keras.optimizers?

No! Instead, SGD needs to be imported from ‘keras.optimizers’. In Python, modules serve as receptacles for classes, functions and variables. ‘SGD’ optimizer belongs to the “keras.optimizers” module as a class. After importing it, one can instantiate and use it during their machine learning model. Trying to use ‘SGD’ without importing is from’ keras.optimizers’ yields the “ImportError: Cannot Import Name ‘SGD’ from ‘keras. optimizers'”.

What is keras. optimizers’ problem with this error?

This is because ‘SGD’ optimizer is expected to be in the ‘keras. optimizers’ module hence the error message is specific about ‘keras.optimizers’. When importing a class or function from a module, Python searches the specific module, to find that item. The error is raised when Python can’t find the ‘SGD’ optimizer in the ‘keras.optimizers’ module.

Cannot import the name ‘SGD’ from ‘keras.optimizers’ error require just an upgrade of Keras?

Yes, although updating the version of Keras is one way, it is not the only one. This could result from bad import statements not being correctly set for other libraries like TensorFlow. Hence, apart from upgrading Keras with pip install – upgrade keras, developers must review and correct the import statement to conform to its correct format. For instance, if you intend to use TensorFlow as a backend module of Keras, including import tensorflow.keras.optimizers might be required.


ImportErrors are very regular in machine and deep learning areas. One of the many challenges programmers encounter is the “Cannot Import Name ‘SGD’ from ‘keras.optimizers’” error. The error is manageable by observing import statements, updating libraries, and compatibility considerations by developers; thus, they can carry out their Machine Learning undertakings smoothly. 

However, debugging involves careful attention to detail, and becoming proficient at resolving import errors is something that developers hone over time.


  1. keras.optimizers
  2. SGD

Leave a Comment