Skip to main content

OneCycle

Struct OneCycle 

Source
pub struct OneCycle<A: Float> { /* private fields */ }
Expand description

One-cycle learning rate policy

The one-cycle policy combines triangular learning rate policy with momentum cycling. It consists of two phases:

  1. A warm-up phase where learning rate increases and momentum decreases
  2. A cool-down phase where learning rate decreases and momentum increases

The schedule is saturating: once total_steps have been taken the learning rate stays at its final value instead of continuing past the end of the cycle (which used to produce negative learning rates).

§Example

use optirs_core::schedulers::{OneCycle, LearningRateScheduler};

let mut scheduler = OneCycle::new(
    0.0001,  // initial learning rate
    0.001,   // max learning rate
    1000,    // total steps
    0.25,    // warm-up percentage
);

// The learning rate will increase from 0.0001 to 0.001 in first 250 steps,
// then decrease to a value lower than initial in remaining 750 steps
for _ in 0..1000 {
    let lr = scheduler.get_learning_rate();
    // Use lr for optimization
    scheduler.step();
}

Implementations§

Source§

impl<A: Float + ScalarOperand + Debug + Send + Sync> OneCycle<A>

Source

pub fn new( initial_lr: A, max_lr: A, total_steps: usize, warmup_frac: f64, ) -> Self

Create a new one-cycle scheduler

§Arguments
  • initial_lr - Starting learning rate
  • max_lr - Maximum learning rate reached after warm-up
  • total_steps - Total number of training steps. 0 is invalid and is clamped to 1; use OneCycle::try_new to reject it instead.
  • warmup_frac - Fraction of total steps used for warm-up (typically 0.2-0.3). Values outside (0, 1) (and non-finite values) are clamped so the resulting schedule always has at least one cool-down step.
Source

pub fn try_new( initial_lr: A, max_lr: A, total_steps: usize, warmup_frac: f64, ) -> Result<Self>

Create a new one-cycle scheduler, validating the configuration

§Errors

Returns OptimError::InvalidConfig when

  • total_steps == 0,
  • warmup_frac is not finite or is outside the open interval (0, 1),
  • initial_lr or max_lr is not finite, or
  • initial_lr <= 0 or max_lr < initial_lr.
Source

pub fn with_final_lr(self, final_lr: A) -> Self

Create with specific final learning rate

Source

pub fn with_momentum( self, min_momentum: A, max_momentum: A, base_momentum: A, ) -> Self

Set momentum cycling parameters

Source

pub fn with_anneal_strategy(self, strategy: AnnealStrategy) -> Self

Set annealing strategy for cool-down phase

Source

pub fn total_steps(&self) -> usize

Total number of steps in the cycle (always >= 1)

Source

pub fn warmup_steps(&self) -> usize

Number of warm-up steps (always < total_steps)

Source

pub fn get_momentum(&self) -> Option<A>

Get current momentum value

Source

pub fn get_percentage_complete(&self) -> A

Get fraction of the cycle that has been completed, clamped to [0, 1]

Trait Implementations§

Source§

impl<A: Float + Debug + Send + Sync> Debug for OneCycle<A>

Source§

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

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

impl<A: Float + ScalarOperand + Debug + Send + Sync> LearningRateScheduler<A> for OneCycle<A>

Source§

fn get_learning_rate(&self) -> A

Get the learning rate at the current step
Source§

fn step(&mut self) -> A

Update the scheduler state and return the new learning rate
Source§

fn reset(&mut self)

Reset the scheduler state
Source§

fn step_with_metric(&mut self, metric: A) -> A

Update the scheduler state using an observed metric and return the new learning rate Read more
Source§

fn apply_to<D: Dimension, O: Optimizer<A, D>>(&self, optimizer: &mut O)
where Self: Sized,

Apply the scheduler to an optimizer

Auto Trait Implementations§

§

impl<A> Freeze for OneCycle<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for OneCycle<A>
where A: RefUnwindSafe,

§

impl<A> Send for OneCycle<A>
where A: Send,

§

impl<A> Sync for OneCycle<A>
where A: Sync,

§

impl<A> Unpin for OneCycle<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for OneCycle<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for OneCycle<A>
where A: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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, 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