Training

See Continuous Learning for the loop structure and the config keys that drive it.

Continuous trainer

class apeiron.training.continuous_trainer.ContinuousTrainer[source]

Bases: object

Trainer for continuous/continual learning with drift handling.

__init__(cfg, modelHarness, logger, profiler)[source]

Initialize the continuous trainer with config, model, logger, and profiler.

Parameters:
Return type:

None

compute_bwt(metric_index=0)[source]

Backward transfer for the task that just finished adapting.

BWT = (1/(T-1)) * sum_{i<T} ( R[T][i] - R[i][i] )

where a task is one drift event, R[T][i] is the current model’s score on task i’s validation split and R[i][i] is the score on that same split recorded right after adapting to it. It therefore compares one task across two model states – the definition of forgetting – rather than comparing different tasks at one state.

Sign follows the raw metric, so the reading depends on the metric’s direction: with a higher-is-better metric (accuracy) negative means forgetting, while with a lower-is-better one (SLAC-FEL’s MAE) positive means forgetting.

Parameters:

metric_index (int) – which entry of eval_metrics to use, matching the index behind test_curr_acc/test_hist_acc.

Returns:

BWT over the retained past tasks, or None before any task has been registered (the first drift event, where the sum is empty).

Return type:

Optional[float]

outer_cl_training_loop(drift_event_id=0)[source]

Run the outer continuous learning training loop for a drift event.

Parameters:

drift_event_id (int)

Return type:

int

inner_cl_training_loop(iter_count, cur_train_loader, train_iter, hist_train_loader=None, hist_train_iter=None)[source]

Run a single inner training iteration with forward/backward and optimizer step.

Returns the training iterators alongside the losses. They are where the step left off, which is not where it started: a loader that runs out is replaced here, and a caller that keeps the old one would restart it on every later step.

Parameters:
  • iter_count (int)

  • cur_train_loader (torch.utils.data.DataLoader)

  • train_iter (Iterator)

  • hist_train_loader (torch.utils.data.DataLoader | None)

  • hist_train_iter (Iterator | None)

Return type:

tuple[float, float, Iterator, Iterator | None]

Updater factory

apeiron.training.updater.create_updater.create_updater(cfg, modelHarness)[source]

Create an updater instance based on the configuration.

Parameters:
  • cfg (Config) – Configuration object with training settings.

  • modelHarness (BaseModelHarness) – Model harness containing model and data loaders.

Returns:

An instance of the specified updater class.

Raises:

NotImplementedError – If the specified updater mode is not implemented.

Return type:

BaseUpdater

Updaters

class apeiron.training.updater.base.BaseUpdater[source]

Bases: object

Base class for model updaters in continual learning.

Defines the interface for gradient-based updates during training. Subclasses implement strategies like JVP or K-FAC regularization.

cfg

Configuration object.

criterion

Loss function for training.

model

Neural network model to update.

__init__(cfg, modelHarness)[source]

Initialize updater with config and model harness.

Parameters:
Return type:

None

fwd_bwd(batch, hist_batch=None)[source]

Perform forward and backward pass.

When continual_learning.mix_historic_data is enabled and a historical batch is supplied, half of each stream is taken so the total sample count per step stays the same as in the un-mixed case. If the historical batch is too small to supply its half, the remainder is taken from the current batch rather than shrinking the step.

Parameters:
  • batch (tuple[torch.Tensor, torch.Tensor])

  • hist_batch (tuple[torch.Tensor, torch.Tensor] | None)

Return type:

float

cl_preprocessing()

Hook called before before the training loop starts

Return type:

None

cl_postprocessing()

Hook called after the training loop ends

Return type:

None

update_pre_fwd_bwd()

Hook called before gradient computation.

Return type:

None

update_post_fwd_bwd()

Hook called after gradient computation, but before the optimizer. Returns regularization loss.

Return type:

float

update_post_optimizer_call()

Hook called after optimizer step to update internal state.

Return type:

None

First-order SAM/Bertsimas robust updater for continual learning.

Despite the historical jvp_reg name (and update_mode = "jvp_reg"), this updater implements a first-order Sharpness-Aware / Bertsimas robust step rather than the original Jacobian-Vector-Product regularizer.

The cost combines the current and historical batches into ONE loss (half/half, like base replay), and the robustness is the gradient of that combined loss at a parameter-perturbed point – first-order only (no Hessian, no third order):

grad = grad_theta L_mix( theta + rho_theta * u_new ; X_mix ) X_mix = concat( X_cur[:n_cur], X_hist[:n_hist] + rho_x * dx_hat ) (n_hist = n//2) u_new = -grad L_cur / ||grad L_cur|| (param direction -> new task) dx_hat = unit( mean(X_cur) - mean(X_hist) ) (batch-mean drift direction)

Keeping the current batch inside the loss every step anchors the online accuracy so it cannot degrade. rho_x = 0 reduces this to param-SAM only. Radii come from [continual_learning]: jvp_rho_theta, jvp_rho_x, jvp_data_sign.

Like the previous JVP updater, this manages the historical batch itself, so it is independent of the mix_historic_data replay flag.

class apeiron.training.updater.jvp_reg.JVPRegUpdater[source]

Bases: BaseUpdater

SAM-based robust updater on a combined current+historical batch.

__init__(cfg, modelHarness)[source]

Initialize the SAM-based updater with config and model harness.

Parameters:
Return type:

None

fwd_bwd(batch, hist_batch=None)[source]

Forward/backward for a single first-order SAM robust step.

With no historical batch this is a plain current-batch step. Otherwise it combines both batches into one loss and back-propagates that loss at a parameter-perturbed point (SAM). Parameters are always restored.

Parameters:
  • batch (tuple[torch.Tensor, torch.Tensor])

  • hist_batch (tuple[torch.Tensor, torch.Tensor] | None)

Return type:

float

update_post_fwd_bwd()

Return the accumulated combined-loss value and reset it.

Return type:

float

class apeiron.training.updater.ewc.OnlineEWCUpdater[source]

Bases: BaseUpdater

Online Elastic Weight Consolidation (EWC) updater. See work by Title: Overcoming catastrophic forgetting in neural networks Authors: James Kirkpatrick, Razvan Pascanu, Neil Rabinowitz, Joel Veness, Guillaume Desjardins, Andrei A. Rusu, Kieran Milan, John Quan, Tiago Ramalho, Agnieszka Grabska-Barwińska, Demis Hassabis, Claudia Clopath, Dharshan Kumaran, Raia Hadsell DOI: https://doi.org/10.1073/pnas.1611835114

  • Anchor (theta_star) and Fisher are held fixed during a CL loop.

  • Fisher is accumulated from gradients during CL.

  • Anchor and Fisher are updated exactly once in cl_postprocessing().

All updater tensors are explicitly created on cfg.device.

__init__(cfg, modelHarness)[source]

Initialize updater with config and model harness.

Parameters:
Return type:

None

cl_preprocessing()

Called once before the CL loop starts.

Return type:

None

cl_postprocessing()

Called once after the CL loop finishes.

Commits the new prior:

F* <- fisher_decay * F* + F_cl_avg θ* <- θ_final

Return type:

None

update_pre_fwd_bwd()

Prepare per-step Fisher accumulator.

Return type:

None

update_post_fwd_bwd()

Called after backward(), before optimizer.step().

  • Adds EWC gradient using fixed anchor and Fisher.

  • Accumulates per-step Fisher estimate from gradients.

Return type:

float

update_post_optimizer_call()

After optimizer.step(): - accumulate Fisher for this CL event - do NOT update anchor or running Fisher yet

Return type:

None

class apeiron.training.updater.kfac.OnlineKFACUpdater[source]

Bases: BaseUpdater

Online K-FAC EWC-style regularizer. See work by Title: Optimizing neural networks with Kronecker-factored approximate curvature Authors: James Martens, Roger Grosse Link: http://proceedings.mlr.press/v37/martens15.html

  • Accumulates Kronecker factors during a CL loop

  • Commits them once at cl_postprocessing()

  • Applies KFAC-structured EWC gradient penalty

__init__(cfg, modelHarness)[source]

Initialize updater with config and model harness.

Parameters:
Return type:

None

cl_preprocessing()
cl_postprocessing()
update_post_fwd_bwd()
Return type:

float

update_post_optimizer_call()
class apeiron.training.updater.no_updater.NoUpdater[source]

Bases: BaseUpdater

No-op updater that skips training updates.

Used when continuous learning updates should be disabled.

fwd_bwd(batch, hist_batch=None)[source]

Skip forward/backward pass, return sentinel value.

Parameters:
  • batch (tuple[torch.Tensor, torch.Tensor])

  • hist_batch (tuple[torch.Tensor, torch.Tensor] | None)

Return type:

float