File size: 1,529 Bytes
e0d0d76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
import torch
import numpy as np


class EarlyStopping:

    def __init__(self, patience=5, verbose=False, path='checkpoint_model.pth'):

        self.patience = patience    # stop cpunter
        self.verbose = verbose 
        self.counter = 0            # current counter
        self.best_score = None      # best score
        self.early_stop = False     # stop flag
        self.val_loss_min = np.Inf   # to memorize previous best score
        self.path = path             # path to save the best model

    def __call__(self, val_loss, model):

        score = -val_loss

        if self.best_score is None:  #1Epoch
            self.best_score = score
            self.checkpoint(val_loss, model)  # save model and show score
        elif score < self.best_score:  # if it can not update best score
            self.counter += 1   # stop counter +1
            if self.verbose:
                print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
            if self.counter >= self.patience:
                self.early_stop = True
        else:  # if it update best score
            self.best_score = score
            self.checkpoint(val_loss, model) # save model and show score
            self.counter = 0  # stop counter is reset

    def checkpoint(self, val_loss, model):
        if self.verbose:
            print(f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}).  Saving model ...')
        torch.save(model.state_dict(), self.path)
        self.val_loss_min = val_loss