모든 챌린지
보통 1명 해결 정답률 20.0%

Layer Normalization

Normalize each row to zero mean and unit variance, then scale and shift it, as every Transformer block does.

배우게 될 내용

  • Normalize each row to zero mean and unit variance
  • Scale and shift with gamma and beta
  • Understand why epsilon is needed

문제

번역본 보기

Layer normalization keeps the activations of each token on a stable scale. For each row x of the matrix:

  1. mean = Σ x / d and var = Σ (x - mean)² / d (population variance over the row's d values)
  2. x̂ = (x - mean) / √(var + epsilon)
  3. output = gamma × x̂ + beta (element-wise: column j uses gamma[j] and beta[j])

Implement layer_norm(x, gamma, beta, epsilon).

Input

x: A matrix with one row per token and d columns.

gamma, beta: The learned scale and shift, d values each.

epsilon: A small number that avoids division by zero.

Output

Return the normalized matrix, same shape as x.

제약 조건

1 ≤ rows ≤ 100, 1 ≤ d ≤ 128

-100 ≤ each value ≤ 100, 0 < epsilon ≤ 0.001

예시

예시 1
입력:
x = [
    [1.0, 2.0, 3.0]
]
gamma = [1.0, 1.0, 1.0]
beta = [0.0, 0.0, 0.0]
epsilon = 1.0e-05

예상 출력:
expected = [
    [-1.224736, 0.0, 1.224736]
]
입력:
std::vector<std::vector<double>> x = {
    {1.0, 2.0, 3.0}
};
std::vector<double> gamma = {1.0, 1.0, 1.0};
std::vector<double> beta = {0.0, 0.0, 0.0};
double epsilon = 1.0e-05;

예상 출력:
std::vector<std::vector<double>> expected = {
    {-1.224736, 0.0, 1.224736}
};
입력:
double[][] x = new double[][] {
    new double[] {1.0, 2.0, 3.0}
};
double[] gamma = new double[] {1.0, 1.0, 1.0};
double[] beta = new double[] {0.0, 0.0, 0.0};
double epsilon = 1.0e-05;

예상 출력:
double[][] expected = new double[][] {
    new double[] {-1.224736, 0.0, 1.224736}
};

설명

mean = 2 and var = (1 + 0 + 1) / 3 ≈ 0.666667, so √(var + epsilon) ≈ 0.816503. The row becomes about [-1.224736, 0, 1.224736]: centered at 0 with variance 1.

예시 2
입력:
x = [
    [5.0, 5.0]
]
gamma = [2.0, 3.0]
beta = [1.0, -1.0]
epsilon = 1.0e-05

예상 출력:
expected = [
    [1.0, -1.0]
]
입력:
std::vector<std::vector<double>> x = {
    {5.0, 5.0}
};
std::vector<double> gamma = {2.0, 3.0};
std::vector<double> beta = {1.0, -1.0};
double epsilon = 1.0e-05;

예상 출력:
std::vector<std::vector<double>> expected = {
    {1.0, -1.0}
};
입력:
double[][] x = new double[][] {
    new double[] {5.0, 5.0}
};
double[] gamma = new double[] {2.0, 3.0};
double[] beta = new double[] {1.0, -1.0};
double epsilon = 1.0e-05;

예상 출력:
double[][] expected = new double[][] {
    new double[] {1.0, -1.0}
};

설명

Both values equal the mean, so x̂ = [0, 0] and the output is just beta: [1, -1]. Epsilon prevents dividing by zero.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Normalize each row independently using that row’s own mean and variance, not statistics across the whole matrix.

힌트 2

After standardizing a row, apply the learned scale and shift element-wise with gamma and beta.

힌트 3

Use the population variance and add epsilon before the square root to avoid division by zero on constant rows.

예상 복잡도

O(rows * d) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 809 ms - 5/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 솔루션

스포일러: 아래에 1개 전체 솔루션 있음 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.