Trung bình 1 đã giải Tỷ lệ chấp nhận 20.0%

Layer Normalization

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ rows ≤ 100, 1 ≤ d ≤ 128

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

Ví dụ

Ví dụ 1
Đầu vào:
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

Kết quả mong đợi:
expected = [
    [-1.224736, 0.0, 1.224736]
]
Đầu vào:
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;

Kết quả mong đợi:
std::vector<std::vector<double>> expected = {
    {-1.224736, 0.0, 1.224736}
};
Đầu vào:
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;

Kết quả mong đợi:
double[][] expected = new double[][] {
    new double[] {-1.224736, 0.0, 1.224736}
};

Giải thích

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.

Ví dụ 2
Đầu vào:
x = [
    [5.0, 5.0]
]
gamma = [2.0, 3.0]
beta = [1.0, -1.0]
epsilon = 1.0e-05

Kết quả mong đợi:
expected = [
    [1.0, -1.0]
]
Đầu vào:
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;

Kết quả mong đợi:
std::vector<std::vector<double>> expected = {
    {1.0, -1.0}
};
Đầu vào:
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;

Kết quả mong đợi:
double[][] expected = new double[][] {
    new double[] {1.0, -1.0}
};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 ННикита Python 809 ms - 5/5 Xem code

Học hỏi từ người khác

Giải pháp cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem mã của người khác và đánh giá AI. Đăng nhập và giải thử thách để tham gia thảo luận.