Skip to main content

Gradients

Struct Gradients 

Source
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

Source

pub fn new(mlp: &Mlp) -> Self

Allocate gradient buffers suitable for mlp.

Source

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) into grads.d_output_mut()
  • mlp.backward(input, &scratch, &mut grads)
Source

pub fn d_output(&self) -> &[f32]

Immutable view of the final upstream gradient buffer.

Source

pub fn d_input(&self) -> &[f32]

Returns dL/d(input) computed by the most recent backward call.

Source

pub fn d_weights(&self, layer_idx: usize) -> &[f32]

Returns the weight gradient for the given layer (row-major (out_dim, in_dim)).

Source

pub fn d_weights_mut(&mut self, layer_idx: usize) -> &mut [f32]

Mutable weight gradient for the given layer.

Source

pub fn d_biases(&self, layer_idx: usize) -> &[f32]

Returns the bias gradient for the given layer (length out_dim).

Source

pub fn d_biases_mut(&mut self, layer_idx: usize) -> &mut [f32]

Mutable bias gradient for the given layer.

Source

pub fn zero_params(&mut self)

Zero the parameter gradient buffers (d_weights and d_biases).

Source

pub fn scale_params(&mut self, scale: f32)

Scale parameter gradients (d_weights and d_biases) in place.

Source

pub fn global_l2_norm_params(&self) -> f32

Compute the global L2 norm of parameter gradients.

Source

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§

Source§

impl Clone for Gradients

Source§

fn clone(&self) -> Gradients

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Gradients

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V