r/MachineLearning • ML Engineer • 18d ago

Discussion Did we ever really get to the bottom of why BatchNorm works? [D]

It's been a long time since I studied this. Just checking in on an old friend.

I also realize it's fallen out of favor for being extremely inconvenient, but why did it ever work?

74 Upvotes

28 comments sorted by

73

u/orangehumanoid 18d ago

This paper found that "internal covariate shift" wasn't right and it's more about making the loss surface nicer for optimization.

30

u/jebuarary 18d ago

All I remember in terms of the explanation is the internal covariant shift memes about batchnorm. I’ve admittedly always used it pretty blindly. BN and Adam optimizer both fell under the category of so much more stable than not using that I didn’t even question it.

But this is a good explanation: https://www.reddit.com/r/MachineLearning/comments/1o7pgbl/d_what_is_internal_covariate_shift/

9

u/NaivelyBayesic 17d ago

I don’t really think it really "stopped" working, I think it just became a pain to deal with.

Most of the issues folks seem to run into were from the batch dependence. Your forward pass for one sample depends on whatever else happens to be in the batch. Fine for imagenet at 256... not so fine for small batches, variable length sequences, or anything multi-gpu where you have to sync stats. And then inference uses running averages so train and test never quite line up.

LayerNorm doesn’t have any of that since it’s just within the sample. So I think for transformers people just went with it and it stuck. 🤷‍♂️

6

u/js49997 17d ago edited 17d ago

Basically keeps things well conditioned and prevents blow up or decay of activation and gradient magnitudes

4

u/KitchenGiraffe1 17d ago

*and gradient magnitudes 

1

u/js49997 17d ago

indeed, thanks!

2

u/sadnesspie42069 17d ago

Conceptually it would be like applying a series of convolutions on the loss landscape which makes the loss landscape look more like a normal distribution. You are literally sampling a distribution and taking a mean such as in this:

Https://www.statscalculators.com/resources/simulations/central-limit-theorem

Https://www.lesswrong.com/posts/YM6Qgiz9RT7EmeFpp/how-long-does-it-take-to-become-gaussian

0

u/XTXinverseXTY ML Engineer 17d ago

I'm sorry, I can't follow the connection here

when you say batchnorm is like applying a series of convolutions on the loss landscape, can you maybe write that out in notation? what's being convolved with what? over the activations or over the params?

1

u/sadnesspie42069 17d ago edited 17d ago

Think of it like the Central Limit Theorem through probability convolutions.

In probability, the distribution of a sum or average of independent variables is literally a convolution of their PDFs.

When we sample rows into a batch and normalize them, we're doing the exact same thing: drawing samples, averaging them (which convolves the underlying distributions), and standardizing the result just like you would when repeating trials for the CLT

The sample space is the product of convolving the loss landscape with itself which transforms it towards a normal distribution. This means that layer residuals will look like normal distributions and backprop helps to generalize through this mechanism.

Convolutional neural networks (which do not perform convolution operations but rather cross-correlation) usually benefitted from these because earlier layers would stabilize more easily to learn shapes and their combinations. If you want a visual walkthrough of this I would walk through that lens and look at the activations

2

u/XTXinverseXTY ML Engineer 17d ago edited 12d ago

When we sample rows into a batch and normalize them, we're doing the exact same thing: drawing samples, averaging them (which convolves the underlying distributions), and standardizing the result just like you would when repeating trials for the CLT

Okay I can see why each activation would be z-scored, but that only centers/scales it, doesn't make the activations look more Gaussian. CLT would imply that sampling distribution of the moment statistics would converge to Gaussian but that's trivial and you're claiming something different

The sample space is the product of convolving the loss landscape with itself which transforms it towards a normal distribution

Like, the loss landscape itself literally looks Gaussian-shaped, as a function of the parameters? Or like that of a Gaussian? Or just that its sampling distribution is Gaussian for fixed params (probably not your point; this is independent of batchnorm)?

1

u/sadnesspie42069 17d ago edited 17d ago

It would be that the residuals of the layers' loss landscapes will be normal distributions.

Note: This holds strictly under Mean Squared Error (MSE) loss. Because MSE assumes Gaussian residuals and produces linear gradients (\nabla L \propto y - \hat{y}), mini-batching acts as a true linear convolution over the error distribution, and Gaussian closure ensures the CLT standardization in BatchNorm is mathematically exact

0

u/XTXinverseXTY ML Engineer 17d ago

It would be that the activations of the layers will be normal distributions.

if you mean "normally distributed", then this is false, post-BN activations are no closer to gaussian than before. Suppose x is a one-hot feature vector

1

u/sadnesspie42069 17d ago

Sorry about that it's not the activations that are gaussian but rather their residuals

1

u/sadnesspie42069 17d ago

Tldr The distribution of the activations is what is convolved per layer of batch norm to answer your question

14

u/TheRealStepBot 18d ago edited 17d ago

It lives in a weird place. You might need normalization. But normalization leaks and needs you to load the whole dataset.

So you pick batch norm for being easier than a full norm to handle all the numerical issues.

But in many very useful datasets like images video and tokenized text not only do you not need norming as they are already in a fixed range but also datasets are massive.

You may still need to reach for norming of some kind when training smaller models on smaller datasets that are not well scaled.

That’s my casual understanding

9

u/HeavenlyAllspotter 18d ago

Why do yo say you don't need norming? Every popular LLM that i can think of has some kind of norm.

11

u/Jojanzing 18d ago edited 17d ago

See child comment.

Batch norm normalizes the data, so that each batch has mean 0 and sd 1. Since text is encoded as a categorical distribution over tokens, normalizing the input isn't necessary. Youre probably thinking of some kind of layer norm, which normalizes activations not data

2

u/robotnarwhal 17d ago edited 17d ago

Strikeout is just one tilde (~) on each side. You used two.

Correction: it's two after all. I mixed it up with another format. Not sure why it isn't working for you, though.

2

u/Jojanzing 17d ago

Other sources say it should be two, and using one doesn't work either.

2

u/robotnarwhal 17d ago

Strange.

~This sentence was written with one tilde on each side.~

The one was written with two.

2

u/Jojanzing 17d ago

I just tried it again, this time it worked! lol

2

u/Background_Camel_711 17d ago

Batch norm also normalises activations. The difference is layer norm calculates the normalisation statistics across the activations of single sample/token. Batch norm instead calculates the normalisation statistics per activation output across samples within a batch. Its the same operation across different dimensions.

Not sure if you’ve misunderstood but batch norm is typically applied to the internal representations of the model, not the input space.

2

u/Jojanzing 17d ago

Yes it appears I did misunderstand, thanks for the correction.

0

u/o-h_2792562 15d ago

Batch-statistic noise can also regularize training; that's a separate benefit from the optimization story.

-1

u/lqstuart 17d ago

It doesn’t do anything, it’s just ten years of idiots cargo culting shit

-7

u/altmly 18d ago

I don't think it's a huge mystery. Batch norm is the same idea as instance norm, but the problem with instance norm is that you're whitening the samples, whereas batch norm has a proxy for overall feature distribution rather than per sample feature distribution.