snn.trainer¶
Checkpoint¶
- class snn.trainer.Checkpoint(monitor='val_loss', mode='auto', save_path='checkpoint', verbose=1)[source]¶
Bases:
objectSaves and restores model weights when a monitored metric improves.
- Parameters:
monitor (str) – Name of the metric to watch (e.g.
'val_loss','val_accuracy').mode (str) –
'min'for loss-like metrics,'max'for accuracy-like metrics,'auto'to infer from the metric name.save_path (str, optional) – Path prefix for the
.npzfile. Defaults to'checkpoint'.verbose (int) – Verbosity level (0 = silent, 1 = print on save).
Trainer¶
- class snn.trainer.Trainer(model, gradient_accumulation_steps=1, mixed_precision=False, clip_grad_norm=None, checkpoints=None, verbose=1)[source]¶
Bases:
objectAdvanced training loop for a
Sequentialmodel.Extends the built-in
model.fit()with:Gradient accumulation — accumulate gradients over N micro-batches before a weight update. Useful for effective large batch sizes when memory is limited.
Mixed-precision simulation — casts activations/gradients to
float16and immediately back tofloat64during the forward/ backward pass, simulating the precision loss of hardware FP16 without requiring actual hardware support.Multi-metric checkpointing — track any number of metrics and save the best weights for each independently.
Callbacks — plug in
EarlyStopping,ReduceLROnPlateau, or any callable(epoch, history, model, optimizer) → None.
- Parameters:
model (Sequential) – Compiled
Sequentialmodel.gradient_accumulation_steps (int) – Number of micro-batches to accumulate before calling
optimizer.apply_gradients. Effective batch size =batch_size × gradient_accumulation_steps.mixed_precision (bool) – Simulate FP16 mixed precision.
clip_grad_norm (float or None) – If set, clip the global gradient norm to this value before each optimizer step.
checkpoints (list[Checkpoint] or None) – One or more
Checkpointobjects. Each monitors its own metric.verbose (int) – 0 = silent, 1 = one line per epoch, 2 = one line per step.
Examples
>>> from snn.trainer import Trainer, Checkpoint >>> ckpt = Checkpoint(monitor='val_loss', save_path='best_model') >>> trainer = Trainer( ... model, ... gradient_accumulation_steps=4, ... mixed_precision=True, ... clip_grad_norm=1.0, ... checkpoints=[ckpt], ... ) >>> history = trainer.fit( ... X_train, y_train, ... epochs=50, ... batch_size=32, ... validation_data=(X_val, y_val), ... ) >>> ckpt.restore(model) # reload best weights
- fit(x, y, epochs=10, batch_size=32, validation_data=None, shuffle=True, callbacks=None)[source]¶
Train the model.
- Parameters:
x (ndarray) – Input features, shape
(N, ...).y (ndarray) – Targets, shape
(N, ...).epochs (int) – Total number of passes over the dataset.
batch_size (int) – Micro-batch size (before accumulation).
validation_data (tuple (x_val, y_val), optional) – If provided, compute validation metrics each epoch.
shuffle (bool) – Shuffle training data each epoch.
callbacks (list, optional) – List of callables invoked as
cb(epoch, history, model, optimizer).
- Returns:
Training history.
- Return type:
dict