pub struct Adam { /* private fields */ }Expand description
Adam optimiser state.
Keeps first- and second-moment vectors m and v for every parameter and
applies bias correction using the timestep t.
Implementations§
Source§impl Adam
impl Adam
Sourcepub fn new(param_count: usize) -> Self
pub fn new(param_count: usize) -> Self
Create a new Adam optimiser for param_count parameters.
Defaults follow the original paper: lr = 0.001, beta1 = 0.9,
beta2 = 0.999, eps = 1e-8.
Sourcepub fn with_lr(learning_rate: f32, param_count: usize) -> Self
pub fn with_lr(learning_rate: f32, param_count: usize) -> Self
Create an Adam optimiser with a custom learning rate.
Sourcepub fn with_hyperparams(
learning_rate: f32,
beta1: f32,
beta2: f32,
eps: f32,
param_count: usize,
) -> Self
pub fn with_hyperparams( learning_rate: f32, beta1: f32, beta2: f32, eps: f32, param_count: usize, ) -> Self
Create an Adam optimiser with full control over all hyperparameters.
Sourcepub fn param_count(&self) -> usize
pub fn param_count(&self) -> usize
Number of parameters tracked by this optimiser.
Sourcepub fn step(&mut self, params: &mut [f32], grad: &[f32])
pub fn step(&mut self, params: &mut [f32], grad: &[f32])
Perform one Adam update on params using the supplied gradient.
params and grad must have the same length as the optimiser’s
parameter count.
Sourcepub fn step_clipped(&mut self, params: &mut [f32], grad: &[f32], max_norm: f32)
pub fn step_clipped(&mut self, params: &mut [f32], grad: &[f32], max_norm: f32)
Perform one Adam update after clipping the gradient to max_norm.
If the global gradient norm is larger than max_norm, the whole
gradient is rescaled before the Adam moment update. This matches the
common “clip by global norm” behaviour.
Sourcepub fn step_slices(&mut self, params: &mut [&mut [f32]], grads: &[&[f32]])
pub fn step_slices(&mut self, params: &mut [&mut [f32]], grads: &[&[f32]])
Perform one Adam update on multiple parameter slices.
params and grads must have the same number of slices, and each
corresponding slice must have the same length. This avoids flattening
and reloading parameters when a model is stored in separate tensors.
Sourcepub fn step_slices_clipped(
&mut self,
params: &mut [&mut [f32]],
grads: &[&[f32]],
max_norm: f32,
)
pub fn step_slices_clipped( &mut self, params: &mut [&mut [f32]], grads: &[&[f32]], max_norm: f32, )
Perform one Adam update on multiple slices after global norm clipping.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Adam
impl RefUnwindSafe for Adam
impl Send for Adam
impl Sync for Adam
impl Unpin for Adam
impl UnsafeUnpin for Adam
impl UnwindSafe for Adam
Blanket Implementations§
impl<T> Allocation for T
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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