pub struct GradAccumulator { /* private fields */ }Expand description
Accumulates parameter gradients across multiple micro-batches.
Supports mean-reduction normalisation and global-norm gradient clipping.
Implementations§
Source§impl GradAccumulator
impl GradAccumulator
Sourcepub fn accumulate(&mut self, name: &str, grad: &Array1<f32>) -> ModelResult<()>
pub fn accumulate(&mut self, name: &str, grad: &Array1<f32>) -> ModelResult<()>
Accumulate grad into the named parameter slot.
If no gradient for name exists yet, it is initialised to zeros of
the same length before accumulation.
§Errors
Returns ModelError::numerical_instability if grad contains NaN
or Inf, or ModelError::dimension_mismatch if the lengths differ
between calls for the same name.
Sourcepub fn get(&self, name: &str) -> Option<&Array1<f32>>
pub fn get(&self, name: &str) -> Option<&Array1<f32>>
Return a reference to the accumulated gradient for name, if any.
Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Divide each accumulated gradient by its accumulation count (mean reduction).
Sourcepub fn apply_clip(&mut self, max_norm: f32) -> f32
pub fn apply_clip(&mut self, max_norm: f32) -> f32
Clip gradients by global L2 norm.
Computes the L2 norm across all parameter gradients; if it exceeds
max_norm, scales all gradients by max_norm / norm.
Returns the L2 norm before clipping.
Sourcepub fn param_names(&self) -> Vec<&str>
pub fn param_names(&self) -> Vec<&str>
Return the names of all parameters with accumulated gradients.
Trait Implementations§
Source§impl Debug for GradAccumulator
impl Debug for GradAccumulator
Source§impl Default for GradAccumulator
impl Default for GradAccumulator
Source§fn default() -> GradAccumulator
fn default() -> GradAccumulator
Auto Trait Implementations§
impl Freeze for GradAccumulator
impl RefUnwindSafe for GradAccumulator
impl Send for GradAccumulator
impl Sync for GradAccumulator
impl Unpin for GradAccumulator
impl UnsafeUnpin for GradAccumulator
impl UnwindSafe for GradAccumulator
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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