Tensor and numerical foundations · Medium

LayerNorm

Problem 5 of 50 · Python and NumPy

Problem

Center and normalize each activation row, then apply a learned affine transform. Use population variance mean((x−mean(x))²), not sample variance. Compute y = (x−mean)/sqrt(variance+eps)*weight+bias.

def layer_norm(x, weight, bias, eps):

Constraints

  • x is N × D; weight and bias have D entries; 1 ≤ N,D ≤ 16.
  • eps ≥ 0 and variance+eps > 0 for every row.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      1,
      3
    ]
  ],
  [
    1,
    1
  ],
  [
    0,
    0
  ],
  0
]
Expected result: [
  [
    -1,
    1
  ]
]

The mean is 2 and variance is 1, giving [-1,1].

Example 2

Arguments: [
  [
    [
      2,
      2
    ]
  ],
  [
    3,
    4
  ],
  [
    1,
    -1
  ],
  1
]
Expected result: [
  [
    1,
    -1
  ]
]

A constant row centers to zero, so the output equals bias [1,-1].

Sign in to solve this problem

Browse all coding problems · Learn the foundations