pub struct ExponentialMovingAverage { /* private fields */ }Expand description
Exponential Moving Average of model parameters.
Maintains shadow copies of parameters that are updated as a weighted
running average: shadow = decay * shadow + (1 - decay) * param.
This is commonly used to produce a smoothed version of model weights
that often generalises better at inference time.
Implementations§
Source§impl ExponentialMovingAverage
impl ExponentialMovingAverage
Sourcepub fn new(decay: f32) -> Self
pub fn new(decay: f32) -> Self
Creates a new EMA tracker with the given decay factor (e.g. 0.999).
Sourcepub fn register(&mut self, params: &[Tensor])
pub fn register(&mut self, params: &[Tensor])
Registers initial parameter values as shadow copies.
Sourcepub fn update(&mut self, params: &[Tensor])
pub fn update(&mut self, params: &[Tensor])
Updates shadow parameters: shadow = decay * shadow + (1 - decay) * param.
Panics if the number of tensors does not match the registered count or if any tensor length differs from its shadow counterpart.
Sourcepub fn shadow_params(&self) -> &[Tensor]
pub fn shadow_params(&self) -> &[Tensor]
Returns a reference to the shadow parameters.
Sourcepub fn apply_shadow(&self, params: &mut [Tensor])
pub fn apply_shadow(&self, params: &mut [Tensor])
Copies shadow parameter values into the provided mutable slice.
Panics if the slice length does not match the shadow parameter count or if any tensor length differs.
Sourcepub fn num_updates(&self) -> usize
pub fn num_updates(&self) -> usize
Returns the number of update steps performed so far.
Auto Trait Implementations§
impl Freeze for ExponentialMovingAverage
impl RefUnwindSafe for ExponentialMovingAverage
impl Send for ExponentialMovingAverage
impl Sync for ExponentialMovingAverage
impl Unpin for ExponentialMovingAverage
impl UnsafeUnpin for ExponentialMovingAverage
impl UnwindSafe for ExponentialMovingAverage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more