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:
- A warm-up phase where learning rate increases and momentum decreases
- 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>
impl<A: Float + ScalarOperand + Debug + Send + Sync> OneCycle<A>
Sourcepub fn new(
initial_lr: A,
max_lr: A,
total_steps: usize,
warmup_frac: f64,
) -> Self
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 ratemax_lr- Maximum learning rate reached after warm-uptotal_steps- Total number of training steps.0is invalid and is clamped to1; useOneCycle::try_newto 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.
Sourcepub fn try_new(
initial_lr: A,
max_lr: A,
total_steps: usize,
warmup_frac: f64,
) -> Result<Self>
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_fracis not finite or is outside the open interval(0, 1),initial_lrormax_lris not finite, orinitial_lr <= 0ormax_lr < initial_lr.
Sourcepub fn with_final_lr(self, final_lr: A) -> Self
pub fn with_final_lr(self, final_lr: A) -> Self
Create with specific final learning rate
Sourcepub fn with_momentum(
self,
min_momentum: A,
max_momentum: A,
base_momentum: A,
) -> Self
pub fn with_momentum( self, min_momentum: A, max_momentum: A, base_momentum: A, ) -> Self
Set momentum cycling parameters
Sourcepub fn with_anneal_strategy(self, strategy: AnnealStrategy) -> Self
pub fn with_anneal_strategy(self, strategy: AnnealStrategy) -> Self
Set annealing strategy for cool-down phase
Sourcepub fn total_steps(&self) -> usize
pub fn total_steps(&self) -> usize
Total number of steps in the cycle (always >= 1)
Sourcepub fn warmup_steps(&self) -> usize
pub fn warmup_steps(&self) -> usize
Number of warm-up steps (always < total_steps)
Sourcepub fn get_momentum(&self) -> Option<A>
pub fn get_momentum(&self) -> Option<A>
Get current momentum value
Sourcepub fn get_percentage_complete(&self) -> A
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 + ScalarOperand + Debug + Send + Sync> LearningRateScheduler<A> for OneCycle<A>
impl<A: Float + ScalarOperand + Debug + Send + Sync> LearningRateScheduler<A> for OneCycle<A>
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Source§fn step_with_metric(&mut self, metric: A) -> A
fn step_with_metric(&mut self, metric: A) -> A
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> 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
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.