-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from aai-institute/feature/callbacks
Add callbacks.
- Loading branch information
Showing
6 changed files
with
300 additions
and
84 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
""" | ||
`continuity.callbacks` | ||
Callbacks for training in Continuity. | ||
""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Optional, List, Dict | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
class Callback(ABC): | ||
""" | ||
Callback base class for `fit` method of `Operator`. | ||
""" | ||
|
||
@abstractmethod | ||
def __call__(self, epoch, logs: Dict[str, float]): | ||
"""Callback function. | ||
Called at the end of each epoch. | ||
Args: | ||
epoch: Current epoch. | ||
logs: Dictionary of logs. | ||
""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def on_train_begin(self): | ||
"""Called at the beginning of training.""" | ||
|
||
@abstractmethod | ||
def on_train_end(self): | ||
"""Called at the end of training.""" | ||
|
||
|
||
class PrintTrainingLoss(Callback): | ||
""" | ||
Callback to print training loss. | ||
""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def __call__(self, epoch: int, logs: Dict[str, float]): | ||
"""Callback function. | ||
Called at the end of each epoch. | ||
Args: | ||
epoch: Current epoch. | ||
logs: Dictionary of logs. | ||
""" | ||
loss_train = logs["loss/train"] | ||
seconds_per_epoch = logs["seconds_per_epoch"] | ||
|
||
print( | ||
f"\rEpoch {epoch}: loss/train = {loss_train:.4e} " | ||
f"({seconds_per_epoch:.2f} s/epoch)", | ||
end="", | ||
) | ||
|
||
def on_train_begin(self): | ||
"""Called at the beginning of training.""" | ||
|
||
def on_train_end(self): | ||
"""Called at the end of training.""" | ||
print("") | ||
|
||
|
||
class LearningCurve(Callback): | ||
""" | ||
Callback to plot learning curve. | ||
Args: | ||
keys: List of keys to plot. Default is ["loss/train"]. | ||
""" | ||
|
||
def __init__(self, keys: Optional[List[str]] = None): | ||
if keys is None: | ||
keys = ["loss/train"] | ||
|
||
self.keys = keys | ||
self.on_train_begin() | ||
super().__init__() | ||
|
||
def __call__(self, epoch: int, logs: Dict[str, float]): | ||
"""Callback function. | ||
Called at the end of each epoch. | ||
Args: | ||
epoch: Current epoch. | ||
logs: Dictionary of logs. | ||
""" | ||
for key in self.keys: | ||
if key in logs: | ||
self.losses[key].append(logs[key]) | ||
|
||
def on_train_begin(self): | ||
"""Called at the beginning of training.""" | ||
self.losses = {key: [] for key in self.keys} | ||
|
||
def on_train_end(self): | ||
"""Called at the end of training.""" | ||
for key in self.keys: | ||
vals = self.losses[key] | ||
epochs = list(range(1, len(vals) + 1)) | ||
plt.plot(epochs, vals) | ||
|
||
plt.yscale("log") | ||
plt.xlabel("Epoch") | ||
plt.ylabel("Loss") | ||
plt.legend(self.keys) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters