pub struct SimdSGD<A: Float> { /* private fields */ }Expand description
SIMD-accelerated Stochastic Gradient Descent optimizer
This is a specialized version of SGD optimized for 1D arrays using SIMD operations. For maximum performance, use this when working with flattened parameter vectors.
Formula: v_t = momentum * v_{t-1} + learning_rate * (gradient + weight_decay * param) param_t = param_{t-1} - v_t
§Performance
This implementation uses SIMD instructions (AVX2/SSE/NEON) for:
- Parameter updates
- Momentum computation
- Weight decay application
Expected speedup: 2-4x over scalar implementation for large parameter arrays
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{SimdSGD, Optimizer};
// Initialize parameters and gradients
let params = Array1::zeros(1000);
let gradients = Array1::from_elem(1000, 0.1);
// Create SIMD-accelerated SGD optimizer
let mut optimizer = SimdSGD::new(0.01);
optimizer.set_momentum(0.9);
// Update parameters with SIMD acceleration
let new_params = optimizer.step(¶ms, &gradients).unwrap();Implementations§
Source§impl<A: Float> SimdSGD<A>
impl<A: Float> SimdSGD<A>
Sourcepub fn new(learning_rate: A) -> Self
pub fn new(learning_rate: A) -> Self
Creates a new SIMD-accelerated SGD optimizer
§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 SIMD SGD optimizer with 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
Sets the momentum factor
Sourcepub fn with_momentum(self, momentum: A) -> Self
pub fn with_momentum(self, momentum: A) -> Self
Builder method to set momentum and return self
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
Sets the weight decay factor
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 current weight decay factor
Trait Implementations§
Source§impl Optimizer<f32, Dim<[usize; 1]>> for SimdSGD<f32>
impl Optimizer<f32, Dim<[usize; 1]>> for SimdSGD<f32>
Source§fn step(
&mut self,
params: &Array1<f32>,
gradients: &Array1<f32>,
) -> Result<Array1<f32>>
fn step( &mut self, params: &Array1<f32>, gradients: &Array1<f32>, ) -> Result<Array1<f32>>
Source§fn get_learning_rate(&self) -> f32
fn get_learning_rate(&self) -> f32
Source§fn set_learning_rate(&mut self, learning_rate: f32)
fn set_learning_rate(&mut self, learning_rate: f32)
Source§impl Optimizer<f64, Dim<[usize; 1]>> for SimdSGD<f64>
impl Optimizer<f64, Dim<[usize; 1]>> for SimdSGD<f64>
Source§fn step(
&mut self,
params: &Array1<f64>,
gradients: &Array1<f64>,
) -> Result<Array1<f64>>
fn step( &mut self, params: &Array1<f64>, gradients: &Array1<f64>, ) -> Result<Array1<f64>>
Source§fn get_learning_rate(&self) -> f64
fn get_learning_rate(&self) -> f64
Source§fn set_learning_rate(&mut self, learning_rate: f64)
fn set_learning_rate(&mut self, learning_rate: f64)
Auto Trait Implementations§
impl<A> Freeze for SimdSGD<A>where
A: Freeze,
impl<A> RefUnwindSafe for SimdSGD<A>where
A: RefUnwindSafe,
impl<A> Send for SimdSGD<A>where
A: Send,
impl<A> Sync for SimdSGD<A>where
A: Sync,
impl<A> Unpin for SimdSGD<A>where
A: Unpin,
impl<A> UnwindSafe for SimdSGD<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
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.