Recently, I was working on a deep learning project for predicting housing prices in California when I encountered an error that stopped me in my tracks: AttributeError: Module ‘keras.optimizers’ has no attribute ‘sgd’. This error can be frustrating, especially when you’re following tutorials that worked perfectly just a few months ago.
The issue usually arises due to changes that may now trigger this error.
In this guide, I’ll show you multiple ways to fix this common error and get your deep learning models running smoothly again. Let’s get in!
Understand the Error
First, let’s understand why this error occurs:
- In older TensorFlow/Keras versions, you could access optimizers like
keras.optimizers.SGD() - In newer versions (TensorFlow 2.8+), the API has changed, and the old method no longer works
Here’s an example of code that would trigger this error:
import tensorflow as tf
from keras import optimizers
# This will cause the error
optimizer = optimizers.sgd(learning_rate=0.01)Method 1: Use the Class-Based Approach
The simplest fix is to use the class-based approach instead of the function-based approach. The optimizer is now a class that should be capitalized.
import tensorflow as tf
from tensorflow.keras.optimizers import SGD
# A trainable variable
x = tf.Variable([2.0], dtype=tf.float32)
# Define the loss function
def loss_fn():
return x ** 2
# Define the optimizer
optimizer = SGD(learning_rate=0.1)
# Minimize the loss for a few steps
for step in range(3):
with tf.GradientTape() as tape:
loss = loss_fn()
grads = tape.gradient(loss, [x])
optimizer.apply_gradients(zip(grads, [x]))
print(f"Step {step+1}, x = {x.numpy()}, Loss = {loss.numpy()}")Output:
Step 1, x = [1.6], Loss = [4.]
Step 2, x = [1.28], Loss = [2.5600002]
Step 3, x = [1.0239999], Loss = [1.6384]You can refer to the screenshot below to see the output.

This approach works because even though the lowercase function-style access has been removed, the class-based approach is still supported.
Read AttributeError: Module ‘tensorflow’ has no attribute ‘variable_scope’
Method 2: Use tensorflow.keras Instead of keras
Another reliable fix is to use tensorflow.keras instead of the standalone keras package:
import tensorflow as tf
# Use tensorflow.keras instead of standalone keras
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
# Define a simple variable
x = tf.Variable([5.0])
# Define a simple loss function
def loss_fn():
return x ** 2 # Just a simple square function for testing
# Use GradientTape to compute and apply gradients
with tf.GradientTape() as tape:
loss = loss_fn()
grads = tape.gradient(loss, [x])
optimizer.apply_gradients(zip(grads, [x]))
print(f"Updated x: {x.numpy()}, Loss: {loss.numpy()} — using tensorflow.keras optimizer")You can refer to the screenshot below to see the output.

This approach is recommended by the TensorFlow team as it ensures better compatibility with the TensorFlow ecosystem.
Check out Module ‘tensorflow’ has no attribute ‘get_variable’
Method 3: Specify the Correct Keras Version
If you’re working on a project that needs to use the older API style, you can install a specific version of Keras that maintains the old behavior:
pip uninstall keras tensorflow
pip install tensorflow==2.7.0This will give you a version where the old API style still works, but be aware that you might miss out on newer features and improvements.
Method 4: Use the legacy Optimizers Module (TensorFlow 2.11+)
In TensorFlow 2.11 and later, there’s a legacy optimizers module that you can use:
import tensorflow as tf
from tensorflow.keras.optimizers import legacy
# Use the legacy module
optimizer = legacy.SGD(learning_rate=0.01)This is useful when you’re upgrading older code but don’t want to change all optimizer references.
Method 5: Update Your Import Structure
You can also directly import the specific optimizer you need:
import tensorflow as tf
from tensorflow.keras.optimizers import SGD
# Directly import the optimizer class
optimizer = SGD(learning_rate=0.01)This approach makes your code more explicit and can help avoid similar errors with other optimizer types.
Read Module ‘tensorflow’ has no attribute ‘truncated_normal’
Real-World Example: Stock Price Prediction Model
Let me show you a practical example of fixing this error in a stock price prediction model for the S&P 500:
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.optimizers import SGD # Correct import
# Load and prepare S&P 500 data
# ... (data preparation code)
# Create LSTM model
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))
# Compile model with the correct optimizer
optimizer = SGD(learning_rate=0.01, momentum=0.9)
model.compile(optimizer=optimizer, loss='mean_squared_error')
# Train model
model.fit(x_train, y_train, batch_size=32, epochs=20)This code correctly uses the class-based approach with the proper import structure, avoiding the AttributeError.
Check out AttributeError: Module ‘tensorflow’ has no attribute ‘trainable_variables’
Common Mistakes to Avoid
When fixing this error, watch out for these common mistakes:
- Mixing keras versions: Having both standalone keras and tensorflow.keras installed can cause conflicts
- Incorrect capitalization: Remember that optimizer classes are capitalized (SGD, Adam, RMSprop)
- Outdated tutorials: Many tutorials online still use the old API style
- Inconsistent imports: Mixing import styles across your project can lead to confusion
Check Your TensorFlow and Keras Versions
If you’re unsure which version you’re using, you can check with:
import tensorflow as tf
import keras
print(f"TensorFlow version: {tf.__version__}")
print(f"Keras version: {keras.__version__}")This will help you determine which approach is best for your specific versions.
The Keras API changed to make the codebase more maintainable and consistent. The class-based approach (using SGD() instead of sgd()) follows Python conventions better and provides clearer error messages and type hints.
While this change improves the code quality, it has created backward compatibility issues that many developers are still encountering.
I hope this guide helps you resolve the error. Now you have several approaches to fix this issue and can select the one that best suits your project requirements.
Other Python articles you may also like:
- Module ‘tensorflow’ has no attribute ‘log’
- AttributeError: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’
- AttributeError: Module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.