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).unwrap();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
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>>
Updates parameters using the given gradients Read more
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Gets the current learning rate
Source§fn set_learning_rate(&mut self, learning_rate: A)
fn set_learning_rate(&mut self, learning_rate: A)
Sets a new learning rate
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> 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.