🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

optimizers.SGD()

AI & DATA SCIENCE // optimizers-sgd

tf.keras.optimizers.SGD() updates weights by moving them in the direction opposite the gradient, scaled by a fixed learning rate, and optionally accelerated with momentum.

Syntax

tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.0)

Deep Dive Course

Stochastic Gradient Descent is the most basic optimization algorithm, applying the same fixed-size step, learning_rate multiplied by the gradient, to every parameter on every update, unlike adaptive optimizers such as Adam which scale each parameter's step individually. Setting a nonzero momentum accumulates a running average of past gradients, letting updates build up speed in a consistent direction and helping push through small local irregularities in the loss landscape — this closely mirrors the physical intuition of a ball rolling downhill and gathering momentum, rather than responding only to the immediate, current slope.

1Understanding optimizers.SGD()

Stochastic Gradient Descent is the most basic optimization algorithm, applying the same fixed-size step, learning_rate multiplied by the gradient, to every parameter on every update, unlike adaptive optimizers such as Adam which scale each parameter's step individually. Setting a nonzero momentum accumulates a running average of past gradients, letting updates build up speed in a consistent direction and helping push through small local irregularities in the loss landscape — this closely mirrors the physical intuition of a ball rolling downhill and gathering momentum, rather than responding only to the immediate, current slope.

💡

Plain SGD with no momentum can converge quite slowly and get stuck oscillating in narrow valleys of the loss landscape — adding a moderate momentum value, commonly 0.9, is a simple, well-established way to meaningfully speed up and stabilize training.

editor.html
import tensorflow as tf

optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
print(optimizer.learning_rate.numpy())
localhost:3000

2Practical Example

Here is a real-world application of optimizers.SGD() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9), loss='mse')
print(model.optimizer.momentum)
localhost:3000

3Best Practices

Follow these guidelines when working with optimizers.SGD():

1. Add momentum, commonly around 0.9, when using SGD, since plain SGD with momentum=0 typically converges much more slowly than momentum-based or adaptive alternatives

2. Expect to need more manual learning-rate tuning with SGD than with an adaptive optimizer like Adam, since it lacks any per-parameter adjustment

3. Consider SGD with momentum for the final fine-tuning stages of a model, since some research suggests it can reach slightly better final generalization than Adam in certain settings, despite typically converging more slowly

⚠️

Tip: Plain SGD with no momentum can converge quite slowly and get stuck oscillating in narrow valleys of the loss landscape — adding a moderate momentum value, commonly 0.9, is a simple, well-established way to meaningfully speed up and stabilize training.

editor.html
import tensorflow as tf

optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
print(optimizer.learning_rate.numpy())
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
print(optimizer.learning_rate.numpy())
Example 02Advanced Example
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9), loss='mse')
print(model.optimizer.momentum)

Best Practices

  • Add momentum, commonly around 0.9, when using SGD, since plain SGD with momentum=0 typically converges much more slowly than momentum-based or adaptive alternatives
  • Expect to need more manual learning-rate tuning with SGD than with an adaptive optimizer like Adam, since it lacks any per-parameter adjustment
  • Consider SGD with momentum for the final fine-tuning stages of a model, since some research suggests it can reach slightly better final generalization than Adam in certain settings, despite typically converging more slowly

Interview Question

What problem does adding momentum to SGD specifically help solve, compared to plain SGD with momentum=0?

Hint: Think about how plain SGD behaves in a narrow, curved valley of the loss landscape.

Plain SGD updates each parameter based solely on the current, immediate gradient at that exact step, with no memory of previous update directions — in a loss landscape shaped like a narrow, curved valley, this tends to cause the parameters to oscillate back and forth across the steep, narrow direction of the valley while making only slow, halting progress along its shallow, elongated direction toward the actual minimum. Momentum addresses this by accumulating a running average of past gradients, so updates that consistently point in a similar direction across steps build up speed, while oscillating components pointing in alternating directions tend to cancel out — the net effect is that momentum smooths out the oscillation across the valley's steep direction while accelerating genuine progress along its shallow direction, generally leading to noticeably faster, more stable convergence than plain SGD alone.

Exercises

MediumPractice using optimizers.SGD() in a real scenario.
View Solution
import tensorflow as tf

optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
print(optimizer.learning_rate.numpy())

Frequently Asked Questions

What problem does adding momentum to SGD specifically help solve, compared to plain SGD with momentum=0?

Plain SGD updates each parameter based solely on the current, immediate gradient at that exact step, with no memory of previous update directions — in a loss landscape shaped like a narrow, curved valley, this tends to cause the parameters to oscillate back and forth across the steep, narrow direction of the valley while making only slow, halting progress along its shallow, elongated direction toward the actual minimum. Momentum addresses this by accumulating a running average of past gradients, so updates that consistently point in a similar direction across steps build up speed, while oscillating components pointing in alternating directions tend to cancel out — the net effect is that momentum smooths out the oscillation across the valley's steep direction while accelerating genuine progress along its shallow direction, generally leading to noticeably faster, more stable convergence than plain SGD alone.

Related Functions

optimizers-adamoptimizers-rmspropmodel-compile