pub struct LayerNorm<B: Backend, T: Float = f32> {
pub scale: Option<Array<B, T>>,
pub bias: Option<Array<B, T>>,
pub epsilon: f64,
pub use_fast_variance: bool,
}Expand description
Layer normalization over the last dimension, with a learned scale and
bias (flax.linen.LayerNorm).
The mean and the variance are computed as Flax’s _compute_stats does:
by default (use_fast_variance) as E[x^2] - E[x]^2, clipped at zero;
otherwise as E[(x - E[x])^2]. Then y = (x - mean) * (rsqrt(var + epsilon) * scale) + bias, as in Flax’s _normalize. Statistics are
computed in the input type (Flax computes them in f32 for 16-bit
inputs).
§Examples
use ruxla_core::{Array, Native};
use ruxla_nn::LayerNorm;
let ln = LayerNorm::<Native>::new(None, None);
let y = ln.apply(&Array::from_vec(vec![1.0, 3.0], [2])).to_vec();
assert!((y[0] + 1.0).abs() < 1e-5 && (y[1] - 1.0).abs() < 1e-5);Fields§
§scale: Option<Array<B, T>>[features], if the layer has a scale (use_scale).
bias: Option<Array<B, T>>[features], if the layer has a bias (use_bias).
epsilon: f64Added to the variance (1e-6 by default, as in Flax).
use_fast_variance: boolWhether the variance is computed as E[x^2] - E[x]^2 (Flax’s
default).
Implementations§
Source§impl<B: Backend, T: Float> LayerNorm<B, T>
impl<B: Backend, T: Float> LayerNorm<B, T>
Source§impl<T: Float> LayerNorm<Native, T>
impl<T: Float> LayerNorm<Native, T>
Sourcepub fn load(weights: &Weights, prefix: &str) -> Result<Self, WeightsError>
pub fn load(weights: &Weights, prefix: &str) -> Result<Self, WeightsError>
Loads {prefix}/scale and {prefix}/bias, each if there is one,
such as the weights of params/LayerNorm_0 of a Flax model, with
Flax’s default configuration (see LayerNorm::new).
§Errors
Returns an error if a weight has another type or is not a vector.