Callbacks

Base

class DLL.DeepLearning.Callbacks.Callback[source]

Bases: ABC

on_batch_end(epoch)[source]

A method, which is automatically called after every batch.

Parameters:

epoch (int) – The current epoch.

on_epoch_end(epoch, metrics)[source]

A method, which is automatically called after every epoch.

Parameters:
  • epoch (int) – The current epoch.

  • metrics (dict[str, float]) – The values of the chosen metrics of the last epoch.

on_train_end()[source]

A method, which is automatically called after training.

on_train_start()[source]

A method, which is automatically called before the training starts.

set_model(model)[source]

Lets the callback know about the chosen model. Is automatically called when using Model.fit()

Parameters:

Model (Models) – The chosen model.

EarlyStopping

class DLL.DeepLearning.Callbacks.EarlyStopping(monitor='val_loss', patience=1, mode='min', restore_best_model=False, warmup_length=0, verbose=False)[source]

Bases: Callback

The early stopping callback. Stops the training of the model early if no improvement is made.

Parameters:
  • monitor (str, optional) – The quantity, which is monitored for the early stop. Must be in the metrics of the model. Defaults to “val_loss”.

  • patience (int, optional) – The number of epochs the model waits without improvement. Must be a positive integer. Defaults to 1.

  • mode (str, optional) – Determines if the monitored metric better if it is maximized or minimized. Must be in [“min”, “max”]. Defaults to “min”.

  • restore_best_model (bool, optional) – Determines if the best model is restored after the training is finished. Defaults to False.

  • warmup_length (int, optional) – Determines how many epochs are trained no matter what. Must be non-negative. Defaults to 0.

  • verbose (bool, optional) – Determines if information about the callbacks is printed. Must be a boolean. Defaults to False.

Note

Using restore_best_model = True considerably slows down training as the best model must be saved every epoch the model improves.

on_epoch_end(epoch, metrics)[source]

Calculates if the model has improved on the last epoch.

Parameters:
  • epoch (int) – The current epoch.

  • metrics (dict[str, float]) – The values of the chosen metrics of the last epoch.

Raises:

NotCompiledError – callback.set_model must be called before the training starts

on_train_end()[source]

Resets the model to the stored model if restore_best_model was chosen.

Raises:

NotCompiledError – callback.set_model must be called before the training starts

on_train_start()[source]

Sets the needed attributes.

Raises:

NotCompiledError – callback.set_model must be called before the training starts

set_model(model)[source]

Lets the callback know about the chosen model. Is automatically called when using Model.fit()

Parameters:

Model (Models) – The chosen model.

BackUp

class DLL.DeepLearning.Callbacks.BackUp(filepath='./model.pkl', frequency=1, on_batch=False, verbose=False)[source]

Bases: Callback

Saves the fitted model to the spesified filepath.

Parameters:
  • filepath (str | , optional) – The filepath where the model is saved. Defaults to “./model.pkl”.

  • frequency (int, optional) – The frequency of saving the model. Must be a positive integer. Defaults to 1.

  • on_batch (bool, optional) – Determines if a model is saved every frequency batch or epoch. Defaults to False.

  • verbose (bool, optional) – Determines if information about the callbacks is printed. Must be a boolean. Defaults to False.

on_batch_end(epoch)[source]

Calculates if the model should be backed up on this batch.

Parameters:

epoch (int) – The current epoch.

Raises:

NotCompiledError – callback.set_model must be called before the training starts

on_epoch_end(epoch, metrics)[source]

Calculates if the model should be backed up on this epoch.

Parameters:
  • epoch (int) – The current epoch.

  • metrics (dict[str, float]) – The values of the chosen metrics of the last epoch.

Raises:

NotCompiledError – callback.set_model must be called before the training starts

ReduceLROnPlateau

class DLL.DeepLearning.Callbacks.ReduceLROnPlateau(monitor='val_loss', patience=0, mode='min', warmup_length=0, factor=0.5, verbose=False)[source]

Bases: Callback

The reduce learning rate on plateau callback. Reduces the learning rate of the model optimiser if no improvement is made.

Parameters:
  • monitor (str, optional) – The quantity, which is monitored. Must be in the metrics of the model. Defaults to “val_loss”.

  • patience (int, optional) – The number of epochs the model waits without improvement. Must be a positive integer. Defaults to 0.

  • mode (str, optional) – Determines if the monitored metric better if it is maximized or minimized. Must be in [“min”, “max”]. Defaults to “min”.

  • warmup_length (int, optional) – Determines how many epochs are trained on the original learning rate. Must be non-negative. Defaults to 0.

  • factor (float, optional) – The learning rate is multiplied by this factor when training. Must be in range (0, 1). Defaults to 0.5.

  • verbose (bool, optional) – Determines if information about the callbacks is printed. Must be a boolean. Defaults to False.

on_epoch_end(epoch, metrics)[source]

Calculates if the learning rate should be removed.

Parameters:
  • epoch (int) – The current epoch.

  • metrics (dict[str, float]) – The values of the chosen metrics of the last epoch.

Raises:

NotCompiledError – callback.set_model must be called before the training starts

on_train_start()[source]

Initializes the needed attributes.

Raises:

NotCompiledError – callback.set_model must be called before the training starts

set_model(model)[source]

Lets the callback know about the chosen model. Is automatically called when using Model.fit()

Parameters:

Model (Models) – The chosen model.