pub struct SGD<A: Float + ScalarOperand + Debug> { /* private fields */ }Expand description
Stochastic Gradient Descent optimizer
Implements the classic SGD algorithm with support for momentum and weight decay.
Formula: v_t = momentum * v_{t-1} + learning_rate * (gradient + weight_decay * param) param_t = param_{t-1} - v_t
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{SGD, 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 an SGD optimizer with learning rate 0.01 and momentum 0.9
let mut optimizer = SGD::new_with_config(0.01, 0.9, 0.0);
// Update parameters
let new_params = optimizer.step(¶ms, &gradients).expect("optimizer.step succeeds");Implementations§
Source§impl<A: Float + ScalarOperand + Debug + Send + Sync> SGD<A>
impl<A: Float + ScalarOperand + Debug + Send + Sync> SGD<A>
Sourcepub fn new(learning_rate: A) -> Self
pub fn new(learning_rate: A) -> Self
Creates a new SGD optimizer with the given learning rate and no momentum/weight decay
§Arguments
learning_rate- The learning rate for parameter updates
Sourcepub fn new_with_config(learning_rate: A, momentum: A, weight_decay: A) -> Self
pub fn new_with_config(learning_rate: A, momentum: A, weight_decay: A) -> Self
Creates a new SGD optimizer with the full configuration
§Arguments
learning_rate- The learning rate for parameter updatesmomentum- The momentum factor (0.0 means no momentum)weight_decay- The weight decay factor (L2 regularization)
Sourcepub fn set_momentum(&mut self, momentum: A) -> &mut Self
pub fn set_momentum(&mut self, momentum: A) -> &mut Self
Sourcepub fn with_momentum(self, momentum: A) -> Self
pub fn with_momentum(self, momentum: A) -> Self
Builder method to set momentum and return self
§Arguments
momentum- The momentum factor (0.0 means no momentum)
Sourcepub fn get_momentum(&self) -> A
pub fn get_momentum(&self) -> A
Gets the current momentum factor
Sourcepub fn learning_rate(&self) -> A
pub fn learning_rate(&self) -> A
Gets the current learning rate
Sourcepub fn set_weight_decay(&mut self, weight_decay: A) -> &mut Self
pub fn set_weight_decay(&mut self, weight_decay: A) -> &mut Self
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
§Arguments
weight_decay- The weight decay factor (L2 regularization)
Sourcepub fn get_weight_decay(&self) -> A
pub fn get_weight_decay(&self) -> A
Gets the current weight decay factor
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 an SGD update in place for the parameter tensor at index
This is the allocation-free hot path: velocity and parameters are updated in a
single fused Zip traversal, so no temporary arrays are created per step.
Sourcepub fn step_inplace<D: Dimension>(
&mut self,
params: &mut Array<A, D>,
gradients: &Array<A, D>,
) -> Result<()>
pub fn step_inplace<D: Dimension>( &mut self, params: &mut Array<A, D>, gradients: &Array<A, D>, ) -> Result<()>
Applies an SGD update in place using the state slot of the first parameter tensor
Sourcepub fn step_indexed<D: Dimension>(
&mut self,
index: usize,
params: &Array<A, D>,
gradients: &Array<A, D>,
) -> Result<Array<A, D>>
pub fn step_indexed<D: Dimension>( &mut self, index: usize, params: &Array<A, D>, gradients: &Array<A, D>, ) -> Result<Array<A, D>>
Performs an SGD update for the parameter tensor at index
Each index owns an independent momentum slot, so several parameter tensors
can be optimized by a single SGD instance without their velocities mixing.
Trait Implementations§
Source§impl<A, D> Optimizer<A, D> for SGD<A>
impl<A, D> Optimizer<A, D> for SGD<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 SGD<A>where
A: Freeze,
impl<A> RefUnwindSafe for SGD<A>where
A: RefUnwindSafe,
impl<A> Send for SGD<A>where
A: Send,
impl<A> Sync for SGD<A>where
A: Sync,
impl<A> Unpin for SGD<A>where
A: Unpin,
impl<A> UnsafeUnpin for SGD<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for SGD<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.