The same loop everywhere
Optimization sounds fancy, but the idea is ancient. Try a small change, measure the result, keep the change if it helps, and undo it if it hurts.
- A/BA/B testing shows half of users Version A and half Version B. Whichever produces more clicks wins.
- EVOEvolution makes small mutations in DNA. Mutations that help survival spread; harmful ones fade away.
- GDGradient descent tweaks model knobs, checks the loss, and nudges the knobs in the direction that lowers the loss.
All three are the same algorithm dressed up in different clothes. In AI, the “reality check” is a number called the loss — a score for how wrong the model is.
Core idea: Optimization is a search. You are standing on a landscape where height means “error.” Your goal is to find the lowest valley.
A landscape of mistakes
Imagine a map where every point is a possible setting of the model's knobs. The elevation at that point is the loss. Dark blue areas are low (good). Yellow areas are high (bad). Your mission: start somewhere and walk downhill.
Click anywhere on the map to place the starting point.
The arrow at the dot points opposite to the gradient: the direction that lowers loss the fastest at this point.
Going downhill
At any point on the landscape, the gradient tells you which way is uphill. Gradient descent takes a small step in the opposite direction. Repeat that thousands of times and you slide into a valley.
def loss(x, y): return (x - 2)**2 + 2*(y - 2)**2 + 0.5*sin(3*x)*cos(3*y) # partial derivatives point uphill gradient_x = 2*(x - 2) + 1.5*cos(3*x)*cos(3*y) gradient_y = 4*(y - 2) - 1.5*sin(3*x)*sin(3*y) # step downhill x = x - learning_rate * gradient_x y = y - learning_rate * gradient_y
The size of each step is the learning rate. A tiny step is slow but safe. A huge step can leap right over a valley and end up higher than where it started.
Use the widget above to experiment. Try a big learning rate first and watch the dot bounce around. Then lower it and watch it settle.
Local minima and restarts
Not every valley is the lowest valley. A landscape can have many dips. A greedy downhill walker gets stuck in whichever one it lands in first. That is a local minimum.
One fix is a random restart: start in many different places and keep the best valley found. Another fix is to give the walker a little momentum so it can roll through small bumps. Real optimizers like Adam do exactly that.
Why this matters: Modern models have billions of knobs and a landscape with countless valleys. Finding a good-enough valley, not necessarily the absolute best, is what makes AI work.
Think about your own code
Have you ever fixed a bug by changing one number at a time — like adjusting a color, a speed, or a threshold — and rerunning your program? That is optimization. How is that similar to gradient descent? How is it different?
Similar: both make a small change and check the result. Different: when you tinker, you rely on intuition and try a few values. Gradient descent computes the exact direction to change using the gradient, then repeats automatically millions of times on many examples.
Three questions
1. What does the gradient point toward?
2. Why is the learning rate important?
3. What problem does a random restart help solve?
Keep learning
Now that you can walk downhill on a loss landscape, it is time to build the machine itself. The next tutorial stacks tiny calculation units into a neural network.
Part of the AI Explained tutorial series.