Lesson 6 of 89 min
Loss landscapes
Flat ground is not always the bottom: it can be a saddle with a way down, and a model with many weights nearly always has one.
After this lesson you can tell a minimum from a saddle point, and say why deep networks still train.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
Where PyTorch starts a layer
A new nn.Linear never starts at zero. Each weight is a random number between minus and plus one over the square root of the layer's inputs. For 768 inputs that is 0.036, the range in the video. A layer with fewer inputs starts with bigger numbers.
torch.manual_seed(0)
layer = torch.nn.Linear(768, 3072)
print(layer.weight.min().item(), layer.weight.max().item())
print(1 / 768 ** 0.5)-0.0360843800008297 0.03608431667089462 0.036084391824351615
Why not all the same number
Zero is not the only bad start. When every weight of a layer starts at one number, every unit gets the same slope, takes the same step, and stays like the others. Here three units start at 0.5 and train on the penguins (x and mass are the video's). After 100 steps they are still one number.
hidden = torch.nn.Linear(1, 3)
out = torch.nn.Linear(3, 1)
for p in [*hidden.parameters(), *out.parameters()]:
torch.nn.init.constant_(p, 0.5)
opt = torch.optim.SGD([*hidden.parameters(), *out.parameters()], lr=0.01)
for step in range(100):
loss = ((out(torch.relu(hidden(x[:, None]))).squeeze() - mass) ** 2).mean()
opt.zero_grad(); loss.backward(); opt.step()
print(hidden.weight.squeeze())tensor([0.1838, 0.1838, 0.1838], grad_fn=<SqueezeBackward0>)
A flat loss curve may be a plateau
When your loss stops falling, it may be a bottom, or only a plateau. This is the video's walker from the ridge, printed every few steps. Between step 10 and step 20 the loss hardly moves. Stop there and you keep a worse line. Run on, and it drops again.
w1 = torch.tensor(0.7, requires_grad=True)
w2 = torch.tensor(-0.69, requires_grad=True)
opt = torch.optim.SGD([w1, w2], lr=0.1)
for step in range(1, 41):
loss = ((x * w1 * w2 + 4.2 - mass) ** 2).mean()
opt.zero_grad(); loss.backward(); opt.step()
if step in (1, 10, 20, 30, 40):
print(step, round(loss.item(), 3))1 2.05 10 0.646 20 0.57 30 0.173 40 0.156
Tell a saddle from a bottom in code
The video cut the landscape into slices and looked. PyTorch can measure the bends instead: hessian holds how the loss bends in every direction, and eigvalsh gives the bend along the most and the least curved ones. A negative bend is a way down: a saddle. At a bottom none is negative, and the near zero is the level valley.
def loss_at(w):
return ((x * w[0] * w[1] + 4.2 - mass) ** 2).mean()
for at in ([0.0, 0.0], [0.7034, 0.7034]):
bend = torch.autograd.functional.hessian(loss_at, torch.tensor(at))
print(at, torch.linalg.eigvalsh(bend))[0.0, 0.0] tensor([-1.9595, 1.9595]) [0.7034, 0.7034] tensor([2.5041e-04, 3.9182e+00])
Run on Python 3.13, PyTorch 2.15.0, pandas 3.1.1 and scikit-learn 1.9.1. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: The chain rule
Every training script calls loss.backward(). What does it do?
2 of 3
A big model's loss has sat flat for many steps, and it's still high. With millions of weights, where is the model most likely standing?
3 of 3
The penguins' line with two layers: its tilt is w1 × w2, and b stays 4.2. A walker starts at w1 = 0 and w2 = 0, where the loss is 0.641. What is its loss after 40 steps at a learning rate of 0.1?
The ideas in this lesson
- Saddle point
- A flat place where the uphill arrow is zero, yet some way still goes down; at a bottom, none does.
- Plateau
- A flat stretch of the loss curve, where a walker near a saddle takes tiny steps before the loss falls again.
- Why big models still train
- With millions of weights, nearly every flat place high up has a way down, so training slows there but rarely stops.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
Flat ground
Here's a question. A model learns like someone walking down a hill. Picture that walk in the dark. You can only feel the ground under your feet.
Then the ground goes flat. Are you at the bottom?
Flat has meant the bottom
If you said yes, this course taught you that. Here's the penguins' bowl again, with its walker. As the walker gets close to the bottom, its uphill arrow shrinks. At the bottom, it's zero. And on a bowl, that's the only place it's zero. So far, flat has always meant the bottom.
Two layers
But a real network isn't one line. It's layers in a row, and each layer multiplies what comes in by its weights.
So let's give our line a second layer. The flipper is multiplied by w1. Then the result is multiplied by w2. Then we add four point two, the same as before.
So the line's tilt is w1 times w2.
This line can't fit the penguins any better than before. We're only adding the layer to see what layers do to the loss.
The loss landscape
So what does its loss look like?
Put the two weights on the floor, and the loss as the height.
Which pairs give the best line?
The best line adds about half a kilo for each centimeter, so its tilt is about a half. Any pair that multiplies to a half gives it.
Zero point seven and zero point seven is one pair. Minus zero point seven and minus zero point seven is another, since minus times minus is plus.
So the bottom isn't one point. It's two long valleys: one where both weights are above zero, one where both are below.
Every point along them gives the same best line, with a loss of about zero point one six.
The loss over all of a model's weights is called its loss landscape. This one isn't a bowl.
A zero arrow
Now look between the two valleys, at zero, zero. Both weights are zero, so the line is flat, with a loss of zero point six four. A walker starts right here. Where will it go?
Nowhere.
Nudge w1 alone.
On its way to the guess, the nudge is multiplied by w2, and w2 is zero. So the guess doesn't move.
The slope along w1 is zero. It's the same for w2: its nudge is multiplied by w1, which is zero too.
Both slopes are zero, so the uphill arrow is zero. Forty steps later, the walker hasn't moved. Its loss curve is a flat line.
A saddle point
The arrow is zero here, just like at the bottom of a bowl. So is this a bottom?
Let's check.
Cut the landscape along a straight line through this point, and look at the loss along it: that's a slice. Take the two diagonals. Where both weights grow together, the line tilts up, the right way. The loss falls into a valley, down to about zero point one six. Where one grows and the other shrinks, the line tilts the wrong way.
The loss climbs up a ridge, past two.
Down one way, up the other, like a horse's saddle. So it's not a bottom.
A flat place with a way down... is a saddle point.
A bottom
Now slice a valley the same way, at zero point seven, zero point seven. Across the valley, the loss rises both ways. Along the valley, it stays almost level. There's no way down. That's a bottom. It's called a minimum.
Into the saddle
Picture a walker that starts high on the ridge, at zero point seven and minus zero point six nine. Its loss is about two. It steps downhill. In five steps, the ground goes flat. It has walked into the saddle point, the flat place in the middle. In the dark, a person could feel around with their feet. This walker only has its arrow, and here the arrow is tiny: about a twentieth of its arrow at the start. Will it stay?
A plateau
Its two numbers weren't exact opposites, so it started a tiny bit off the ridge. So its arrow isn't quite zero, and it points a little toward a valley. Each step is tiny. Then a little bigger. Then the walker turns, and runs down into the valley, to about zero point one six.
Look at its loss curve. A fast drop. Then a long flat stretch. Then a second drop. That flat stretch is a plateau. The walker wasn't stuck. It was slow.
Which valley
Now start a second walker, a tiny bit the other way, at zero point seven and minus zero point seven one. The two walkers come down together, and wait in the saddle together.
Then they go different ways, one into each valley. Different weights. The same line, and the same loss. In this model, every bottom is the best line.
A wall every way
A real model has millions of weights. Can training get stuck in a bottom that's worse than the best?
This is the picture most people have. A curve over one weight, with two dips, and a ball stuck in the shallow one. A bottom that's worse than the best is called a local minimum.
Over one weight, a dip only needs two walls. Left and right. With more weights, a dip needs a wall in every direction. Roughly speaking, think of each weight's direction as a coin toss: up or down. For a bottom, every coin has to land up. With ten weights, that's about one chance in a thousand.
Many weights, a way down
Now take a real model, with millions of weights. For a bottom, millions of coins all have to land up. So at a flat place where the loss is still high, some direction nearly always goes down. It's a saddle, not a bottom.
Studies of big networks find exactly that.
Bad bottoms get rarer as networks grow.
So can training get stuck?
In a small network, yes, it can. In a big one, it slows down on plateaus, and it rarely gets stuck.
In code
Now in real code: the two layers in PyTorch, with both weights at zero. The guess is the flipper times w1 times w2, plus four point two.
Then the loss.
Then backward, which fills in every slope.
What will the two slopes be?
Zero and zero. So every step is zero. A hundred steps later, nothing has moved. Real code never starts at zero. Here's a layer as big as one in GPT-2, made with PyTorch. Print its weights. They're small random numbers, all within four hundredths of zero. Small, but not zero.
So training can find the way down.
Look for a way down
So when the arrow is zero, look for a way down. If there is one, it's a saddle, and training gets out, slowly at first, across a plateau. If there's none, it's a bottom.
The lab
That's the end of this unit. In the lab, you use these two lessons together. You find the slopes of the two-layer line with the chain rule. Then you get a walker off the saddle.
Flat is not always the bottom
So flat ground is not always the bottom. Now try it on this lesson's page, and take the quiz. Find a start that walks into the saddle and stays.
You use this lesson in Lab 2: Get a two-layer line off the saddle, which comes next.
Facts last checked on .
