We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Neural Network Layers step 2 of 9
Implement Batch Normalization
For a batch of feature vectors, batch normalization computes separate statistics for each feature across the examples. With examples in rows, this means normalizing each column. The result for one example therefore depends on the other examples in its batch.
For x = [[1, 2], [3, 4]], the column means are [2, 3] and the
column variances are [1, 1]. With eps = 1e-5, subtracting the means
and dividing by sqrt(variance + eps) gives approximately
[[-0.999995, -0.999995], [0.999995, 0.999995]].
Each feature then gets its own scale and shift:
output[:, j] = gamma[j] * normalized[:, j] + beta[j].
Your task
Implement batch_norm(x, gamma, beta, eps=1e-5) for the current batch:
xis a floating-point tensor of shape(N, D), with positive dimensions.gammaandbetaeach have shape(D,).epsis a positive float. Add it inside the square root.- Return a tensor of shape
(N, D).
For each column, compute its mean and population variance (sum of
squared deviations divided by N, not N-1). Normalize as
(x - mean) / sqrt(variance + eps), then multiply by gamma and add beta.
Use basic reductions and arithmetic rather than a built-in BatchNorm layer.
This exercise has no running averages, momentum, or evaluation mode. Always use the provided batch's statistics. It also defines N=1: every variance is zero, so every output feature equals its beta value. You are implementing this formula, not all the behavior of a framework's BatchNorm module.
The diagram compares normalization groups. Here columns are features with no spatial axes. With BatchNorm selected, entries in one column 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.