EarlyStopping#

class braintools.trainer.EarlyStopping(monitor='val_loss', mode='min', patience=3, min_delta=0.0, verbose=False, strict=True, check_finite=True)#

Stop training when a monitored metric has stopped improving.

Parameters:
  • monitor (str) – Metric to monitor.

  • mode (str) – One of ‘min’ or ‘max’. In ‘min’ mode, training stops when the quantity monitored has stopped decreasing.

  • patience (int) – Number of epochs with no improvement after which training will be stopped.

  • min_delta (float) – Minimum change to qualify as an improvement.

  • verbose (bool) – Whether to print early stopping messages.

  • strict (bool) – If True, raise error if monitor metric not found.

  • check_finite (bool) – Stop training if the metric becomes NaN or infinite.

Examples

With a large min_delta no epoch counts as an improvement, so training stops once patience epochs have passed without one:

>>> import numpy as np
>>> import jax.numpy as jnp
>>> import brainstate
>>> import braintools
>>> class Regressor(braintools.trainer.LightningModule):
...     def __init__(self):
...         super().__init__()
...         self.linear = brainstate.nn.Linear(2, 1)
...     def training_step(self, batch, batch_idx):
...         x, y = batch
...         loss = jnp.mean((self.linear(x) - y) ** 2)
...         self.log('loss', loss, prog_bar=True)
...         return {'loss': loss}
...     def validation_step(self, batch, batch_idx):
...         x, y = batch
...         return {'loss': jnp.mean((self.linear(x) - y) ** 2)}
...     def configure_optimizers(self):
...         return braintools.optim.Adam(lr=0.1)
>>> rng = np.random.default_rng(0)
>>> X = rng.normal(size=(64, 2)).astype(np.float32)
>>> y = X @ np.array([[1.0], [-2.0]], dtype=np.float32) + 0.5
>>> loader = braintools.trainer.DataLoader((X, y), batch_size=32)
>>> brainstate.random.seed(0)
>>> model = Regressor()
>>> early_stop = braintools.trainer.EarlyStopping(
...     monitor='val_loss',
...     patience=2,
...     mode='min',
...     min_delta=100.0,
... )
>>> trainer = braintools.trainer.Trainer(
...     max_epochs=50, callbacks=[early_stop],
...     logger=False, enable_progress_bar=False, enable_checkpointing=False)
>>> trainer.fit(model, loader, loader)
>>> early_stop.should_stop, early_stop.stopped_epoch, trainer.current_epoch
(True, 2, 2)
load_state_dict(state_dict)[source]#

Load state from state dict.

on_validation_epoch_end(trainer, module)[source]#

Check if training should stop.

property should_stop: bool#

Whether training should stop.

state_dict()[source]#

Return state dict for checkpointing.

Return type:

Dict[str, Any]