Layer Normalization
Normalize within one token’s feature vector, using population variance and epsilon inside the square root. Learned gain and bias then restore useful scale.
import math
values = [1, 3]
mean, variance, epsilon = 2, 1, 3
print([(x-mean)/math.sqrt(variance+epsilon) for x in values])From arithmetic to an experiment
Normalize within one token’s feature vector, using population variance and epsilon inside the square root. Learned gain and bias then restore useful scale. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
layer_norm(values, gain, bias, epsilon) returns per-feature gain*(x-mean)/sqrt(variance+epsilon)+bias. Inputs are nonempty with matching widths; epsilon > 0.
The explanation opens here once every check passes.