LieselOptim#
- class liesel.optim.LieselOptim(model, *, loss_monitor, optimizers, stopper=None, seed=0, split=None, batch_size=None, batches=None, loss=None, validation_strategy='log_lik', scale_loss=True, save_position_history=True, show_progress=True, show_step_progress=False, progress_update_every=10, step_progress_update_every=10)[source]#
Bases:
objectBuilds an
OptimEnginefor a Liesel model using sensible defaults.LieselOptimis the quick-start wrapper for regular model optimization. It creates a negative log-posterior loss, full-data training batches, and wraps a supplied Optax transformation over all model parameters. Callbuild_engine()to inspect or modify the low-level engine before fitting, orfit()for the direct path.- Parameters:
model (
Model) – Liesel model to optimize.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 use the post-update epoch position."train_full_data"always adds the full-data evaluation, including with one full-data optimization batch.optimizers (
GradientTransformation|Sequence[OptimizerLike] |Literal['lbfgs']) – Required optimizer choice. An Optax transformation such asoptax.adam(0.01)applies to all model parameters. Pass"lbfgs"for the built-in full-data L-BFGS optimizer, or a sequence of explicit optimizers for selected parameters. If a parameter is weak (computed), automatic selection raises an error. Explicitly name its strong source variables withOptimizerorLBFGS; those sources need not be marked as parameters. Priors on weak parameters remain in the loss. Pass a configured transformation, not an optimizer factory. Transformations must support updates from gradients, state and parameters without extra objective arguments.stopper (
Stopper|None, default:None) – Maximum-epoch and early-stopping configuration.Nonecreates a newStopperwithepochs=1000,patience=10, andrtol=1e-6.seed (
int|None, default:0) – Integer seed for batch shuffling or random batch sampling, and for custom losses or optimizers that usecarry.key. Starting parameter values come frommodel. Set the seed for a random training/validation/test split separately when creating that split. Defaults to0. ExplicitNoneuses the current Unix time in whole seconds.split (
PositionSplit|PositionSplitManager|None, default:None) – Optional split. If neithersplitnorlossis supplied, all observed data is used for training; weak observations are recomputed from their strong inputs. Observed arrays with different axis lengths require an explicit split. Check their row alignment before opting in withsplit=PositionSplit.from_model(model, multi_size="manager"). Use nestedposition_keysto specify independent groups andsplit_axes={key: None}for shared values. With a custom loss, the split must be the same object asloss.split. Models withper_obs=Falserequire an explicitly constructed split: usePositionSplit.from_modelwithinfer_sample_sizes=Falsefor axis counts, or supply effectivesample_sizesthere. Custom aggregate likelihood or probability nodes require a custom loss as well as an explicit split.batch_size (
int|None, default:None) – Rows per batch in each training group. UsesBatches.from_splitwith otherwise default settings.Noneuses all training data. Passbatchesinstead for custom settings, such as non-leading axes.batches (
Batches|BatchManager|None, default:None) – Explicit batch configuration. Cannot be combined with a non-Nonebatch_size.loss (
Loss|None, default:None) – Custom loss. Usesloss.splitand overridesvalidation_strategyandscale_loss.validation_strategy (
Literal['log_lik','log_prob'], default:'log_lik') – Validation strategy passed toNegLogProbLosswhenlossis not supplied.scale_loss (
bool, default:True) – Whether the defaultNegLogProbLossshould divide losses by the training sample size. Defaults toTrue. This setting has no effect whenlossis supplied.save_position_history (
bool, default:True) – Whether to save parameter values at every epoch. Defaults toTrue. History is allocated for the maximum epoch budget before fitting: its size is approximately epochs times the total parameter bytes (4 GB for 1,000 epochs and one million float32 parameters). SetFalseto skip this allocation; losses and final/best positions remain available.show_progress (
bool, default:True) – Whether the built engine should showtqdmprogress bars.show_step_progress (
bool, default:False) – Whether to show an additional progress bar for batches within each epoch whenshow_progressis enabled.progress_update_every (
int, default:10) – Update the epoch progress bar after this many completed epochs. When batch progress is active, the epoch bar advances after every epoch.step_progress_update_every (
int, default:10) – Update the batch progress bar after this many completed batches.
Examples
>>> import jax.numpy as jnp >>> import optax >>> import liesel.model as lsl >>> import tensorflow_probability.substrates.jax.distributions as tfd >>> from liesel.optim import EmaTrainLossMonitor, LieselOptim >>> loc = lsl.Var.new_param(jnp.array(0.0), name="loc") >>> y = lsl.Var.new_obs( ... jnp.array([0.0, 1.0]), ... lsl.Dist(tfd.Normal, loc=loc, scale=1.0), ... name="y", ... ) >>> model = lsl.Model([y]) >>> engine = LieselOptim( ... model, ... optimizers=optax.adam(0.01), ... loss_monitor=EmaTrainLossMonitor(effective_window=2.0), ... seed=1, ... ).build_engine() >>> type(engine).__name__ 'OptimEngine'
Methods
Builds the low-level optimization engine.
fit()Builds an engine and runs optimization immediately.
Attributes
Explicit batch configuration.
Custom loss.
Source for the epoch-level stopping and progress loss.
Liesel model to optimize.
Required optimizer choice.
Update the epoch progress bar after this many completed epochs.
Whether to save parameter values at every epoch.
Integer seed for batch shuffling or random batch sampling, and for custom losses or optimizers that use
carry.key.Whether the built engine should show
tqdmprogress bars.Whether to show an additional progress bar for batches within each epoch when
show_progressis enabled.Optional split.
Update the batch progress bar after this many completed batches.
Maximum-epoch and early-stopping configuration.