pub struct Lookahead<A, O, D>{ /* private fields */ }Expand description
Lookahead optimizer
Implements the “Lookahead Optimizer: k steps forward, 1 step back” algorithm. This optimizer maintains two sets of weights: “fast” weights that are updated by an inner optimizer, and “slow” weights that follow behind at a controlled pace.
The algorithm proceeds by:
- Starting with both sets of weights synchronized
- Letting the fast weights explore using the inner optimizer for k steps
- Then updating the slow weights to move partially toward the fast weights
- Resetting the fast weights back to the slow weights
- Repeating this process
This provides more stable optimization by allowing aggressive exploration while maintaining a conservative trajectory.
§Parameters
inner_optimizer- The optimizer to use for fast weight updatesalpha- The step size for slow weight updates (default: 0.5)k- The number of fast weight updates before updating slow weights (default: 5)
§Example
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{Lookahead, SGD};
use optirs_core::Optimizer;
// Create an inner optimizer
let sgd = SGD::new(0.01);
// Wrap it with Lookahead
let mut optimizer = Lookahead::new(sgd);
// Use like any other optimizer
let params = Array1::zeros(10);
let gradients = Array1::ones(10);
let updated_params = optimizer.step(¶ms, &gradients).unwrap();Implementations§
Source§impl<A, O, D> Lookahead<A, O, D>
impl<A, O, D> Lookahead<A, O, D>
Sourcepub fn new(inner_optimizer: O) -> Self
pub fn new(inner_optimizer: O) -> Self
Creates a new Lookahead optimizer with the given inner optimizer and default settings
Sourcepub fn with_config(inner_optimizer: O, alpha: A, k: usize) -> Self
pub fn with_config(inner_optimizer: O, alpha: A, k: usize) -> Self
Creates a new Lookahead optimizer with the specified alpha and k values
Sourcepub fn with_alpha(self, alpha: A) -> Self
pub fn with_alpha(self, alpha: A) -> Self
Set the alpha parameter (slow weights step size)
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 use_slow_weights_for_eval(&mut self)
pub fn use_slow_weights_for_eval(&mut self)
Switches to using slow weights for evaluation Call this before evaluation to get better performance
Sourcepub fn use_fast_weights_for_train(&mut self)
pub fn use_fast_weights_for_train(&mut self)
Switches to using fast weights for training Call this after evaluation to resume training
Trait Implementations§
Source§impl<A, O, D> Optimizer<A, D> for Lookahead<A, O, D>
impl<A, O, D> Optimizer<A, D> for Lookahead<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>>
Source§fn set_learning_rate(&mut self, learning_rate: A)
fn set_learning_rate(&mut self, learning_rate: A)
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Auto Trait Implementations§
impl<A, O, D> Freeze for Lookahead<A, O, D>
impl<A, O, D> RefUnwindSafe for Lookahead<A, O, D>
impl<A, O, D> Send for Lookahead<A, O, D>
impl<A, O, D> Sync for Lookahead<A, O, D>
impl<A, O, D> Unpin for Lookahead<A, O, D>
impl<A, O, D> UnwindSafe for Lookahead<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
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.