Skip to main content
Batch normalization

Batch normalization

Search complete. 40 mentions across 4 episodes found for "Batch normalization".

Oct 31, 2026

Andrej KarpathyHOST
3:29
And linear just does a matrix multiply in the forward pass of this module.
Andrej KarpathyHOST
3:34
BatchNorm, of course, is this crazy layer that we developed in the previous lecture.
Andrej KarpathyHOST
3:38
And what's crazy about it is, well, there's many things.
Andrej KarpathyHOST
3:42
Number one, it has these running mean and variances that are trained outside of backward application.
Andrej KarpathyHOST
5:20
This should look very familiar.
Andrej KarpathyHOST
5:21
We have our embedding table C, and then we have a list of players.
Andrej KarpathyHOST
5:26
And it's a linear, feeds to BatchNorm, feeds to 10H, and then a linear output layer.
Andrej KarpathyHOST
5:31
And its weights are scaled down, so we are not confidently wrong at initialization.
Andrej KarpathyHOST
54:21
But how do we do that when our neural net now in a forward pass estimates the statistics of the mean understanding deviation of a batch? The neural net expects batches as an input now.
Andrej KarpathyHOST
54:30
So how do we feed in a single example and get sensible results out? And so the proposal in the batch normalization paper is the following.
Andrej KarpathyHOST
54:38
What we would like to do here is we would like to basically have a step after training that calculates and sets the BatchNorm mean and standard deviation a single time over the training set.
Andrej KarpathyHOST
54:52
And so I wrote this code here in the interest of time, and we're going to call what's called calibrate the BatchNorm statistics.
Andrej KarpathyHOST
54:59
And basically what we do is torch.nograd telling PyTorch that none of this, we will call a dot backward on, and it's going to be a bit more efficient.
Andrej KarpathyHOST
55:08
We're going to take the training set, get the pre-activations for every single training example, and then one single time estimate the mean and standard deviation over the entire training set.

9 MINS LATER

Andrej KarpathyHOST
63:51
And these are not trained using backpropagation.
Andrej KarpathyHOST
63:53
These are trained using this janky update, kind of like a running mean update.
Andrej KarpathyHOST
10:51
and instead we're going to analytically derive using math and paper and pencil the gradient of the loss with respect to the logits and instead of back propagating through all of its little chunks one at a time we're just going to analytically drive what that gradient is and we're going to implement that which is much more efficient as we'll see in a bit Then we're going to do the exact same thing for BatchNormalization.
Andrej KarpathyHOST
11:14
So instead of breaking up BatchNorm into all the little tiny components, we're going to use pen and paper and mathematics and calculus to derive the gradient through the BatchNorm layer.
Andrej KarpathyHOST
11:25
So we're going to calculate the backward pass through BatchNorm layer in a much more efficient expression instead of backward propagating through all of its little pieces independently.
Andrej KarpathyHOST
11:33
So that's going to be exercise three.
Andrej KarpathyHOST
11:36
And then in exercise four, we're going to put it all together.

44 MINS LATER

Andrej KarpathyHOST
55:12
Okay, next up, we have dhPreact, and we want to backpropagate into the gain, the bnRaw, and the bnBias.
Andrej KarpathyHOST
55:19
So here, this is the bash norm parameters, bnGain and bias, inside the bash norm, that take the bnRaw, that is exact unit Gaussian, and they scale it and shift it.
Andrej KarpathyHOST
55:29
and these are the parameters of the BatchNorm.
Andrej KarpathyHOST
40:40
Now, right now, for our model as is, this doesn't actually do anything because the only thing inside this model is this nn.embedding. And this network would behave the same in both evaluation mode and training mode.
Andrej KarpathyHOST
40:56
We have no dropout layers, we have no BatchNorm layers, etc.
Andrej KarpathyHOST
40:59
But it is a good practice to think through what mode your neural network is in, because some layers will have different behavior at inference time or training time.
Andrej KarpathyHOST
41:11
There's also this context manager torch.nograd, and this is just telling PyTorch that everything that happens inside this function, we will not call .backward on.

51 MINS LATER

Andrej KarpathyHOST
92:28
And we also see that the network is starting to get big enough that our train loss is getting ahead of validation loss.
Andrej KarpathyHOST
92:33
So we started to see a little bit of overfitting.
Andrej KarpathyHOST
92:36
And our... generations here are still not amazing but at least you see that we can see like is here this now grief sync like this starts to almost look like english so um yeah we're starting to really get there okay and the second innovation that is very helpful for optimizing very deep neural networks is right here so we have this addition now that's the residual part but this norm is referring to something called layer norm so layer norm is implemented in pytorch it's a paper that came out a while back here and layer norm is very very similar to batch norm so remember back to our make more series part three we implemented batch normalization and batch normalization basically just made sure that across the batch dimension any individual neuron had unit Gaussian distribution so it was zero mean and unit standard deviation one standard deviation output So what I did here is I'm copy pasting the BatchNorm1D that we developed in our MakeMore series.
Andrej KarpathyHOST
93:41
And see here, we can initialize, for example, this module, and we can have a batch of 32 100 dimensional vectors feeding through the BatchNorm layer.

We value your privacy

We use cookies to understand how you use our platform and to improve your experience. Click “Accept All” to consent, or “Decline non-essential” to opt out of non-essential cookies. Read our Privacy Policy.