Layer Normalization

~12 mincode completion

Implement layer_normalize(x, eps) for a single 1D input vector.

Examples

Known normalized values

Input
layer_normalize([1, 3, 5], 0)
Output
[-1.22474, 0, 1.22474]

Constant input: zero after normalization (with eps)

Input
layer_normalize([7, 7, 7], 1)
Output
[0, 0, 0]

Hints

Hint 1

Work directly with the arguments x, eps and return the result rather than printing it.

Hint 2

Watch for this: normalized column wise not element wise.

Requirements

  • x: 1D input vector of shape (d,)

  • eps: Numerical stability constant

  • Return Normalized vector of shape (d,) with mean ≈ 0, std ≈ 1.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 frontier labs, 2 big tech firms, 1 autonomy company, 1 enterprise vendor. Top match scores 81.

Python
import numpy as np

def layer_normalize(x: np.ndarray, eps: float = 1e-8) -> np.ndarray:
    """
    Apply layer normalization to a 1D vector.

    Args:
        x:   1D input vector of shape (d,)
        eps: Numerical stability constant

    Returns:
        Normalized vector of shape (d,) with mean ≈ 0, std ≈ 1.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.