Lookahead

Struct Lookahead 

Source
pub struct Lookahead<A, O, D>
where A: Float + ScalarOperand + Debug, O: Optimizer<A, D> + Clone, D: Dimension,
{ /* 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:

  1. Starting with both sets of weights synchronized
  2. Letting the fast weights explore using the inner optimizer for k steps
  3. Then updating the slow weights to move partially toward the fast weights
  4. Resetting the fast weights back to the slow weights
  5. 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 updates
  • alpha - 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(&params, &gradients).unwrap();

Implementations§

Source§

impl<A, O, D> Lookahead<A, O, D>
where A: Float + ScalarOperand + Debug, O: Optimizer<A, D> + Clone, D: Dimension,

Source

pub fn new(inner_optimizer: O) -> Self

Creates a new Lookahead optimizer with the given inner optimizer and default settings

Source

pub fn with_config(inner_optimizer: O, alpha: A, k: usize) -> Self

Creates a new Lookahead optimizer with the specified alpha and k values

Source

pub fn with_alpha(self, alpha: A) -> Self

Set the alpha parameter (slow weights step size)

Source

pub fn with_k(self, k: usize) -> Self

Set the k parameter (synchronization period)

Source

pub fn inner_optimizer(&self) -> &O

Get the inner optimizer

Source

pub fn inner_optimizer_mut(&mut self) -> &mut O

Get a mutable reference to the inner optimizer

Source

pub fn alpha(&self) -> A

Get the alpha parameter (slow weights step size)

Source

pub fn k(&self) -> usize

Get the k parameter (synchronization period)

Source

pub fn use_slow_weights_for_eval(&mut self)

Switches to using slow weights for evaluation Call this before evaluation to get better performance

Source

pub fn use_fast_weights_for_train(&mut self)

Switches to using fast weights for training Call this after evaluation to resume training

Source

pub fn reset(&mut self)

Resets the internal state

Trait Implementations§

Source§

impl<A, O, D> Clone for Lookahead<A, O, D>
where A: Float + ScalarOperand + Debug, O: Optimizer<A, D> + Clone, D: Dimension,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A, O, D> Debug for Lookahead<A, O, D>
where A: Float + ScalarOperand + Debug, O: Optimizer<A, D> + Clone + Debug, D: Dimension,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, O, D> Optimizer<A, D> for Lookahead<A, O, D>
where A: Float + ScalarOperand + Debug + Send + Sync, O: Optimizer<A, D> + Clone + Send + Sync, D: Dimension,

Source§

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)

Sets a new learning rate
Source§

fn get_learning_rate(&self) -> A

Gets the current learning rate
Source§

fn step_list( &mut self, params_list: &[&Array<A, D>], gradients_list: &[&Array<A, D>], ) -> Result<Vec<Array<A, D>>>

Updates multiple parameter arrays at once Read more

Auto Trait Implementations§

§

impl<A, O, D> Freeze for Lookahead<A, O, D>
where O: Freeze, A: Freeze, D: Freeze,

§

impl<A, O, D> RefUnwindSafe for Lookahead<A, O, D>

§

impl<A, O, D> Send for Lookahead<A, O, D>
where O: Send, A: Send,

§

impl<A, O, D> Sync for Lookahead<A, O, D>
where O: Sync, A: Sync,

§

impl<A, O, D> Unpin for Lookahead<A, O, D>
where O: Unpin, A: Unpin, D: Unpin,

§

impl<A, O, D> UnwindSafe for Lookahead<A, O, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V