megnet.callbacks module

callbacks functions used in training process

class ManualStop[source]

Bases: keras.callbacks.Callback

Stop the training manually by putting a “STOP” file in the directory

on_batch_end(epoch: int, logs: Optional[Dict] = None) None[source]

Codes called at the end of a batch :param epoch: epoch id :type epoch: int :param logs: log dict :type logs: Dict

Returns: None

class ModelCheckpointMAE(filepath: str = './callback/val_mae_{epoch:05d}_{val_mae:.6f}.hdf5', monitor: str = 'val_mae', verbose: int = 0, save_best_only: bool = True, save_weights_only: bool = False, val_gen: Optional[keras.utils.data_utils.Sequence] = None, steps_per_val: Optional[int] = None, target_scaler: Optional[megnet.utils.preprocessing.Scaler] = None, period: int = 1, mode: str = 'auto')[source]

Bases: keras.callbacks.Callback

Save the best MAE model with target scaler

Parameters
  • filepath (string) – path to save the model file with format. For example weights.{epoch:02d}-{val_mae:.6f}.hdf5 will save the corresponding epoch and val_mae in the filename

  • monitor (string) – quantity to monitor, default to “val_mae”

  • verbose (int) – 0 for no training log, 1 for only epoch-level log and 2 for batch-level log

  • save_best_only (bool) – whether to save only the best model

  • save_weights_only (bool) – whether to save the weights only excluding model structure

  • val_gen (generator) – validation generator

  • steps_per_val (int) – steps per epoch for validation generator

  • target_scaler (object) – exposing inverse_transform method to scale the output

  • period (int) – number of epoch interval for this callback

  • mode – (string) choose from “min”, “max” or “auto”

on_epoch_end(epoch: int, logs: Optional[Dict] = None) None[source]

Codes called by the callback at the end of epoch :param epoch: epoch id :type epoch: int :param logs: logs of training :type logs: dict

Returns

None

class ReduceLRUponNan(filepath: str = './callback/val_mae_{epoch:05d}_{val_mae:.6f}.hdf5', factor: float = 0.5, verbose: bool = True, patience: int = 500, monitor: str = 'val_mae', mode: str = 'auto', has_sample_weights: bool = False)[source]

Bases: keras.callbacks.Callback

This callback function solves a problem that when doing regression, an nan loss may occur, or the loss suddenly shoot up. If such things happen, the model will reduce the learning rate and load the last best model during the training process. It has an extra function that patience for early stopping. This will move to indepedent callback in the future.

Parameters
  • filepath (str) – filepath for saved model checkpoint, should be consistent with checkpoint callback

  • factor (float) – a value < 1 for scaling the learning rate

  • verbose (bool) – whether to show the loading event

  • patience (int) – number of steps that the val mae does not change. It is a criteria for early stopping

  • monitor (str) – target metric to monitor

  • mode (str) – min, max or auto

  • has_sample_weights (bool) – whether the data has sample weights

on_epoch_end(epoch: int, logs: Optional[Dict] = None)[source]

Check the loss value at the end of an epoch :param epoch: epoch id :type epoch: int :param logs: log history :type logs: dict

Returns: None