fl4health.utils.early_stopper module

class EarlyStopper(client, train_loop_checkpoint_dir, patience=1, interval_steps=5)[source]

Bases: object

__init__(client, train_loop_checkpoint_dir, patience=1, interval_steps=5)[source]

Early stopping class is a plugin for the client that allows to stop local training based on the validation loss. At each training step this class saves the best state of the client and restores it if the client is stopped. If the client starts to overfit, the early stopper will stop the training process and restore the best state of the client before sending the model to the server.

Parameters:
  • client (BasicClient) – The client to be monitored.

  • train_loop_checkpoint_dir (Path) – Directory to checkpoint the “best” state seen so far.

  • patience (int, optional) – Number of validation cycles to wait before stopping the training. If it is equal to None client never stops, but still loads the best state before sending the model to the server. Defaults to 1.

  • interval_steps (int) – Specifies the frequency, in terms of training intervals, at which the early stopping mechanism should evaluate the validation loss. Defaults to 5.

load_snapshot(attributes=None)[source]

Load the best snapshot of the client state from the checkpoint directory.

Parameters:

attributes (list[str] | None, optional) – List of attributes to load from the checkpoint. If None, all attributes as defined in state_checkpointer are loaded. Defaults to None.

Return type:

None

should_stop(steps)[source]

Determine if the client should stop training based on early stopping criteria.

Parameters:

steps (int) – Number of steps since the start of the training.

Returns:

True if training should stop, otherwise False.

Return type:

bool