Layer Normalization
Easy
~12 min
code completion
Layer Normalization
Layer normalization is similar to batch normalization but normalizes across features within each sample, instead of across the batch.
For a single sample vector of shape (d,):
This makes it batch-size independent — crucial for Transformers, where sequence lengths vary and batch sizes may be small.
Your task:
Implement layer_normalize(x, eps) for a single 1D input vector.
Example Tests
Sum of output is 0 (zero mean)
Input: {"x":[1,2,3,4,5],"eps":0}
Expected: 0
Known normalized values
Input: {"x":[1,3,5],"eps":0}
Expected: [-1.22474,0,1.22474]
Constant input: zero after normalization (with eps)
Input: {"x":[7,7,7],"eps":1}
Expected: [0,0,0]