pub struct SparseAdam<A: Float + ScalarOperand + Debug> { /* private fields */ }Expand description
SparseAdam optimizer for sparse gradients
Implements a variant of the Adam optimization algorithm that’s optimized for sparse gradients. It only updates the parameters and momentum vectors for indices that have non-zero gradients, saving computation and memory.
This optimizer is particularly useful for large embedding layers or models with sparse input features.
Formula (for non-zero gradient indices): 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) theta_t = theta_{t-1} - alpha * m_hat_t / (sqrt(v_hat_t) + epsilon)
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{SparseAdam, SparseGradient, Optimizer};
// Initialize parameters
let params = Array1::zeros(5);
// Create sparse gradient with non-zero values at indices 1 and 3
let sparse_grad = SparseGradient::new(
vec![1, 3], // Indices
vec![0.2, 0.5], // Values
5 // Total dimension
);
// Create a SparseAdam optimizer
let mut optimizer = SparseAdam::new(0.001);
// Update parameters with sparse gradient
let new_params = optimizer.step_sparse(¶ms, &sparse_grad).unwrap();Implementations§
Source§impl<A: Float + ScalarOperand + Debug + Send + Sync> SparseAdam<A>
impl<A: Float + ScalarOperand + Debug + Send + Sync> SparseAdam<A>
Sourcepub fn new(learning_rate: A) -> Self
pub fn new(learning_rate: A) -> Self
Creates a new SparseAdam 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 SparseAdam 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 for L2 regularization (default: 0.0)
Sourcepub fn with_beta1(self, beta1: A) -> Self
pub fn with_beta1(self, beta1: A) -> Self
Builder method to set beta1 and return self
Sourcepub fn with_beta2(self, beta2: A) -> Self
pub fn with_beta2(self, beta2: A) -> Self
Builder method to set beta2 and return self
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 with_epsilon(self, epsilon: A) -> Self
pub fn with_epsilon(self, epsilon: A) -> Self
Builder method to set epsilon and return self
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 with_weight_decay(self, weight_decay: A) -> Self
pub fn with_weight_decay(self, weight_decay: A) -> Self
Builder method to set weight decay and return self
Sourcepub fn get_weight_decay(&self) -> A
pub fn get_weight_decay(&self) -> A
Gets the weight decay parameter
Sourcepub fn step_sparse(
&mut self,
params: &Array<A, Ix1>,
gradient: &SparseGradient<A>,
) -> Result<Array<A, Ix1>>
pub fn step_sparse( &mut self, params: &Array<A, Ix1>, gradient: &SparseGradient<A>, ) -> Result<Array<A, Ix1>>
Trait Implementations§
Source§impl<A: Clone + Float + ScalarOperand + Debug> Clone for SparseAdam<A>
impl<A: Clone + Float + ScalarOperand + Debug> Clone for SparseAdam<A>
Source§fn clone(&self) -> SparseAdam<A>
fn clone(&self) -> SparseAdam<A>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<A: Debug + Float + ScalarOperand + Debug> Debug for SparseAdam<A>
impl<A: Debug + Float + ScalarOperand + Debug> Debug for SparseAdam<A>
Source§impl<A> Optimizer<A, Dim<[usize; 1]>> for SparseAdam<A>
impl<A> Optimizer<A, Dim<[usize; 1]>> for SparseAdam<A>
Source§fn step(
&mut self,
params: &Array<A, Ix1>,
gradients: &Array<A, Ix1>,
) -> Result<Array<A, Ix1>>
fn step( &mut self, params: &Array<A, Ix1>, gradients: &Array<A, Ix1>, ) -> Result<Array<A, Ix1>>
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 SparseAdam<A>where
A: Freeze,
impl<A> RefUnwindSafe for SparseAdam<A>where
A: RefUnwindSafe,
impl<A> Send for SparseAdam<A>where
A: Send,
impl<A> Sync for SparseAdam<A>where
A: Sync,
impl<A> Unpin for SparseAdam<A>where
A: Unpin,
impl<A> UnwindSafe for SparseAdam<A>where
A: UnwindSafe,
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
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
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.