pub struct RAdam<A: Float + ScalarOperand + Debug> { /* private fields */ }Expand description
RAdam (Rectified Adam) optimizer
Implements the RAdam algorithm from the paper: “On the Variance of the Adaptive Learning Rate and Beyond” by Liu et al. (2019).
RAdam improves upon Adam by addressing the early-stage training instability with a rectified variance term. It eliminates the need for a warmup period and often leads to better convergence.
Formula: m_t = beta1 * m_{t-1} + (1 - beta1) * g_t v_t = beta2 * v_{t-1} + (1 - beta2) * g_t^2 m_hat_t = m_t / (1 - beta1^t) v_hat_t = v_t / (1 - beta2^t)
rho_inf = 2 / (1 - beta2) - 1 rho_t = rho_inf - 2 * t * beta2^t / (1 - beta2^t)
If rho_t > 4 (the variance of the adaptive learning rate is tractable): r_t = sqrt( ((rho_t - 4)(rho_t - 2) rho_inf) / ((rho_inf - 4)(rho_inf - 2) rho_t) ) theta_t = theta_{t-1} - lr * r_t * m_hat_t / (sqrt(v_hat_t) + epsilon) Else: theta_t = theta_{t-1} - lr * m_hat_t (non-adaptive, SGD-with-momentum-like)
The rectification term r_t tends to 1 as t -> infinity, so late training
behaves like Adam. See Liu et al. (2019), Algorithm 2.
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{RAdam, Optimizer};
// Initialize parameters and gradients
let params = Array1::zeros(5);
let gradients = Array1::from_vec(vec![0.1, 0.2, -0.3, 0.0, 0.5]);
// Create a RAdam optimizer with default hyperparameters
let mut optimizer = RAdam::new(0.001);
// Update parameters
let new_params = optimizer.step(¶ms, &gradients).expect("optimizer.step succeeds");Implementations§
Source§impl<A: Float + ScalarOperand + Debug + Send + Sync> RAdam<A>
impl<A: Float + ScalarOperand + Debug + Send + Sync> RAdam<A>
Sourcepub fn new(learning_rate: A) -> Self
pub fn new(learning_rate: A) -> Self
Creates a new RAdam optimizer with the given learning rate and default settings
§Arguments
learning_rate- The learning rate for parameter updates
Sourcepub fn new_with_config(
learning_rate: A,
beta1: A,
beta2: A,
epsilon: A,
weight_decay: A,
) -> Self
pub fn new_with_config( learning_rate: A, beta1: A, beta2: A, epsilon: A, weight_decay: A, ) -> Self
Creates a new RAdam optimizer with the full configuration
§Arguments
learning_rate- The learning rate for parameter updatesbeta1- Exponential decay rate for the first moment estimates (default: 0.9)beta2- Exponential decay rate for the second moment estimates (default: 0.999)epsilon- Small constant for numerical stability (default: 1e-8)weight_decay- Weight decay factor (default: 0.0)
Sourcepub fn set_epsilon(&mut self, epsilon: A) -> &mut Self
pub fn set_epsilon(&mut self, epsilon: A) -> &mut Self
Sets the epsilon parameter
Sourcepub fn get_epsilon(&self) -> A
pub fn get_epsilon(&self) -> A
Gets the epsilon parameter
Sourcepub fn set_weight_decay(&mut self, weight_decay: A) -> &mut Self
pub fn set_weight_decay(&mut self, weight_decay: A) -> &mut Self
Sets the weight decay parameter
Sourcepub fn get_weight_decay(&self) -> A
pub fn get_weight_decay(&self) -> A
Gets the weight decay parameter
Sourcepub fn learning_rate(&self) -> A
pub fn learning_rate(&self) -> A
Gets the current learning rate
Sourcepub fn timestep(&self, index: usize) -> usize
pub fn timestep(&self, index: usize) -> usize
Returns the timestep recorded for the parameter tensor at index
Returns 0 when the index has never been stepped.
Sourcepub fn rho_t(&self, t: usize) -> Option<A>
pub fn rho_t(&self, t: usize) -> Option<A>
Computes rho_t, the length of the approximated simple moving average at step t
Returns None when t == 0 (no step has been taken yet).
Sourcepub fn rectification_term(&self, t: usize) -> Option<A>
pub fn rectification_term(&self, t: usize) -> Option<A>
Computes the RAdam rectification term r_t for step t
Returns None when the variance is not yet tractable (rho_t <= 4), in which
case RAdam falls back to a non-adaptive, SGD-like update.
r_t converges to 1 as t -> infinity.
Sourcepub fn step_inplace_indexed<D: Dimension>(
&mut self,
index: usize,
params: &mut Array<A, D>,
gradients: &Array<A, D>,
) -> Result<()>
pub fn step_inplace_indexed<D: Dimension>( &mut self, index: usize, params: &mut Array<A, D>, gradients: &Array<A, D>, ) -> Result<()>
Applies a RAdam update in place for the parameter tensor at index
Trait Implementations§
Source§impl<A, D> Optimizer<A, D> for RAdam<A>
impl<A, D> Optimizer<A, D> for RAdam<A>
Source§fn step(
&mut self,
params: &Array<A, D>,
gradients: &Array<A, D>,
) -> Result<Array<A, D>>
fn step( &mut self, params: &Array<A, D>, gradients: &Array<A, D>, ) -> Result<Array<A, D>>
Source§fn step_list(
&mut self,
params_list: &[&Array<A, D>],
gradients_list: &[&Array<A, D>],
) -> Result<Vec<Array<A, D>>>
fn step_list( &mut self, params_list: &[&Array<A, D>], gradients_list: &[&Array<A, D>], ) -> Result<Vec<Array<A, D>>>
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Source§fn set_learning_rate(&mut self, learning_rate: A)
fn set_learning_rate(&mut self, learning_rate: A)
Auto Trait Implementations§
impl<A> Freeze for RAdam<A>where
A: Freeze,
impl<A> RefUnwindSafe for RAdam<A>where
A: RefUnwindSafe,
impl<A> Send for RAdam<A>where
A: Send,
impl<A> Sync for RAdam<A>where
A: Sync,
impl<A> Unpin for RAdam<A>where
A: Unpin,
impl<A> UnsafeUnpin for RAdam<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for RAdam<A>where
A: UnwindSafe + RefUnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.