-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathearlyStop.py
30 lines (27 loc) · 972 Bytes
/
earlyStop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import torch
class EarlyStopping():
"""
Early stopping to stop the training when the loss does not improve after
certain epochs.
"""
def __init__(self, patience=5, min_delta=0):
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, loss, model, args, name, iteration):
if self.best_loss == None:
self.best_loss = loss
elif self.best_loss - loss > self.min_delta:
self.best_loss = loss
self.counter = 0
elif self.best_loss - loss < self.min_delta:
self.counter += 1
print(f"INFO: Early stopping counter {self.counter} of {self.patience}")
if self.counter >= self.patience:
print('INFO: Early stopping')
self.early_stop = True
if __name__ == '__main__':
early = EarlyStopping()