pub struct SAM<A, O, D>{ /* private fields */ }Expand description
Sharpness-Aware Minimization (SAM) optimizer
SAM is an optimization technique that seeks parameters that lie in neighborhoods having uniformly low loss values, which improves generalization. It achieves this by performing a two-step update process:
- Compute and take a step in the direction of the “sharpness” gradient (perturbed parameters)
- Compute the gradient at these perturbed parameters and use it to update the original parameters
This implementation wraps around a base optimizer and modifies its behavior to implement the SAM algorithm.
§Parameters
inner_optimizer- The optimizer to use for the parameter updatesrho- The neighborhood size for perturbation (default: 0.05)epsilon- Small constant for numerical stability (default: 1e-12)adaptive- Whether to use adaptive perturbation size (SAM-A) (default: false)
§Example
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{SAM, SGD};
use optirs_core::Optimizer;
// Create a base optimizer
let sgd = SGD::new(0.1);
// Wrap it with SAM
let mut optimizer = SAM::new(sgd);
// First step to compute perturbed parameters and store perturbed gradients
let params = Array1::zeros(10);
let gradients = Array1::ones(10);
let (perturbed_params_) = optimizer.first_step(¶ms, &gradients).unwrap();
// Second step to update original parameters using gradients at perturbed parameters
// Normally, you would compute new gradients at perturbed_params
let new_gradients = Array1::ones(10) * 0.5; // Example new gradients
let updated_params = optimizer.second_step(¶ms, &new_gradients).unwrap();Implementations§
Source§impl<A, O, D> SAM<A, O, D>
impl<A, O, D> SAM<A, O, D>
Sourcepub fn new(inner_optimizer: O) -> Self
pub fn new(inner_optimizer: O) -> Self
Creates a new SAM optimizer with the given inner optimizer and default settings
Sourcepub fn with_config(inner_optimizer: O, rho: A, adaptive: bool) -> Self
pub fn with_config(inner_optimizer: O, rho: A, adaptive: bool) -> Self
Creates a new SAM optimizer with the given inner optimizer and configuration
Sourcepub fn with_epsilon(self, epsilon: A) -> Self
pub fn with_epsilon(self, epsilon: A) -> Self
Set the epsilon parameter (numerical stability)
Sourcepub fn with_adaptive(self, adaptive: bool) -> Self
pub fn with_adaptive(self, adaptive: bool) -> Self
Set whether to use adaptive perturbation size (SAM-A)
Sourcepub fn inner_optimizer(&self) -> &O
pub fn inner_optimizer(&self) -> &O
Get the inner optimizer
Sourcepub fn inner_optimizer_mut(&mut self) -> &mut O
pub fn inner_optimizer_mut(&mut self) -> &mut O
Get a mutable reference to the inner optimizer
Sourcepub fn is_adaptive(&self) -> bool
pub fn is_adaptive(&self) -> bool
Check if using adaptive perturbation size
Sourcepub fn first_step(
&mut self,
params: &Array<A, D>,
gradients: &Array<A, D>,
) -> Result<(Array<A, D>, A)>
pub fn first_step( &mut self, params: &Array<A, D>, gradients: &Array<A, D>, ) -> Result<(Array<A, D>, A)>
Trait Implementations§
Source§impl<A, O, D> Optimizer<A, D> for SAM<A, O, D>
impl<A, O, D> Optimizer<A, D> for SAM<A, O, D>
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 set_learning_rate(&mut self, learning_rate: A)
fn set_learning_rate(&mut self, learning_rate: A)
Sets a new learning rate
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Gets the current learning rate
Auto Trait Implementations§
impl<A, O, D> Freeze for SAM<A, O, D>
impl<A, O, D> RefUnwindSafe for SAM<A, O, D>
impl<A, O, D> Send for SAM<A, O, D>
impl<A, O, D> Sync for SAM<A, O, D>
impl<A, O, D> Unpin for SAM<A, O, D>
impl<A, O, D> UnwindSafe for SAM<A, O, D>
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.