Gradient Descent Explained Simply
How models learn by measuring error and stepping downhill, with a runnable Python example showing learning rate, steps and local minima.
Short answer: gradient descent explained simply is this: a model measures how wrong it is, works out which direction makes the error smaller, takes a small step that way, and repeats until the error stops improving. It is how most machine learning models, including neural networks, learn their numbers.
What is gradient descent, in plain words?
Imagine you are on a foggy hillside and want to reach the lowest point. You cannot see the valley, but you can feel which way the ground slopes under your feet. So you take a step downhill, feel the slope again, and repeat.
That is gradient descent. The "hill" is the loss: a number that says how wrong the model is. The "position" is the model's settings (its weights). The gradient is the slope: it tells you which direction increases the loss fastest, so you move the opposite way. For a quick refresher on the maths, see How Much Maths Do You Need for Machine Learning?; this post goes deeper into how the steps behave.
What is the update rule?
Every step uses one line:
new_weight = weight - learning_rate * gradient
gradient: the slope of the loss at the current weight. A large slope means a big correction is needed.
learning_rate: how much of that slope you trust. It is a setting you choose, not something the model learns.
The minus sign moves you downhill, not uphill.
A small example you can run
Let the loss be (x - 3) ** 2. Its lowest point is at x = 3, and its gradient is 2 * (x - 3). We start far away at x = 10 and let gradient descent find the answer:
def loss(x):
return (x - 3) ** 2
def gradient(x):
return 2 * (x - 3)
x = 10.0
learning_rate = 0.1
for step in range(1, 26):
x = x - learning_rate * gradient(x)
if step % 5 == 0:
print(step, round(x, 4), round(loss(x), 6))
The output shows x moving towards 3 and the loss shrinking:
5 5.2938 5.261335
10 3.7516 0.564932
15 3.2463 0.060659
20 3.0807 0.006513
25 3.0264 0.000699
Notice that the steps get smaller on their own. Near the bottom the slope is gentle, so the gradient is small and so is the step. A real model does exactly this, only with millions of weights instead of one x.
How does the learning rate change the result?
The learning rate is the most important setting. Using the same code and starting point, here is what happens after 25 steps:
Learning rate | x after 25 steps | What happened |
|---|---|---|
0.1 | about 3.03 | Reached the minimum smoothly |
0.01 | about 7.22 | Too small: heading the right way but very slowly |
1.1 | about -664.8 | Too large: it overshoots, and each jump is bigger than the last (it diverges) |
A rate that is too small wastes time; one that is too large makes the loss bounce around or explode. Finding a good value is a normal part of training.
What are local minima?
Our example has one valley. Real loss surfaces can have several. Gradient descent only feels the slope under its feet, so it settles in whichever valley is closest to where it started. Try this loss, x**4 - 3*x**2 + x, which has two valleys:
def gradient(x):
return 4 * x**3 - 6 * x + 1
for start in (-2.0, 2.0):
x = start
for _ in range(200):
x = x - 0.01 * gradient(x)
print(start, round(x, 3), round(x**4 - 3 * x**2 + x, 3))
-2.0 -1.301 -3.514
2.0 1.131 -1.07
Starting at -2 finds the deeper valley (loss -3.514). Starting at 2 gets stuck in a shallower one (loss -1.07), which is a local minimum. In practice, very large neural networks train well despite this, and tricks such as random starting weights and small random batches help. You do not need to solve it by hand, but you should know it exists.
Does a real model use the whole dataset every step?
Usually not. Batch gradient descent uses all the data for each step, which is slow on big datasets. Stochastic gradient descent uses one example, and mini-batch uses a small group, which is the common choice. Popular optimisers such as Adam are refinements of the same idea. How a network computes the gradient for every weight is covered in How Does a Neural Network Work?.
Common mistakes
Adding the gradient instead of subtracting it, so the loss goes up.
Picking a learning rate by guessing once and never checking the loss curve.
Thinking the model "knows" the answer. It only follows the slope, step by step.
Believing a low training loss always means a good model. It can still fail on new data.
Key Takeaway
Gradient descent is "measure the error, step downhill, repeat". The gradient gives the direction, the learning rate sets the step size, and the starting point can decide which valley you end up in.
For a hands-on walk-through, see the chapter Gradient Descent Explained.
Ready to start? It builds the derivative and gradient ideas step by step, with code you can run. Begin with the free course Maths and Statistics for AI: The Essentials.
Related articles
More in Machine Learning
How Does Machine Learning Detect Fraud?
Machine learning fraud detection scores every transaction using learned fraud patterns and anomaly detection. See why 99% accuracy can catch nothing, a worked precision and recall example, and runnable code.
6 min readDec 2, 2026What Is Cross-Validation? k-Fold Explained Simply
Cross-validation tests a model on several splits of the data and averages the scores. See k-fold on a tiny worked example and run it in scikit-learn.
5 min readDec 1, 2026What Is Data Labeling and Why Does AI Need It?
Data labeling adds the right answers to raw data so models can learn. See the main label types, how to measure labeler agreement, and how AI-assisted labeling works.
5 min readNov 30, 2026What Is Feature Engineering? Better Data for Better Models
Feature engineering turns raw data into inputs a model can learn from. See the common techniques, a runnable Python example, and the mistakes that cause data leakage.
4 min readNov 26, 2026What Is Computer Vision? How AI “Sees” Images
Computer vision lets AI work with images and video. See how a picture becomes numbers, how models learn to recognise things, the main tasks, and where it goes wrong.
5 min readNov 22, 2026What Is NLP (Natural Language Processing)?
NLP is how computers read and produce human language. See how it works, the main tasks, a small Python example, and how NLP relates to LLMs.
4 min readNov 20, 2026
Comments
Sign in to join the discussion.

