pub struct Adam { /* private fields */ }Expand description
Pure-Rust PyTorch-compatible Adam optimizer (without decoupled weight decay) with state export/import.
Implementations§
Source§impl Adam
impl Adam
Sourcepub fn new(
vars: &[(String, Var)],
lr: f64,
beta1: f64,
beta2: f64,
eps: f64,
) -> Result<Self, CodecError>
pub fn new( vars: &[(String, Var)], lr: f64, beta1: f64, beta2: f64, eps: f64, ) -> Result<Self, CodecError>
Creates an Adam optimizer tracking vars.
§Errors
Returns CodecError::Candle if moment buffer allocation fails.
Sourcepub fn learning_rate(&self) -> f64
pub fn learning_rate(&self) -> f64
Returns the active learning rate.
Sourcepub fn set_learning_rate(&mut self, lr: f64)
pub fn set_learning_rate(&mut self, lr: f64)
Updates the optimizer learning rate.
Sourcepub fn step(&mut self, grads: &GradStore) -> Result<(), CodecError>
pub fn step(&mut self, grads: &GradStore) -> Result<(), CodecError>
Performs one Adam update step over all tracked parameters present in grads.
§Errors
Returns CodecError::Candle if tensor updates fail.
Sourcepub fn step_clipped(
&mut self,
grads: &GradStore,
max_norm: f64,
) -> Result<f64, CodecError>
pub fn step_clipped( &mut self, grads: &GradStore, max_norm: f64, ) -> Result<f64, CodecError>
Computes global gradient L2 norm, applies global norm clipping up to max_norm
(when max_norm > 0.0), and performs one fused Adam update step.
§Errors
Returns CodecError if gradient norm reduction or parameter updates fail.
Sourcepub fn export_state(
&self,
prefix: &str,
out_tensors: &mut HashMap<String, Tensor>,
) -> Result<BTreeMap<String, u64>, CodecError>
pub fn export_state( &self, prefix: &str, out_tensors: &mut HashMap<String, Tensor>, ) -> Result<BTreeMap<String, u64>, CodecError>
Exports moment tensors into out_tensors and returns per-parameter step counts for checkpointing.
§Errors
Returns CodecError::Candle if transferring moments to CPU fails.
Sourcepub fn import_state(
&mut self,
prefix: &str,
steps: &BTreeMap<String, u64>,
tensors: &HashMap<String, Tensor>,
) -> Result<(), CodecError>
pub fn import_state( &mut self, prefix: &str, steps: &BTreeMap<String, u64>, tensors: &HashMap<String, Tensor>, ) -> Result<(), CodecError>
Restores step counts and moment buffers from a loaded checkpoint.
§Errors
Returns CodecError if any parameter state is missing or mismatched in shape.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Adam
impl !UnwindSafe for Adam
impl Freeze for Adam
impl Send for Adam
impl Sync for Adam
impl Unpin for Adam
impl UnsafeUnpin for Adam
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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