Layer normalization: one embedding, any scale #Layernorm#Normalization#Interactive
How our TFM handles normalization and stability → How it’s built
Scale one purchase's 16-number embedding up or down and watch layer normalization keep its output steady, then see the learned gain and bias reshape it.
Transformer foundationsEmbeddings
Layer normalization works on one vector at a time. For one transaction embedding x (16 numbers here), it computes the mean μ and the spread σ = √(variance + ε). It then rewrites each number as x̂ = (x − μ) / σ. The result always has mean 0 and spread 1, so the next layer sees the same pattern whether the raw inputs are tiny, huge or shifted.
After that, a learned gain γ and bias β are applied to each dimension (y = γ·x̂ + β). With them, the model can bring back whatever scale or offset helps. They start at 1 and 0 and are learned during training.
In this lab, the 16 input numbers and the "learned" gain pattern are made up for illustration. Every mean, spread and output shown is calculated live from the formula.
Transcript
A Transaction Foundation Model turns one purchase into a vector, here sixteen numbers. Layer normalization reshapes that vector before each major block of the transformer.
It works on one vector at a time, so each transaction is normalized on its own, without looking at any other.
First it measures the vector's mean, the gold line, and its spread, the violet band.
Then it subtracts the mean and divides by the spread. The result always has a mean of zero and a spread of one.
Now the inputs are turned up and down. The raw bars grow and shrink, yet the normalized rows hardly change.
Whatever the scale of the input, the next layer sees the same pattern, which keeps training stable.
Even tiny inputs, shrunk to a third and then shifted down by three, give the same normalized pattern. Only the vector's shape is kept; its size and its offset are removed, before the next block ever sees them.
Then a learned gain stretches each dimension, and a learned bias shifts it, so the model can bring back any scale that helps.
They start at one and zero and are learned in training. The inputs and this gain pattern are just an example; every result is computed live.
Now it's your turn: try the sliders, or follow one number through each step. Layer normalization keeps the shape, and lets the model choose the scale.
Chapters
In code (PyTorch)
import torch, torch.nn as nn
x = torch.randn(1, 16) # one transaction embedding
ln = nn.LayerNorm(16) # weight = gain γ (init 1), bias = β (init 0)
for s in (0.1, 1.0, 5.0):
y = ln(x * s + 2.0) # scale and shift the raw input
print(s, y[0, :4]) # nearly identical every time