pub struct LBFGS<A: Float + ScalarOperand + Debug> { /* private fields */ }Expand description
L-BFGS optimizer
Implements the Limited-memory Broyden-Fletcher-Goldfarb-Shanno (L-BFGS) algorithm. This is a quasi-Newton method that approximates the Hessian inverse using a limited amount of memory by storing only a few vectors from previous iterations.
§Curvature pairs
The optimizer stores the previous parameters and the previous gradient, so the
curvature pair is the true s = x_k - x_{k-1}, y = g_k - g_{k-1} even when the
caller post-processes the returned parameters (projection, clipping, weight decay,
a different step size, …). Pairs with y·s <= 0 are skipped so the implicit
inverse-Hessian stays positive definite.
§Step size and line search
Optimizer::step applies a fixed step size (the learning rate) along the
two-loop direction: it has no access to the objective, so it cannot run a line
search. Use LBFGS::step_with_loss to get a backtracking Armijo line search that
uses the configured c1 and max_ls parameters.
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{LBFGS, Optimizer};
// Initialize parameters and gradients
let params = Array1::zeros(5);
let gradients = Array1::from_vec(vec![0.1, 0.2, -0.3, 0.0, 0.5]);
// Create an L-BFGS optimizer
let mut optimizer = LBFGS::new(1.0);
// Update parameters
let new_params = optimizer.step(¶ms, &gradients).expect("optimizer.step succeeds");Implementations§
Source§impl<A: Float + ScalarOperand + Debug + Send + Sync> LBFGS<A>
impl<A: Float + ScalarOperand + Debug + Send + Sync> LBFGS<A>
Sourcepub fn new(learning_rate: A) -> Self
pub fn new(learning_rate: A) -> Self
Creates a new L-BFGS optimizer with the given learning rate
§Arguments
learning_rate- The learning rate for parameter updates
Sourcepub fn new_with_config(
learning_rate: A,
history_size: usize,
tolerance_grad: A,
c1: A,
c2: A,
max_ls: usize,
) -> Self
pub fn new_with_config( learning_rate: A, history_size: usize, tolerance_grad: A, c1: A, c2: A, max_ls: usize, ) -> Self
Creates a new L-BFGS optimizer with full configuration
§Arguments
learning_rate- The learning rate for parameter updateshistory_size- Number of past gradients/steps to store (default: 100)tolerance_grad- Gradient norm tolerance for convergence (default: 1e-7)c1- Wolfe line search parameter for Armijo condition (default: 1e-4)c2- Wolfe line search parameter for curvature condition (default: 0.9)max_ls- Maximum line search iterations (default: 25)
Sourcepub fn learning_rate(&self) -> A
pub fn learning_rate(&self) -> A
Gets the current learning rate
Sourcepub fn history_len(&self) -> usize
pub fn history_len(&self) -> usize
Number of curvature pairs currently stored
Sourcepub fn last_curvature_pair(&self) -> Option<(&Array1<A>, &Array1<A>)>
pub fn last_curvature_pair(&self) -> Option<(&Array1<A>, &Array1<A>)>
The most recently stored curvature pair (s, y), if any.
s is the true parameter difference x_k - x_{k-1} (as observed across two
consecutive calls) and y is the corresponding gradient difference
g_k - g_{k-1}.
Sourcepub fn initial_hessian_scale(&self) -> A
pub fn initial_hessian_scale(&self) -> A
The current initial inverse-Hessian scaling gamma_k = (s·y) / (y·y).
Sourcepub fn set_line_search_contraction(&mut self, rho: A) -> Result<()>
pub fn set_line_search_contraction(&mut self, rho: A) -> Result<()>
Sets the backtracking contraction factor used by LBFGS::step_with_loss.
Must lie strictly between 0 and 1; other values are rejected.
Sourcepub fn step_with_loss<D, F>(
&mut self,
params: &Array<A, D>,
gradients: &Array<A, D>,
loss_fn: F,
) -> Result<Array<A, D>>
pub fn step_with_loss<D, F>( &mut self, params: &Array<A, D>, gradients: &Array<A, D>, loss_fn: F, ) -> Result<Array<A, D>>
Performs an L-BFGS step with a backtracking Armijo line search.
Unlike Optimizer::step, which has no access to the objective and therefore
applies a fixed step size, this method evaluates loss_fn at trial points and
accepts the first step size satisfying the Armijo sufficient-decrease condition
f(x + alpha * d) <= f(x) + c1 * alpha * g^T dstarting from alpha = learning_rate and contracting by the line search
contraction factor (default 0.5) for at most max_ls iterations. If no trial
step satisfies the condition, the trial with the lowest objective value is used
when it improves on f(x); otherwise the parameters are returned unchanged.
If the two-loop direction is not a descent direction (which can only happen through numerical error, since non-positive curvature pairs are never stored), the search falls back to steepest descent for this step.
§Errors
Returns OptimError::DimensionMismatch if params and gradients have
different shapes, and OptimError::InvalidConfig if the objective is not
finite at the current parameters.
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::LBFGS;
let mut optimizer = LBFGS::new(1.0);
let mut params = Array1::from_vec(vec![2.0_f64, -3.0]);
let loss = |x: &Array1<f64>| x.iter().map(|v| v * v).sum::<f64>();
for _ in 0..30 {
let grads = params.mapv(|v| 2.0 * v);
params = optimizer
.step_with_loss(¶ms, &grads, loss)
.expect("step succeeds");
}
assert!(params.iter().all(|v| v.abs() < 1e-6));Trait Implementations§
Source§impl<A, D> Optimizer<A, D> for LBFGS<A>
impl<A, D> Optimizer<A, D> for LBFGS<A>
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>>
Performs an L-BFGS step with a fixed step size.
This trait method has no access to the objective, so no line search is possible;
the two-loop direction is scaled by the learning rate (reduced by
1 / (1 + ||g||) on the very first step, before any curvature information
exists). Use LBFGS::step_with_loss for the backtracking Armijo line search
that uses the configured c1 and max_ls.
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Source§fn set_learning_rate(&mut self, learning_rate: A)
fn set_learning_rate(&mut self, learning_rate: A)
Auto Trait Implementations§
impl<A> Freeze for LBFGS<A>where
A: Freeze,
impl<A> RefUnwindSafe for LBFGS<A>where
A: RefUnwindSafe,
impl<A> Send for LBFGS<A>where
A: Send,
impl<A> Sync for LBFGS<A>where
A: Sync,
impl<A> Unpin for LBFGS<A>where
A: Unpin,
impl<A> UnsafeUnpin for LBFGS<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for LBFGS<A>where
A: UnwindSafe + RefUnwindSafe,
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> 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
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.