pub struct SGD { /* private fields */ }Expand description
Stochastic Gradient Descent optimizer.
SGD updates parameters using the gradient of the loss function:
θ = θ - η * ∇L(θ)With momentum:
v = γ * v + η * ∇L(θ)
θ = θ - vwhere:
- θ is the parameter vector
- η is the learning rate
- γ is the momentum coefficient
- v is the velocity vector
- ∇L(θ) is the gradient of the loss
§Example
use aprender::optim::SGD;
use aprender::primitives::Vector;
// Create SGD with momentum
let mut optimizer = SGD::new(0.1).with_momentum(0.9);
let mut params = Vector::from_slice(&[0.0, 0.0]);
let gradients = Vector::from_slice(&[1.0, 2.0]);
// First step
optimizer.step(&mut params, &gradients);
// With momentum, velocity builds up over iterations
optimizer.step(&mut params, &gradients);Implementations§
Source§impl SGD
impl SGD
Sourcepub fn with_momentum(self, momentum: f32) -> Self
pub fn with_momentum(self, momentum: f32) -> Self
Sets the momentum coefficient.
Momentum helps accelerate SGD in the relevant direction and dampens oscillations. Typical values are 0.9 or 0.99.
§Arguments
momentum- Momentum coefficient between 0.0 and 1.0
§Example
use aprender::optim::SGD;
let optimizer = SGD::new(0.01).with_momentum(0.9);
assert!((optimizer.momentum() - 0.9).abs() < 1e-6);Sourcepub fn learning_rate(&self) -> f32
pub fn learning_rate(&self) -> f32
Returns the learning rate.
Sourcepub fn step(&mut self, params: &mut Vector<f32>, gradients: &Vector<f32>)
pub fn step(&mut self, params: &mut Vector<f32>, gradients: &Vector<f32>)
Updates parameters using gradients.
If momentum is enabled, maintains velocity vectors for each parameter.
§Arguments
params- Mutable reference to parameter vectorgradients- Gradient vector (same length as params)
§Panics
Panics if params and gradients have different lengths.
§Example
use aprender::optim::SGD;
use aprender::primitives::Vector;
let mut optimizer = SGD::new(0.1);
let mut params = Vector::from_slice(&[1.0, 2.0]);
let gradients = Vector::from_slice(&[0.5, 1.0]);
optimizer.step(&mut params, &gradients);
// params = [1.0 - 0.1*0.5, 2.0 - 0.1*1.0] = [0.95, 1.9]
assert!((params[0] - 0.95).abs() < 1e-6);
assert!((params[1] - 1.9).abs() < 1e-6);Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the optimizer state (velocity vectors).
Call this when starting training on a new model or after significant changes to the optimization problem.
§Example
use aprender::optim::SGD;
use aprender::primitives::Vector;
let mut optimizer = SGD::new(0.1).with_momentum(0.9);
let mut params = Vector::from_slice(&[1.0]);
let gradients = Vector::from_slice(&[1.0]);
optimizer.step(&mut params, &gradients);
optimizer.reset();
// Velocity is now reset to zeroSourcepub fn has_momentum(&self) -> bool
pub fn has_momentum(&self) -> bool
Returns whether momentum is enabled.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for SGD
impl<'de> Deserialize<'de> for SGD
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for SGD
impl RefUnwindSafe for SGD
impl Send for SGD
impl Sync for SGD
impl Unpin for SGD
impl UnwindSafe for SGD
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.