pub struct Gradients { /* private fields */ }Expand description
Parameter gradients for an Mlp (overwrite semantics).
Allocate once via Mlp::gradients() and reuse across training steps.
Implementations§
Source§impl Gradients
impl Gradients
Sourcepub fn d_output_mut(&mut self) -> &mut [f32]
pub fn d_output_mut(&mut self) -> &mut [f32]
Mutable view of the upstream gradient buffer for the final model output.
Typical training flow:
mlp.forward(input, &mut scratch)- loss writes
dL/d(output)intograds.d_output_mut() mlp.backward(input, &scratch, &mut grads)
Sourcepub fn d_weights(&self, layer_idx: usize) -> &[f32]
pub fn d_weights(&self, layer_idx: usize) -> &[f32]
Returns the weight gradient for the given layer (row-major (out_dim, in_dim)).
Sourcepub fn d_weights_mut(&mut self, layer_idx: usize) -> &mut [f32]
pub fn d_weights_mut(&mut self, layer_idx: usize) -> &mut [f32]
Mutable weight gradient for the given layer.
Sourcepub fn d_biases(&self, layer_idx: usize) -> &[f32]
pub fn d_biases(&self, layer_idx: usize) -> &[f32]
Returns the bias gradient for the given layer (length out_dim).
Sourcepub fn d_biases_mut(&mut self, layer_idx: usize) -> &mut [f32]
pub fn d_biases_mut(&mut self, layer_idx: usize) -> &mut [f32]
Mutable bias gradient for the given layer.
Sourcepub fn zero_params(&mut self)
pub fn zero_params(&mut self)
Zero the parameter gradient buffers (d_weights and d_biases).
Sourcepub fn scale_params(&mut self, scale: f32)
pub fn scale_params(&mut self, scale: f32)
Scale parameter gradients (d_weights and d_biases) in place.
Sourcepub fn global_l2_norm_params(&self) -> f32
pub fn global_l2_norm_params(&self) -> f32
Compute the global L2 norm of parameter gradients.
Sourcepub fn clip_global_norm_params(&mut self, max_norm: f32) -> f32
pub fn clip_global_norm_params(&mut self, max_norm: f32) -> f32
Clip parameter gradients by global norm.
Returns the pre-clip norm.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Gradients
impl RefUnwindSafe for Gradients
impl Send for Gradients
impl Sync for Gradients
impl Unpin for Gradients
impl UnwindSafe for Gradients
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