liblaf.peach.optim.base
¶
Base protocols and result containers for optimizers.
Classes:
-
BaseProblem–Marker protocol for optimizer problems.
-
Optimizer–Base class for iterative optimizers.
-
Problem–Protocol implemented by differentiable optimization problems.
-
Result–Result code returned by optimizers.
-
Solution–Optimizer output bundle.
-
State–Protocol for optimizer states that expose current parameters.
-
Stats–Protocol for optimizer-specific summary statistics.
BaseProblem
¶
Bases: Protocol
flowchart TD
liblaf.peach.optim.base.BaseProblem[BaseProblem]
click liblaf.peach.optim.base.BaseProblem href "" "liblaf.peach.optim.base.BaseProblem"
Marker protocol for optimizer problems.
Optimizer
¶
Base class for iterative optimizers.
Subclasses keep optimizer-specific data in a mutable state object. A
step implementation mutates the
model state and optimizer state in place, so callbacks and
postprocess observe the
same final objects.
Methods:
-
init–Create optimizer state from a model state and parameter vector.
-
minimize–Run optimization until the configured termination rule stops.
-
postprocess–Build the final solution object.
-
step–Advance the optimizer by one step.
-
terminate–Return whether optimization should stop and why.
init
¶
init[X](
problem: BaseProblem[X],
model_state: X,
params: Vector,
) -> S
Create optimizer state from a model state and parameter vector.
minimize
¶
minimize[X](
problem: BaseProblem[X],
model_state: X,
params: Vector,
) -> Solution[S, T]
Run optimization until the configured termination rule stops.
After every step, Problem.callback
is called when the concrete problem implements it. The callback receives
the current model state and the same mutable optimizer state that will be
stored on the returned Solution.
Parameters:
-
problem(BaseProblem[X]) –Optimization problem that supplies objective hooks.
-
model_state(X) –Initial model state used by objective hooks.
-
params(Vector) –Initial optimizer parameter vector.
Returns:
-
Solution[S, T]–The final solution.
Source code in src/liblaf/peach/optim/base/_optimizer.py
postprocess
¶
postprocess[X](
problem: BaseProblem[X],
model_state: X,
opt_state: S,
result: Result,
) -> Solution[S, T]
Build the final solution object.
Source code in src/liblaf/peach/optim/base/_optimizer.py
step
¶
step[X](
problem: BaseProblem[X], model_state: X, opt_state: S
) -> None
Advance the optimizer by one step.
Implementations should mutate model_state and opt_state with any
accepted parameter, gradient, or diagnostic updates.
Source code in src/liblaf/peach/optim/base/_optimizer.py
terminate
¶
terminate[X](
problem: BaseProblem[X], model_state: X, opt_state: S
) -> tuple[bool, Result]
Return whether optimization should stop and why.
Problem
¶
Bases: Protocol
flowchart TD
liblaf.peach.optim.base.Problem[Problem]
click liblaf.peach.optim.base.Problem href "" "liblaf.peach.optim.base.Problem"
Protocol implemented by differentiable optimization problems.
Optimizers evaluate objective derivatives on a model state. Drivers that
produce fresh parameter vectors use
update to mutate the model state
before asking for objective values or derivatives.
Methods:
-
callback–Run an optional side-effect after an optimizer step.
-
fun–Evaluate the scalar objective value.
-
grad–Evaluate the objective gradient.
-
hess_diag–Evaluate or approximate the Hessian diagonal.
-
hess_prod–Evaluate the Hessian-vector product along
p. -
hess_quad–Evaluate the quadratic form
p.T @ H @ p. -
max_step_size–Return a safe fraction of proposed trial displacement
p. -
update–Mutate model state for a line-search trial at
x. -
value_and_grad–Evaluate the objective and gradient together.
Result
¶
Bases: StrEnum
flowchart TD
liblaf.peach.optim.base.Result[Result]
click liblaf.peach.optim.base.Result href "" "liblaf.peach.optim.base.Result"
Result code returned by optimizers.
Attributes:
-
INTERRUPT– -
MAX_STEPS_REACHED– -
NAN– -
PRIMARY_SUCCESS– -
SECONDARY_SUCCESS– -
STAGNATION– -
SUCCESS– -
UNKNOWN_ERROR– -
success(bool) –Whether the result represents an accepted optimization outcome.