pub struct LBFGS { /* private fields */ }Expand description
Limited-memory BFGS (L-BFGS) optimizer.
L-BFGS is a quasi-Newton method that approximates the inverse Hessian using a limited history of gradient information. It’s efficient for large-scale optimization problems where storing the full Hessian is infeasible.
§Algorithm
- Compute gradient
g_k= ∇f(x_k) - Compute search direction
d_kusing two-loop recursion (approximates H^(-1) *g_k) - Find step size
α_kvia line search (Wolfe conditions) - Update: x_{k+1} =
x_k-α_k*d_k - Store gradient and position differences for next iteration
§Parameters
max_iter: Maximum number of iterations- tol: Convergence tolerance (gradient norm)
- m: History size (typically 5-20, tradeoff between memory and convergence)
§Precision
This type is f32 and NON-generic — deliberately, because adding a generic
or default type parameter would break downstream use sites. For f64 (for
example a softmax-NLL objective whose gradient norm reaches f32 epsilon
near the optimum) use LbfgsF64.
§Example
use aprender::optim::{LBFGS, Optimizer};
use aprender::primitives::Vector;
let mut optimizer = LBFGS::new(100, 1e-5, 10);
// Define Rosenbrock function and its gradient
let f = |x: &Vector<f32>| {
let a = x[0];
let b = x[1];
(1.0 - a).powi(2) + 100.0 * (b - a * a).powi(2)
};
let grad = |x: &Vector<f32>| {
let a = x[0];
let b = x[1];
Vector::from_slice(&[
-2.0 * (1.0 - a) - 400.0 * a * (b - a * a),
200.0 * (b - a * a),
])
};
let x0 = Vector::from_slice(&[0.0, 0.0]);
let result = optimizer.minimize(f, grad, x0);
// Should converge to (1, 1)
assert_eq!(result.status, aprender::optim::ConvergenceStatus::Converged);Implementations§
Trait Implementations§
Source§impl Optimizer for LBFGS
impl Optimizer for LBFGS
Source§fn step(&mut self, _params: &mut Vector<f32>, _gradients: &Vector<f32>)
fn step(&mut self, _params: &mut Vector<f32>, _gradients: &Vector<f32>)
Stochastic update (mini-batch mode) - for SGD, Adam,
RMSprop. Read moreAuto Trait Implementations§
impl Freeze for LBFGS
impl RefUnwindSafe for LBFGS
impl Send for LBFGS
impl Sync for LBFGS
impl Unpin for LBFGS
impl UnsafeUnpin for LBFGS
impl UnwindSafe for LBFGS
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
Mutably borrows from an owned value. Read more
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> ⓘ
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 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> ⓘ
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