nemos.callbacks.TrainingContext#

class nemos.callbacks.TrainingContext(model=None, solver=None, params=None, state=None, aux=None, pass_idx=None, batch_idx=None, n_passes=0, frozen=None)[source]#

Bases: object

Mutable context object passed to callbacks during training.

One instance is created per training run. Fields are updated in-place by the training loop before each callback invocation.

Parameters:
  • model (Any) – The model being trained (e.g. GLM instance). Set by stochastic_fit.

  • solver (Any) – The solver instance running the optimization.

  • params (Any) – Current model parameters. Exposed as a read/write property: the training loop assigns the actively optimized subtree (ctx.params = ...), and reading ctx.params recombines the frozen subtree (see frozen) so callbacks always see the complete parameters.

  • state (Any) – Current solver state.

  • aux (Any) – Auxiliary output from the last batch.

  • pass_idx (int | None) – Current pass index (0-based).

  • batch_idx (int | None) – Current batch index within the pass.

  • n_passes (int) – Total number of passes requested.

  • frozen (Any) – Parameter subtree held fixed during optimization (e.g. a zero intercept when fit_intercept=False). The solver optimizes only the active subtree; this is recombined with it so callbacks always see the complete parameters. None when nothing is frozen.

Attributes

params

Current parameters, with the frozen subtree recombined into the active one.

should_stop

Whether a callback has requested early stopping.

stop_reason

Reason for the stop request, if any.

__init__(model=None, solver=None, params=None, state=None, aux=None, pass_idx=None, batch_idx=None, n_passes=0, frozen=None)[source]#
Parameters:

Methods

__init__([model, solver, params, state, ...])

request_stop([reason])

Request early stopping of the training loop.

to_summary()

Create a post-fit summary from the current training context.

__repr__(N_CHAR_MAX=700)[source]#

Represent this context as a string.

Simple string representation, similar to that of a dataclass.

Return type:

str

Parameters:

N_CHAR_MAX (int)

property params: Any#

Current parameters, with the frozen subtree recombined into the active one.

request_stop(reason='')[source]#

Request early stopping of the training loop.

Parameters:

reason (str) – Human-readable reason for stopping.

Return type:

None

property should_stop: bool#

Whether a callback has requested early stopping.

property stop_reason: str#

Reason for the stop request, if any.

to_summary()[source]#

Create a post-fit summary from the current training context.

Return type:

StochasticFitSummary