MCPcopy Create free account
hub / github.com/AtlasAnalyticsLab/AdaFisher / EarlyStop

Class EarlyStop

Image_Classification/src/utils/early_stop.py:6–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5
6class 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

Callers 1

load_configMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected