pub struct GradientUtils;Expand description
Utility functions for gradient operations
Implementations§
Source§impl GradientUtils
impl GradientUtils
Sourcepub fn clip_grad_norm(gradients: &mut Vec<Tensor>, max_norm: f32) -> Result<f32>
pub fn clip_grad_norm(gradients: &mut Vec<Tensor>, max_norm: f32) -> Result<f32>
Clip gradients by global norm
This function clips the gradients of a list of tensors by their global norm. If the global norm exceeds max_norm, all gradients are scaled down proportionally.
§Arguments
gradients- Mutable reference to a vector of gradientsmax_norm- Maximum allowed norm for gradient clipping
§Returns
The actual norm of the gradients before clipping
Sourcepub fn clip_grad_value(
gradients: &mut Vec<Tensor>,
clip_value: f32,
) -> Result<()>
pub fn clip_grad_value( gradients: &mut Vec<Tensor>, clip_value: f32, ) -> Result<()>
Clip gradients by value
This function clips each gradient tensor element-wise to be within [-clip_value, clip_value]
§Arguments
gradients- Mutable reference to a vector of gradientsclip_value- Maximum absolute value for each gradient element
Sourcepub fn accumulate_gradients(
accumulated_grads: &mut Vec<Tensor>,
new_grads: &[Tensor],
accumulation_steps: usize,
) -> Result<()>
pub fn accumulate_gradients( accumulated_grads: &mut Vec<Tensor>, new_grads: &[Tensor], accumulation_steps: usize, ) -> Result<()>
Accumulate gradients by averaging
This function adds new gradients to accumulated gradients and scales them by 1/accumulation_steps to maintain proper scaling
§Arguments
accumulated_grads- Mutable reference to accumulated gradientsnew_grads- New gradients to addaccumulation_steps- Number of accumulation steps (for scaling)
Auto Trait Implementations§
impl Freeze for GradientUtils
impl RefUnwindSafe for GradientUtils
impl Send for GradientUtils
impl Sync for GradientUtils
impl Unpin for GradientUtils
impl UnsafeUnpin for GradientUtils
impl UnwindSafe for GradientUtils
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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