Problems
Loading...

07/08Layer Normalization

Easy

Implement layer normalization over the final dimension.

y=γ⊙x−μσ2+ε+βy = \gamma \odot \frac{x-\mu}{\sqrt{\sigma^2+\varepsilon}} + \beta

Here, (\mu) and (\sigma^2) are the population mean and variance of each final-axis feature vector, (\gamma) is the learned scale, (\beta) is the learned shift, and (\varepsilon) is eps. Return a NumPy float64 array with the same shape as x.

Loading visualization...

Examples

Input: x = [[1.0, 2.0, 3.0, 4.0]], gamma = [1.0, 1.0, 1.0, 1.0], beta = [0.0, 0.0, 0.0, 0.0]

Output: [[-1.3416, -0.4472, 0.4472, 1.3416]]

Explanation: The mean of [1, 2, 3, 4] is 2.5 and variance is 1.25. Each value is centered and scaled, then multiplied by gamma and shifted by beta (both identity here).

Input: x = [[1.0, 2.0, 3.0, 4.0]], gamma = [2.0, 2.0, 2.0, 2.0], beta = [1.0, 1.0, 1.0, 1.0]

Output: [[-1.6833, 0.1056, 1.8944, 3.6833]]

Input: x = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], gamma = [1.0, 1.0, 1.0], beta = [0.0, 0.0, 0.0]

Output: [[-1.2247, 0.0, 1.2247], [-1.2247, 0.0, 1.2247]]

Hint 1

Use axis=-1 and keepdims=True for both statistics.

Hint 2

Normalize with np.sqrt(variance + eps).

Hint 3

Broadcast gamma and beta over all leading dimensions.

Requirements

  • Use NumPy float64 computations.
  • Compute mean and population variance along the final axis.
  • Keep the reduced dimension for broadcasting.
  • Apply gamma and beta after normalization.
  • Return the unrounded array.

Constraints

  • x has at least one dimension.
  • gamma and beta match x's final width.
  • eps is positive.
  • Valid inputs are guaranteed.