pub struct SGD<B>where
B: Backend,{ /* private fields */ }Expand description
Stochastic Gradient Descent optimizer with optional momentum.
§Parameters
lr: learning rate (typical: 0.01 - 0.1)momentum: momentum factor (typical: 0.9, 0 = no momentum)weight_decay: L2 regularization coefficient (typical: 1e-4)
Implementations§
Source§impl<B> SGD<B>where
B: Backend,
impl<B> SGD<B>where
B: Backend,
Sourcepub fn new(
params: Vec<Tensor<B>>,
lr: f64,
momentum: f64,
weight_decay: f64,
) -> SGD<B>
pub fn new( params: Vec<Tensor<B>>, lr: f64, momentum: f64, weight_decay: f64, ) -> SGD<B>
Create a new SGD optimizer.
§Arguments
params: the model parameters to optimizelr: learning ratemomentum: momentum factor (0 for vanilla SGD)weight_decay: L2 regularization strength
Sourcepub fn update_params(&mut self, new_params: Vec<Tensor<B>>)
pub fn update_params(&mut self, new_params: Vec<Tensor<B>>)
Update the parameter references (needed after step() returns new tensors).
Sourcepub fn params_mut(&mut self) -> &mut Vec<Tensor<B>>
pub fn params_mut(&mut self) -> &mut Vec<Tensor<B>>
Mutable access to current parameters (for checkpoint loading).
Trait Implementations§
Source§impl<B> Stateful for SGD<B>where
B: Backend,
impl<B> Stateful for SGD<B>where
B: Backend,
Source§fn state_dict(&self) -> OptimizerState
fn state_dict(&self) -> OptimizerState
Export the optimizer’s internal state as a serializable dictionary.
Source§fn load_state_dict(&mut self, state: &OptimizerState) -> Result<(), Error>
fn load_state_dict(&mut self, state: &OptimizerState) -> Result<(), Error>
Restore the optimizer’s internal state from a previously saved dictionary. Read more
Auto Trait Implementations§
impl<B> Freeze for SGD<B>
impl<B> RefUnwindSafe for SGD<B>
impl<B> Send for SGD<B>
impl<B> Sync for SGD<B>
impl<B> Unpin for SGD<B>
impl<B> UnwindSafe for SGD<B>
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> 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 more