2020-02-26 04:17:27 +00:00
|
|
|
from abc import ABC
|
2020-03-12 16:41:37 +00:00
|
|
|
from typing import Callable
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
from pytorch_lightning.callbacks import Callback
|
|
|
|
|
|
|
|
|
|
|
|
class TrainerCallbackHookMixin(ABC):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# this is just a summary on variables used in this abstract class,
|
|
|
|
# the proper values/initialisation should be done in child class
|
|
|
|
self.callbacks: list[Callback] = []
|
|
|
|
self.get_model: Callable = ...
|
|
|
|
|
2020-03-03 04:51:32 +00:00
|
|
|
def on_init_start(self):
|
|
|
|
"""Called when the trainer initialization begins, model has not yet been set."""
|
2020-02-26 04:17:27 +00:00
|
|
|
for callback in self.callbacks:
|
2020-03-03 04:51:32 +00:00
|
|
|
callback.on_init_start(self)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
2020-03-03 04:51:32 +00:00
|
|
|
def on_init_end(self):
|
|
|
|
"""Called when the trainer initialization ends, model has not yet been set."""
|
2020-02-26 04:17:27 +00:00
|
|
|
for callback in self.callbacks:
|
2020-03-03 04:51:32 +00:00
|
|
|
callback.on_init_end(self)
|
2020-02-26 04:17:27 +00:00
|
|
|
|
|
|
|
def on_epoch_start(self):
|
|
|
|
"""Called when the epoch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_epoch_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_epoch_end(self):
|
|
|
|
"""Called when the epoch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_epoch_end(self, self.get_model())
|
|
|
|
|
|
|
|
def on_train_start(self):
|
|
|
|
"""Called when the train begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_train_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_train_end(self):
|
|
|
|
"""Called when the train ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_train_end(self, self.get_model())
|
|
|
|
|
|
|
|
def on_batch_start(self):
|
|
|
|
"""Called when the training batch begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_batch_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_batch_end(self):
|
|
|
|
"""Called when the training batch ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_batch_end(self, self.get_model())
|
|
|
|
|
|
|
|
def on_validation_start(self):
|
|
|
|
"""Called when the validation loop begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_validation_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_validation_end(self):
|
|
|
|
"""Called when the validation loop ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_validation_end(self, self.get_model())
|
|
|
|
|
|
|
|
def on_test_start(self):
|
|
|
|
"""Called when the test begins."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_test_start(self, self.get_model())
|
|
|
|
|
|
|
|
def on_test_end(self):
|
|
|
|
"""Called when the test ends."""
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback.on_test_end(self, self.get_model())
|