Class: Ignis::AI::NN::LayerNorm
- Defined in:
- lib/nnw/ai/nn/layer_norm.rb
Overview
Layer normalization: y = gamma * (x - mean) / sqrt(var + eps) + beta Normalizes along the last dimension(s).
Instance Attribute Summary collapse
-
#bias ⇒ Tensor
readonly
Beta (shift).
-
#weight ⇒ Tensor
readonly
Gamma (scale).
Attributes inherited from Module
Instance Method Summary collapse
-
#forward(x) ⇒ Tensor
Forward pass: applies layer normalization.
-
#initialize(normalized_shape, eps: 1e-5, device_id: 0) ⇒ LayerNorm
constructor
A new instance of LayerNorm.
- #to_s ⇒ String
Methods inherited from Module
#call, #eval!, #load_state_dict, #named_parameters, #num_parameters, #parameters, #state_dict, #to, #train!, #zero_grad!
Constructor Details
#initialize(normalized_shape, eps: 1e-5, device_id: 0) ⇒ LayerNorm
Returns a new instance of LayerNorm.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/nnw/ai/nn/layer_norm.rb', line 20 def initialize(normalized_shape, eps: 1e-5, device_id: 0) super() @normalized_shape = normalized_shape @eps = eps # Initialize weight (gamma) to ones weight_nv = Ignis::Shared::NvArray.new(shape: [normalized_shape], dtype: :float32, device_id: device_id) weight_nv.from_host(Array.new(normalized_shape, 1.0)) @weight = register_parameter("weight", Tensor.new(data: weight_nv, requires_grad: true)) # Initialize bias (beta) to zeros bias_nv = Ignis::Shared::NvArray.new(shape: [normalized_shape], dtype: :float32, device_id: device_id) bias_nv.from_host(Array.new(normalized_shape, 0.0)) @bias = register_parameter("bias", Tensor.new(data: bias_nv, requires_grad: true)) end |
Instance Attribute Details
#bias ⇒ Tensor (readonly)
Returns beta (shift).
15 16 17 |
# File 'lib/nnw/ai/nn/layer_norm.rb', line 15 def bias @bias end |
#weight ⇒ Tensor (readonly)
Returns gamma (scale).
12 13 14 |
# File 'lib/nnw/ai/nn/layer_norm.rb', line 12 def weight @weight end |
Instance Method Details
#forward(x) ⇒ Tensor
Forward pass: applies layer normalization.
43 44 45 |
# File 'lib/nnw/ai/nn/layer_norm.rb', line 43 def forward(x) x.layer_norm(@weight, @bias, eps: @eps) end |
#to_s ⇒ String
48 49 50 |
# File 'lib/nnw/ai/nn/layer_norm.rb', line 48 def to_s "LayerNorm(#{@normalized_shape}, eps=#{@eps})" end |