summaryrefslogtreecommitdiff
path: root/src/training/callbacks
diff options
context:
space:
mode:
Diffstat (limited to 'src/training/callbacks')
-rw-r--r--src/training/callbacks/__init__.py1
-rw-r--r--src/training/callbacks/base.py101
-rw-r--r--src/training/callbacks/early_stopping.py1
3 files changed, 103 insertions, 0 deletions
diff --git a/src/training/callbacks/__init__.py b/src/training/callbacks/__init__.py
new file mode 100644
index 0000000..868d739
--- /dev/null
+++ b/src/training/callbacks/__init__.py
@@ -0,0 +1 @@
+"""TBC."""
diff --git a/src/training/callbacks/base.py b/src/training/callbacks/base.py
new file mode 100644
index 0000000..d80a1e5
--- /dev/null
+++ b/src/training/callbacks/base.py
@@ -0,0 +1,101 @@
+"""Metaclass for callback functions."""
+
+from abc import ABC
+from typing import Callable, List, Type
+
+
+class Callback(ABC):
+ """Metaclass for callbacks used in training."""
+
+ def on_fit_begin(self) -> None:
+ """Called when fit begins."""
+ pass
+
+ def on_fit_end(self) -> None:
+ """Called when fit ends."""
+ pass
+
+ def on_train_epoch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_train_epoch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
+
+ def on_val_epoch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_val_epoch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
+
+ def on_train_batch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_train_batch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
+
+ def on_val_batch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_val_batch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
+
+
+class CallbackList:
+ """Container for abstracting away callback calls."""
+
+ def __init__(self, callbacks: List[Callable] = None) -> None:
+ """TBC."""
+ self._callbacks = callbacks if callbacks is not None else []
+
+ def append(self, callback: Type[Callback]) -> None:
+ """Append new callback to callback list."""
+ self.callbacks.append(callback)
+
+ def on_fit_begin(self) -> None:
+ """Called when fit begins."""
+ for _ in self._callbacks:
+ pass
+
+ def on_fit_end(self) -> None:
+ """Called when fit ends."""
+ pass
+
+ def on_train_epoch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_train_epoch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
+
+ def on_val_epoch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_val_epoch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
+
+ def on_train_batch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_train_batch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
+
+ def on_val_batch_begin(self) -> None:
+ """Called at the beginning of an epoch."""
+ pass
+
+ def on_val_batch_end(self) -> None:
+ """Called at the end of an epoch."""
+ pass
diff --git a/src/training/callbacks/early_stopping.py b/src/training/callbacks/early_stopping.py
new file mode 100644
index 0000000..4da0e85
--- /dev/null
+++ b/src/training/callbacks/early_stopping.py
@@ -0,0 +1 @@
+"""Implements Early stopping for PyTorch model."""