OptimEngine#
- class liesel.optim.OptimEngine(loss, batches, optimizers, stopper, seed, initial_state, prune_history=True, show_progress=True, save_position_history=True, *, debug_nans=False, loss_monitor, progress_update_every=10, show_step_progress=False, step_progress_update_every=10)[source]#
Bases:
objectRuns an optimization loop over epochs, batches, and optimizers.
OptimEngineruns the fit configured byLieselOptim. Each epoch starts by askingbatchesfor fresh batch indices, then iterates over all full batches. For each batch, each optimizer gets a turn to update the subset of parameters named in itsposition_keys. The first active optimizer’s pre-update loss supplies the batch observation; if none is active, the loss is evaluated once at the unchanged position. At the end of the epoch, the engine records training and monitoring losses, updates the minimum-monitor position, and asksstopperwhether to continue.- Parameters:
batches (
Batches|BatchManager) – Batch configuration used for the training data. UseBatchesfor a single observation size andBatchManagerfor multi-branch models with different observation sizes.optimizers (
Sequence[OptimizerLike]) – Sequence of optimizers. Each optimizer must claim a disjoint set of position keys. Individual optimizers may delay activation withOptimizer.activate_after_epochs.stopper (
Stopper) – Early-stopping and maximum-epoch configuration.seed (
int|Array) – Integer seed or JAX PRNG key for batch shuffling or random batch sampling, and for losses or optimizers that usecarry.key. Starting parameters come fromloss.position(); the data split is already defined. Resuming a checkpoint uses its saved random key.initial_state (
Any) – Initial model state passed intoOptimCarry.prune_history (
bool, default:True) – IfTrue, remove unused history entries after early stopping.show_progress (
bool, default:True) – Whether to showtqdmprogress bars. This is the master switch for both epoch and optional batch progress.save_position_history (
bool, default:True) – Whether to store the full position history. The minimum-monitor position is tracked independently of this setting.loss_monitor (
EmaTrainLossMonitor|Literal['validation','train_full_data']) – Source for the epoch-level stopping and progress loss. PassEmaTrainLossMonitorfor a continuous EMA of pre-update losses,"validation"for one complete validation-loss evaluation after each epoch, or"train_full_data"for one complete training-loss evaluation after each epoch. Exact monitors are evaluated at the final post-update epoch position, and"train_full_data"incurs this additional full-data evaluation even when optimization itself uses one full-data batch.debug_nans (
bool, default:False) – Whether to capture first-NaN reproduction data during batch updates. Every active optimizer’s returned pre-update loss and the updated position are checked; when no optimizer is active, the explicit batched loss is checked.progress_update_every (
int, default:10) – Update the epoch progress bar after this many completed epochs. Defaults to 10. The final state is always rendered. When batch progress is active, the epoch bar advances after every epoch to keep the nested display consistent.show_step_progress (
bool, default:False) – Whether to show an additional progress bar for batches within each epoch whenshow_progressis enabled.step_progress_update_every (
int, default:10) – Update the batch progress bar after this many completed batches. Defaults to 10. The final state of an interrupted epoch is always rendered.
Notes
OptimEngineusescarry.epochas the number of completed epochs and as the next history index to be written. This matchesStopper’s indexing convention. Built-inLBFGSis accepted only with full-data batches and also requires a deterministic objective, which the engine cannot validate. Exact monitor minima retain the post-update position used for the evaluation. An EMA minimum instead retains the associated parameter snapshot; because an EMA combines losses from several positions, it is not an exact loss-position pairing.Examples
Start with
LieselOptimfor ordinary fits. To assemble the pieces yourself:>>> import jax.numpy as jnp >>> import optax >>> import tensorflow_probability.substrates.jax.distributions as tfd >>> import liesel.model as lsl >>> import liesel.optim as opt >>> loc = lsl.Var.new_param(jnp.array(0.0), name="loc") >>> y = lsl.Var.new_obs( ... jnp.array([1.0, 2.0, 3.0]), ... lsl.Dist(tfd.Normal, loc=loc, scale=1.0), ... name="y", ... ) >>> model = lsl.Model([y]) >>> split = opt.PositionSplit.from_model(model) >>> engine = opt.OptimEngine( ... loss=opt.NegLogProbLoss(model, split, scale=True), ... batches=opt.Batches.from_split(split, batch_size=None), ... optimizers=[opt.Optimizer(["loc"], optax.adam(0.01))], ... stopper=opt.Stopper(epochs=5, patience=5), ... initial_state=model.state, ... loss_monitor="train_full_data", ... seed=42, ... show_progress=False, ... ) >>> result = engine.fit() >>> result.n_epochs 5
Methods
fit(*[, checkpoint, pause_after, ...])Runs optimization and returns processed results.
Attributes
Whether to capture first-NaN reproduction data during batch updates.
Position keys claimed by all optimizers.
Update the epoch progress bar after this many completed epochs.
If
True, remove unused history entries after early stopping.Whether to store the full position history.
Whether to show
tqdmprogress bars.Whether to show an additional progress bar for batches within each epoch when
show_progressis enabled.Train/validation/test split supplied by
loss.Update the batch progress bar after this many completed batches.
Loss object implementing the
Lossprotocol.Batch configuration used for the training data.
Sequence of optimizers.
Early-stopping and maximum-epoch configuration.
Integer seed or JAX PRNG key for batch shuffling or random batch sampling, and for losses or optimizers that use
carry.key.Initial model state passed into
OptimCarry.Source for the epoch-level stopping and progress loss.