मध्यम 0 हल किए गए 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]
]
beta = [0.0, 0.0, 0.0]
gamma = [1.0, 1.0, 1.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> beta = {0.0, 0.0, 0.0};
std::vector<double> gamma = {1.0, 1.0, 1.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[] beta = new double[] {0.0, 0.0, 0.0};
double[] gamma = new double[] {1.0, 1.0, 1.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]
]
beta = [1.0, -1.0]
gamma = [2.0, 3.0]
epsilon = 1.0e-05

अपेक्षित आउटपुट:
expected = [
    [1.0, -1.0]
]
इनपुट:
std::vector<std::vector<double>> x = {
    {5.0, 5.0}
};
std::vector<double> beta = {1.0, -1.0};
std::vector<double> gamma = {2.0, 3.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[] beta = new double[] {1.0, -1.0};
double[] gamma = new double[] {2.0, 3.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.

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।