| 4 | |
| 5 | |
| 6 | class EarlyStop: |
| 7 | def __init__(self, patience: int = 10, threshold: float = 1e-2) -> None: |
| 8 | # self.queue = collections.deque([0] * patience, maxlen=patience) |
| 9 | self.patience = patience |
| 10 | self.threshold = threshold |
| 11 | self.wait = 0 |
| 12 | self.best_loss = np.Inf |
| 13 | |
| 14 | def reset(self) -> None: |
| 15 | self.best_loss = np.Inf |
| 16 | self.wait = 0 |
| 17 | |
| 18 | def __call__(self, train_loss: float) -> bool: |
| 19 | """ |
| 20 | @monitor: value to monitor for early stopping |
| 21 | (e.g. train_loss, test_loss, ...) |
| 22 | @mode: specify whether you want to maximize or minimize |
| 23 | relative to @monitor |
| 24 | """ |
| 25 | if np.less(self.threshold, 0): |
| 26 | return False |
| 27 | if train_loss is None: |
| 28 | return False |
| 29 | # self.queue.append(train_loss) |
| 30 | if np.less(train_loss - self.best_loss, -self.threshold): |
| 31 | self.best_loss = train_loss |
| 32 | self.wait = 0 |
| 33 | else: |
| 34 | self.wait += 1 |
| 35 | if self.wait >= self.patience: |
| 36 | return True |
| 37 | return False |