accelmo.tracking.sgd.loop

The mini-batch gradient-descent loop of the tracking fit.

accelmo.tracking.sgd.loop.PT_SEED = 1e-06

Starting value for a pt knob that starts at exactly zero.

class accelmo.tracking.sgd.loop.EpochState(epoch, knobs, loss, grad_norm, lr, started, true_diff=0.0, validation_loss=None, new_best=False, note=None)[source]

Bases: object

What one epoch produced: the knobs after it and the numbers logged for it.

Parameters:
  • epoch (int)

  • knobs (dict[str, float])

  • loss (float)

  • grad_norm (float)

  • lr (float)

  • started (float)

  • true_diff (float)

  • validation_loss (float | None)

  • new_best (bool)

  • note (str | None)

epoch: int
knobs: dict[str, float]
loss: float

Training chi-square per weighted point.

grad_norm: float
lr: float
started: float
true_diff: float = 0.0

Distance of knobs from the true strengths (see SGDLoop._true_diff()).

validation_loss: float | None = None
new_best: bool = False
note: str | None = None

Whatever the epoch-end hook returned, appended to the log line.

property selection_loss: float

validation when available.

Type:

The loss best-selection and early stopping use

class accelmo.tracking.sgd.loop.SGDLoop(knob_names, true_strengths, config, num_batches)[source]

Bases: object

Runs epochs of mini-batch gradient descent over the training workers.

Each epoch sends every batch to the workers, steps the optimiser after each one, then validates, picks the best knobs so far and checks the stopping rules. best holds the result; diagnostics how the run ended.

Parameters:
  • knob_names (list[str])

  • true_strengths (dict[str, float])

  • config (OptimiserConfig)

  • num_batches (int)

gain_names: list[str]
best: BestTracker[dict[str, float]]

Best knobs so far and their selection loss; empty until the first epoch.

diagnostics: FitDiagnostics | None

How the last run() ended.

run(knobs, channels, *, n_points, run_log=None, dashboard=None, checkpoint_config=None, validation_loss_fn=None, loss_callback=None, epoch_end_hook=None, gain_names=None, gain_ridge=0.0)[source]

Optimise from knobs and return the best knobs found.

The loss is the training chi-square per weighted point (n_points). validation_loss_fn scores the knobs after each epoch on held-out data; epoch_end_hook runs after best-selection and returns a note for the log line. gain_names are BPM gains carried in the knobs but not stepped by the optimiser: after every batch each is set to its least-squares value given the current magnets, Σ w m (y - m) / (Σ w m² + gain_ridge / num_batches), from the sums the workers return; gain_ridge is the prior’s 1/σ² in the same units. dashboard is redrawn after every epoch.

Return type:

dict[str, float]

Parameters:
  • knobs (dict[str, float])

  • channels (WorkerChannels)

  • n_points (int)

  • run_log (RunLog | None)

  • dashboard (FitDashboard | None)

  • checkpoint_config (CheckpointConfig | None)

  • validation_loss_fn (Callable[[dict[str, float]], float | None] | None)

  • loss_callback (Callable[[int, float, float | None, float, float], None] | None)

  • epoch_end_hook (Callable[[dict[str, float], dict[str, float]], str | None] | None)

  • gain_names (list[str] | None)

  • gain_ridge (float)