forked from haonan-yuan/DG-Mamba
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathearly_stopping.py
79 lines (69 loc) · 2.89 KB
/
early_stopping.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import torch
import torch.nn as nn
import logging
class early_stopping(object):
def __init__(self, patience: int, save_model_path: str, model_name: str = None):
"""
Early stop strategy.
:param patience: int, max patience
:param save_model_path: str, save model path
:param logger: Logger
:param model_name: str, model name
"""
self.patience = patience
self.counter = 0
self.best_metrics = {}
self.early_stop = False
self.save_model_path = save_model_path
self.model_name = model_name
def step(self, metrics: list, model: nn.Module):
"""
execute the early stop strategy for each evaluation process
:param metrics: list, list of metrics, each element is a tuple (str, float, boolean) -> (metric_name, metric_value, whether higher means better)
:param model: nn.Module
:return:
"""
metrics_compare_results = []
for metric_tuple in metrics:
metric_name, metric_value, higher_better = metric_tuple[0], metric_tuple[1], metric_tuple[2]
if higher_better:
if self.best_metrics.get(metric_name) is None or metric_value >= self.best_metrics.get(metric_name):
metrics_compare_results.append(True)
else:
metrics_compare_results.append(False)
else:
if self.best_metrics.get(metric_name) is None or metric_value <= self.best_metrics.get(metric_name):
metrics_compare_results.append(True)
else:
metrics_compare_results.append(False)
# all the computed metrics are better than the best metrics
if torch.all(torch.tensor(metrics_compare_results)):
for metric_tuple in metrics:
metric_name, metric_value = metric_tuple[0], metric_tuple[1]
self.best_metrics[metric_name] = metric_value
self.save_checkpoint(model)
self.counter = 0
# metrics are not better at the epoch
else:
self.counter += 1
if self.counter >= self.patience:
self.early_stop = True
return self.early_stop
def save_checkpoint(self, model: nn.Module):
"""
saves model at self.save_model_path
:param model: nn.Module
:return:
"""
print(f"save model {self.save_model_path}")
torch.save(model.state_dict(), self.save_model_path)
def load_checkpoint(self, model: nn.Module, map_location: str = None):
"""
load model at self.save_model_path
:param model: nn.Module
:param map_location: str, how to remap the storage locations
:return:
"""
print(f"load model {self.save_model_path}")
model.load_state_dict(torch.load(self.save_model_path, map_location=map_location))