# Re-Examining LayerNorm

*Please check out the* [*colab notebook*](https://colab.research.google.com/drive/1S39-w4vzX3VzZx_27X_BtrLs442pOJnJ?usp=sharing) *for interactive figures and more detailed technical explanations.*

*This post is part of the work done at *[*Conjecture*](https://www.conjecture.dev/)*.*

*Special thanks to Sid Black, Dan Braun, Carlos Ramón Guevara, Beren Millidge, Chris Scammell, Lee Sharkey, and Lucas Teixeira for feedback on early drafts.*

There's a lot of non-linearities floating around in neural networks these days, but one that often gets overlooked is LayerNorm. This is understandable because it's not "supposed" to be doing anything; it was originally introduced to [stabilize training](https://arxiv.org/abs/1607.06450). Contemporary attitudes about LayerNorm's computational power range from "it's just normalizing a vector" to ["it can do division apparently"](https://openreview.net/forum?id=0g0X4H8yN4I). And theories of mechanistic interpretability such as [features as directions](https://transformer-circuits.pub/2022/toy_model/index.html) and [polytopes](https://www.lesswrong.com/posts/eDicGjD9yte6FLSie/interpreting-neural-networks-through-the-polytope-lens) are unhelpful, or even harmful, in understanding normalization's impact on a network's representations. After all, normalization doesn't alter the direction of vectors, but it still bends lines and planes (the boundaries of polytopes) out of shape. As it turns out, LayerNorm can be used as a general purpose activation function (you can solve MNIST with a LayerNorm MLP, for example). Concretely, it can do things like this:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/571d420e67bb5c98005a282d4e464f8a7e4dba18c7364875.gif)

and this:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/485899c004ba7eda7939142c92caf52c46a1831d23aca924.gif)

We will explain what's going on in these animations later, but the point is that to develop a strong, principled theory of mechanistic interpretability, we need to grapple with this non-linearity.

In [this interactive notebook](https://colab.research.google.com/drive/1S39-w4vzX3VzZx_27X_BtrLs442pOJnJ?usp=sharing), we study LayerNorm systematically using math and geometric intuition to characterize the ways in which it can manipulate data. We show that the core non-linearity of LayerNorm can be understood via simple geometric primitives. We explain how these basic primitives may perform semantic operations. For example, folding can be viewed as extracting extremal features (e.g. separating extreme temperatures from normal ones). We leverage these primitives to understand more complex low-dimensional classification tasks with multiple layers of non-linearities. The methods and intuition developed here extend to non-linearities beyond LayerNorm, and we plan to extend them in future work. Below is an interactive summary of the content of the notebook. (If you find the summary interesting, you really should [check out the notebook](https://colab.research.google.com/drive/1S39-w4vzX3VzZx_27X_BtrLs442pOJnJ?usp=sharing): manipulating the figures helps build geometric intuition.)

Summary
=======

The formula for LayerNorm is something messy like

$$
\text{LayerNorm}[x]=\frac{x-\textrm{E}[x]}{\sqrt{\textrm{Var}[x]+\epsilon}}*\gamma+\beta.
$$

But it turns out the core non-linear operation is (almost) normalizing a vector:

$$
u_\epsilon(x)=\frac{x}{\sqrt{||x||^2+\epsilon}}.
$$

$$Graphically, this function has the iconic sigmoid shape in one dimension (note that in 1D the norm is simply the absolute value).

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/28f7e077623943d4a54cbacc2beaaf2563544d2c9fcb882f.png)

Interesting things start happening when we precompose this normalization function with affine transformations (such as scaling and shifting). Below, we start with a collection of points, $(x,y)$, distributed uniformly on the sphere. Then we compute $u_\epsilon(tx,y)$ (stretch/shrink by a factor of $t$ in the $x$ direction and then normalize). Since normalization guarantees points end up on the circle, this operation *stretches* the distribution along the circle. The right hand panel illustrates how this stretching is captured in the shape of a 1D activation function (the $x$ coordinate of the input against the $x$ coordinate of the output). For example, when $t$ is close to 5, we see that any points with $x$ coordinates not near 0 get compressed towards $x=-1$ or $x=1$. This matches the picture in the circle where we see the points bunch up at the left and right sides of the circle.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/4d106487de598f7c5a60261df0c285f1f7cc0ec34337f313.gif)

If we make the same plots with a shifting operation, $u_\epsilon(x+t,y)$, the operation *folds* the input data.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/548540646f97bec3e8c2366cc79badb9fb4a1efb824541d9.gif)

We can envision possible "semantic" uses for these geometric operations. Stretching can be used to perform an approximate "sign" operation (as in + or - sign). For example, it can take a continuous representation such as a numeric temperature and reduce it to two groups: "hot" vs "cold". Folding can be used to perform an approximate "absolute value" operation. For example, it can be used to separate out extremes from a continuous representation such as temperature and make two groups: "very hot"/"very cold" vs "typical temperature".

The right hand activation plot for folding looks pretty different from typical "activation functions" such as ReLUs or sigmoids. When $t$ is near 2 for example, points with $x$ coordinates near 1 and -1 both get mapped to 1, where points closer to 0 are relaxed away from 1. This gives a characteristic "divet" or, in the language of our semantic explanation, "absolute value" shape. We would not expect this from the 1D activation curve we originally drew. We can explain how this shape arises by instead looking at an activation *surface* for $u_\epsilon$ in 2D. The various 1D shapes arise as *slices* of this 2D activation surface. So its the combination

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/1175108134410c6c31eb54a69c2279864bbb5b11ab06c432.gif)

We can now bring our geometric intuition to bear on a small classification task. Below we have 2 classes forming a spiral. We apply a width 3 MLP with $u_\epsilon$ as the activation function (Linear->$u_\epsilon$->Linear->$u_\epsilon$->Linear). Below, we've sketched the classification boundary it finds after training.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/bf2b2008ed21461cecc4cf8601ebd166799c48c96b63cf4f.png)

If we check out the loss and accuracy curves, we see evidence of a phase transition. We can speculate on why this occurs. Operations like folding in low dimensions can create sharp changes in the classifier output, since points can move with high-velocity relative to changes in weights (see the 1D folding diagram and note that right before the fold is complete some points are jumping very quickly from one side of the circle to the other). Another possibility is that there are regions in the loss landscape of low, but nonzero, gradients that must be traversed to get to a region of higher gradient where the phase transition happens. It's not clear that the folding and loss gradient explanations are mutually exclusive. In future work, we would like to thoroughly investigate this.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/7d391d94f16423ca9edf2aad7a515570170ebbe5e5c11051.png)

To understand the solution the model found to the task, we can embed the data in 3D and linearly interpolate from layer to layer. Specifically, we interpolate from the raw data to the first activation (output of first $u_\epsilon$) to the second activation (output of second $u_\epsilon$). At the end, we rotate the second activation output to aid in visualizing its 3D structure. The neural network has a final separating plane that it applies to construct the class boundary, and we can see roughly where this could go in the visual we have made. 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/485899c004ba7eda7939142c92caf52c46a1831d23aca924.gif)

Essentially, the network has turned the dataset into one where there is a blue cap with a red ring around it, which is more-or-less linearly separable. We can have more fun with this set-up. Why not try a harder dataset with more layers? Let's consider this dataset of a sphere divided into 8-parts, where adjacent parts are in opposite classes:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/930c275f46c220afeaf149a8df41c45fc00b75cf7a21498a.gif)

If we run our model the final activation output looks like this:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/6ea005db6ac1bd8d092f2cb7e3872c1dba9db7dcf1435cf0.gif)

Note that the two classes are now almost linearly separable. To get here, the model has to go through a number of phase transitions and the loss curve looks... weird.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/3c2f86ea4e00f7ac2a7cee4a07a6891a1d6e24f2ff6e4c2c.png)

We can reuse our animation method from the spiral example to visualize the way the model is solving this task. We linearly interpolate from the input to the first activation, then the second activation, and so on.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/8735d161535b857eb59fde29554c1b40ead84249e744233d.gif)

So the model has found an elegant way to fold up the sphere just by using linear and normalization operations! If you've found this interesting, we have a [notebook](https://colab.research.google.com/drive/1S39-w4vzX3VzZx_27X_BtrLs442pOJnJ?usp=sharing) with detailed math and code. In follow-up posts, we plan to look at some variations on this type of analysis and link it to interpreting large models.

From a mechanistic interpretability perspective, we cannot afford to ignore LayerNorm. We may consider finding alternatives to LayerNorm in order to produce models that are better understood by current mechanistic interpretability tools. In the big picture, however, we may have to grapple with the technical difficulties of LayerNorm either way. After all, the core obstacle in understanding LayerNorm is our lack of a theory for mechanistically interpreting non-linear activations. And as long as we cannot account for non-linearities in mechanistic interpretability, we will be unable to robustly explain and constrain the behavior of neural networks. The geometric perspective we have presented here is a step towards a mechanistic theory of non-linear activations that closes the gap in our understanding.