use crate::nn::module::Module;
use crate::tensor::Tensor;
use crate::error::Result;
const EPSILON: f32 = 1e-5;
pub struct LayerNorm {
gamma: Tensor,
beta: Tensor,
epsilon: f32,
}
impl LayerNorm {
pub fn new(normalized_shape: usize) -> Self {
let gamma = Tensor::ones(&[1, normalized_shape], true);
let beta = Tensor::zeros(&[1, normalized_shape], true);
Self {
gamma,
beta,
epsilon: EPSILON,
}
}
}
impl Module for LayerNorm {
fn forward(&self, inputs: &Tensor) -> Result<Tensor> {
inputs.layer_norm(&self.gamma, &self.beta, self.epsilon)
}
fn parameters(&self) -> Vec<Tensor> {
vec![self.gamma.clone(), self.beta.clone()]
}
}