📘
Layer Normalization
Implement layer normalization for a single vector.
Layer normalization normalizes across the feature dimension: LayerNorm(x)=σ2+ϵx−μ⋅γ+β
where μ and σ² are the mean and variance of x, γ (gain) and β (bias) are learnable parameters.
For this problem, use γ = 1 and β = 0 (no affine transform), and ε = 1e-5.
Input: Space-separated floats (the vector x) Output: Normalized vector, values rounded to 4 decimal places.
Example:
Input:
1.0 2.0 3.0
Output:
-1.2247 0.0000 1.2247
Reasoning:
- The input vector x is [1.0,2.0,3.0].
- We calculate the mean μ and variance σ2 of x: μ=31.0+2.0+3.0=2.0 and σ2=3(1.0−2.0)2+(2.0−2.0)2+(3.0−2.0)2=32.
- Then, we apply the layer normalization formula: LayerNorm(x)=σ2+ϵx−μ⋅γ+β=32+1e−5x−2.0⋅1+0.
- The final output is calculated by applying the formula to each element of x: [32+1e−51.0−2.0,32+1e−52.0−2.0,32+1e−53.0−2.0] = [−1.2247,0.0000,1.2247].
Constraints:
- 1 <= dimension <= 100
- Use ε = 1e-5 for numerical stability
- γ = 1, β = 0
- Round to 4 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.