What is Gradient Descent?
Gradient descent is an optimization algorithm used to reduce a model's error. It is one of the most important ideas in machine learning because many models learn by minimizing a loss function.
A loss function measures how wrong the model is. A high loss means bad predictions; a low loss means better predictions. Gradient descent changes the model parameters step by step so that the loss becomes smaller.
Imagine descending a hill using only the slope under your feet:
- Height: model error (loss).
- Position: current model parameters.
- Step: adjust parameters in the local downhill direction.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- Tpoint Tech/Javatpoint, "Gradient Descent in Machine Learning".
The Core Idea
For a model parameter w, gradient descent repeats this update:
- : parameter before update step
t. - : parameter after update step
t. - is the learning rate.
- is the loss
- is its gradient at the current parameter.
The gradient tells us the direction in which the loss increases fastest. Because we want the loss to decrease, we move in the opposite direction.
In compact notation:
- is the parameter being updated
- controls step size.
- is the loss slope with respect to
w.
where:
- is a model parameter.
- is the loss function.
- is the gradient of the loss with respect to
w. - is the learning rate.
When does a downhill update overshoot and diverge?
1. Formula and symbols
- : First model coordinate
- : Second model coordinate
- : loss
- : learning rate
- : update index
The fixed quadratic has curvatures 1 and 4, giving convergence for 0 < η < 0.5.
2. Work with the current inputs
3. Result
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Step-by-Step Training Loop
- Initialize parameters: Start with random or small values.
- Make predictions: Use the current model to predict outputs.
- Compute loss: Compare predictions with true values.
- Compute gradients: Measure how each parameter contributed to the loss.
- Update parameters: Move each parameter in the direction that lowers loss.
- Repeat: Continue until the loss stops improving or the maximum number of iterations is reached.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Learning Rate
The learning rate controls the step size. It is one of the most important hyperparameters in ML.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
If the Learning Rate is Too Small
Training is stable, but very slow. The model may need thousands or millions of updates to reach a useful solution.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
If the Learning Rate is Too Large
The model may jump over the minimum again and again. Loss can oscillate or even explode.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
If the Learning Rate is Reasonable
The model moves quickly at first and then settles near a good minimum.
In real training, the best learning rate often changes over time. Many deep learning systems use learning-rate schedules, warmup, decay, or adaptive optimizers.
References for this section3
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Batch, Stochastic, and Mini-Batch Gradient Descent
Batch size: few careful steps or many noisy ones?
1. Formula and symbols
- : Model parameters (slope and intercept)
- : Training examples (32)
- : Batch size: examples averaged per update
- : The batch drawn for this update (shuffled each epoch)
- : Loss on example i alone
- : Learning rate (0.2 for every batch size here)
B = 1 is stochastic gradient descent, B = n is batch gradient descent, anything between is mini-batch.
2. Work with the current inputs
3. Result
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Batch Gradient Descent
Batch gradient descent uses the entire training dataset before making one update.
Advantages:
- Stable gradient estimate.
- Good for small datasets.
Disadvantages:
- Very slow for large datasets.
- Requires loading all data for each update.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- Tpoint Tech/Javatpoint, "Gradient Descent in Machine Learning".
Stochastic Gradient Descent
Stochastic gradient descent (SGD) updates the model using one training example at a time.
Advantages:
- Fast updates.
- Can escape shallow local minima due to noisy updates.
Disadvantages:
- Noisy learning path.
- Loss may fluctuate strongly.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Mini-Batch Gradient Descent
Mini-batch gradient descent uses a small batch, such as 16, 32, 64, or 128 examples, before each update. This is the common choice in modern deep learning.
Advantages:
- More stable than SGD.
- Faster than full batch training.
- Works efficiently on GPUs.
References for this section3
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- Tpoint Tech/Javatpoint, "Gradient Descent in Machine Learning".
Why Gradients Matter in Deep Learning
Deep learning models may have millions or billions of parameters. Manually adjusting them is impossible. Gradients provide an automatic way to know how each parameter should change.
The backpropagation algorithm efficiently computes these gradients layer by layer. Rumelhart, Hinton, and Williams popularized backpropagation for neural networks in their 1986 Nature paper.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Common Problems
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Local Minima
- Convex loss: may have one global minimum.
- Deep-network loss: can include flat regions, saddle points, and poorly scaled directions.
- Practical issue: an optimizer may progress slowly even without being trapped in a simple local minimum.
Stuck in a local minimum? Momentum can roll over the hill
1. Formula and symbols
- : Model parameter (the ball’s position)
- : Loss: a shallow local valley on the right, the deeper global valley on the left
- : Slope of the loss, 0.4w³ − 2w + 0.6
- : Velocity: the accumulated update
- : Learning rate
- : Momentum: fraction of the previous velocity kept (β = 0 is plain gradient descent)
Momentum lets the update carry speed over a small hill. It is not a guarantee: with other settings it can also overshoot the good valley. Real deep-learning losses have many dimensions, where saddle points matter more than isolated local minima.
2. Work with the current inputs
3. Result
References for this section3
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Vanishing Gradients
In deep networks, gradients can become extremely small as they move backward through layers. Early layers then learn very slowly. Activation functions, normalization, residual connections, and better initialization help reduce this problem.
References for this section3
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Exploding Gradients
Gradients can also become too large, causing unstable updates. Gradient clipping is a common solution, especially in recurrent neural networks.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Overfitting
Gradient descent can reduce training loss while the model becomes worse on new data. Regularization, dropout, early stopping, and validation monitoring help prevent this.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Popular Optimizers Built on Gradient Descent
- Momentum: Smooths updates by remembering the previous direction.
- RMSProp: Adapts the learning rate for each parameter using recent squared gradients.
- Adam: Combines momentum and adaptive learning rates. It is widely used in deep learning.
- AdamW: A variant of Adam with improved weight decay behavior.
References for this section3
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- Tpoint Tech/Javatpoint, "Gradient Descent in Machine Learning".
Python: Gradient Descent Through scikit-learn
You normally do not write gradient descent manually for classical ML. You choose a model that uses it internally. SGDClassifier and SGDRegressor are useful when the dataset is large or arrives in batches.
from sklearn.datasets import make_classification
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
X, y = make_classification(
n_samples=2000,
n_features=20,
n_informative=8,
random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = make_pipeline(
StandardScaler(),
SGDClassifier(loss="log_loss", learning_rate="optimal", random_state=42)
)
model.fit(X_train, y_train)
print("Accuracy:", accuracy_score(y_test, model.predict(X_test)))
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- Tpoint Tech/Javatpoint, "Gradient Descent in Machine Learning".
Python: Manual Gradient Descent for Linear Regression
This tiny example shows the update rule directly. It is not meant to replace libraries, but it makes the mechanics visible.
import numpy as np
rng = np.random.default_rng(42)
X = rng.normal(size=(100, 1))
y = 3 * X[:, 0] + 2 + rng.normal(scale=0.2, size=100)
w = 0.0
b = 0.0
alpha = 0.05
for epoch in range(500):
y_hat = w * X[:, 0] + b
error = y_hat - y
dw = (2 / len(X)) * np.sum(error * X[:, 0])
db = (2 / len(X)) * np.sum(error)
w -= alpha * dw
b -= alpha * db
print("learned weight:", round(w, 2))
print("learned bias:", round(b, 2))
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- Tpoint Tech/Javatpoint, "Gradient Descent in Machine Learning".
Practical Checklist
- Normalize or standardize features before training.
- Start with a reasonable learning rate and monitor the loss curve.
- If loss explodes, reduce the learning rate.
- If loss decreases too slowly, try a larger learning rate or an adaptive optimizer.
- Use a validation set to detect overfitting.
- For neural networks, try Adam or AdamW first, then tune.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Takeaway
- Core question: which parameter change reduces loss locally?
- Learning rate: step size.
- Optimizer: rule that shapes or adapts the steps.
Continue with Classification Algorithms to build on this note.
References for this section3
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
Check your understanding
0 of 4 answered
For w=3, gradient=2 and learning rate 0.1, what is the next w?
For L=(u²+4v²)/2, what is its gradient at (3,2)?
In the lab, the v update multiplies v by 1−4η. What happens at η=0.6?
What does backpropagation compute?
Complete references and further reading
- D. E. Rumelhart, G. E. Hinton, and R. J. Williams, "Learning representations by back-propagating errors", Nature, vol. 323, pp. 533-536, 1986.
- I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT Press, 2016.
- D. P. Kingma and J. Ba, "Adam: A Method for Stochastic Optimization", arXiv:1412.6980, 2014.
- Tpoint Tech/Javatpoint, "Gradient Descent in Machine Learning".