Source code for torchmate.callbacks.callback

[docs] 1class Callback: 2 """Base class for creating callback objects in an experimental or training framework. 3 4 Callbacks are used to customize and extend the behavior of an experiment, training loop, 5 or optimization process by hooking into various stages of the execution. 6 7 **Callback Methods:** 8 9 ``on_experiment_begin(self, trainer: Trainer) -> None`` 10 Called at the beginning of an experiment. 11 12 ``on_experiment_end(self, trainer: Trainer) -> None`` 13 Called at the end of an experiment. 14 15 16 ``on_epoch_begin(self, trainer: Trainer) -> None`` 17 Called at the beginning of an epoch. 18 19 ``on_epoch_begin(self, trainer: Trainer) -> None`` 20 Called at the end of an epoch. 21 22 ``on_(train|val|predict)_begin(self, trainer: Trainer) -> None`` 23 Called at the beginning of fit/evaluate/predict. 24 25 ``on_(train|val|predict)_end(self, trainer: Trainer) -> None`` 26 Called at the end of fit/evaluate/predict. 27 28 ``on_(train|val|predict)_batch_begin(self, trainer: Trainer) -> None`` 29 Called right before processing a batch during training/validating/predicting. 30 31 ``on_(train|val|predict)_batch_end(self, trainer: Trainer) -> None`` 32 Called at the end of training/validating/predicting a batch. 33 34 Parameters: 35 trainer (Trainer) - An instance of (torchmate.trainer.Trainer) class. 36 37 38 Note: 39 This base class provides empty implementations for all callback methods, allowing derived callback classes 40 to selectively override only the methods that need to be customized. 41 42 43 **Example Usage:** 44 Below is an example of a custom callback class that inherits from Callback and overrides 45 specific methods to customize behavior during training: 46 47 .. code-block:: python 48 49 class CustomCallback(Callback): 50 def __init__(self): 51 self.current_epoch = 0 52 53 def on_epoch_begin(self, trainer): 54 self.current_epoch +=1 55 print(f"Epoch {self.current_epoch} begins!") 56 57 def on_epoch_end(self, trainer): 58 print(f"Epoch {self.current_epoch} has finished!") 59 60 def on_experiment_end(self, trainer): 61 print("Experiment finished!") 62 print(f"History: {trainer.history}") 63 64 # Create an instance of the custom callback and use it during training 65 custom_callback = CustomCallback() 66 67 68 69 """ 70
[docs] 71 def on_experiment_begin(self, trainer) -> None: 72 pass
73
[docs] 74 def on_experiment_end(self, trainer) -> None: 75 pass
76
[docs] 77 def on_epoch_begin(self, trainer) -> None: 78 pass
79
[docs] 80 def on_epoch_end(self, trainer) -> None: 81 pass
82
[docs] 83 def on_train_begin(self, trainer) -> None: 84 pass
85
[docs] 86 def on_train_end(self, trainer) -> None: 87 pass
88
[docs] 89 def on_train_batch_begin(self, trainer) -> None: 90 pass
91
[docs] 92 def on_train_batch_end(self, trainer) -> None: 93 pass
94
[docs] 95 def on_val_begin(self, trainer) -> None: 96 pass
97
[docs] 98 def on_val_end(self, trainer) -> None: 99 pass
100
[docs] 101 def on_val_batch_begin(self, trainer) -> None: 102 pass
103
[docs] 104 def on_val_batch_end(self, trainer) -> None: 105 pass
106
[docs] 107 def on_predict_begin(self, trainer) -> None: 108 pass
109
[docs] 110 def on_predict_end(self, trainer) -> None: 111 pass
112
[docs] 113 def on_predict_batch_begin(self, trainer) -> None: 114 pass
115
[docs] 116 def on_predict_batch_end(self, trainer) -> None: 117 pass
118
[docs] 119 def on_backward_end(self, trainer) -> None: 120 pass