We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Normalization step 2 of 6
Implement Layer Normalization
Layer normalization rescales each example using statistics from that example's features. For a matrix with examples in rows, each row gets its own mean and variance. Changing another row does not affect those statistics.
For the row [1, 2, 3], the mean is 2. The squared deviations are
[1, 0, 1], so the variance is 2 / 3. Subtract the mean and divide by
sqrt(2 / 3 + eps). With eps = 1e-5, the result before scaling and
shifting is approximately [-1.224736, 0, 1.224736].
The final scale gamma and shift beta each contain one value per feature.
For feature j, return gamma[j] * normalized[j] + beta[j].
Your task
Implement layer_norm(x, gamma, beta, eps=1e-5):
xis a floating-point tensor of shape(N, D), with positive dimensions.gammaandbetahave shape(D,)and broadcast across rows.epsis a positive float. Add it to the variance inside the square root.- Return a tensor of shape
(N, D).
Compute row means and population variance: the mean squared deviation,
dividing by D, not D-1. Keep the reduced dimension so the statistics have
shape (N, 1) and broadcast over each row.
Use basic tensor reductions and arithmetic rather than a built-in LayerNorm
operation. A constant row, including a row with D=1, normalizes to zero
before the affine step, so its output is beta. Positive epsilon means
the pre-affine variance is not generally exactly 1; gamma and beta can
also change the output mean and variance.
The diagram compares normalization groups. For this 2D exercise, each column is one feature and there are no extra spatial dimensions. With LayerNorm selected, entries in one row share their statistics.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Decide which values share a mean and variance
Many normalization methods subtract a mean and divide by a standard deviation:
$$\hat x=\frac{x-\mu}{\sqrt{\sigma^2+\epsilon}}.$$
The small positive epsilon keeps the denominator away from zero. The main
difference between methods is which values are used to compute the mean and
variance.
- Batch normalization usually computes one set of statistics per channel, combining examples and spatial positions during training. Its usual inference behavior uses running statistics collected during training.
- Layer normalization computes statistics over selected feature dimensions within each example. Different examples do not share statistics.
- Group normalization divides channels into groups. Within each example, it computes statistics over each group's channels and spatial positions.
For the feature vector [1, 3], the mean is 2 and population variance is 1.
Subtracting the mean gives [-1, 1]; dividing by sqrt(1 + epsilon) leaves values
close to [-1, 1]. Computing statistics over the batch axis instead would combine
values from different examples and produce a different operation.
RMSNorm rescales without centering
RMSNorm divides by the root mean square, with epsilon added for stability:
$$\hat x=\frac{x}{\sqrt{\operatorname{mean}(x^2)+\epsilon}}.$$
It does not subtract the mean, so its output need not have mean zero. For example,
[1, 1] stays close to [1, 1] rather than becoming [0, 0].
Normalization layers may apply learnable scale and bias after normalizing. Their shapes must broadcast over the intended axes. These extra parameters mean that the final output need not have mean zero or variance one.
Match the formula and the source of the statistics
Check whether epsilon goes inside or outside the square root. Also check whether the variance divides by the number of values (population variance) or one fewer (sample variance). These choices change the answer.
For feature preprocessing, estimate statistics on training data and reuse them on validation and test data. Using test data to estimate them leaks information into training. An individual exercise may instead ask you to use only the input it supplies. Follow that contract and handle constant features as specified.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.