-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.py
More file actions
78 lines (61 loc) · 2.09 KB
/
callbacks.py
File metadata and controls
78 lines (61 loc) · 2.09 KB
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
"""
Training Callbacks
==================
Early stopping, checkpointing, and logging.
"""
import torch
import numpy as np
class EarlyStopping:
"""Stop training when validation metric stops improving"""
def __init__(self, patience=20, min_delta=0.0001, mode='max'):
self.patience = patience
self.min_delta = min_delta
self.mode = mode
self.best_score = None
self.counter = 0
self.early_stop = False
def __call__(self, score):
if self.best_score is None:
self.best_score = score
return False
if self.mode == 'max':
improved = score > self.best_score + self.min_delta
else:
improved = score < self.best_score - self.min_delta
if improved:
self.best_score = score
self.counter = 0
else:
self.counter += 1
if self.counter >= self.patience:
self.early_stop = True
return self.early_stop
class ModelCheckpoint:
"""Save best model based on validation metric"""
def __init__(self, filepath, monitor='val_r2', mode='max'):
self.filepath = filepath
self.monitor = monitor
self.mode = mode
self.best_score = None
def __call__(self, model, metrics):
score = metrics.get(self.monitor, 0)
if self.best_score is None:
self.best_score = score
self.save_checkpoint(model, metrics)
return True
if self.mode == 'max':
improved = score > self.best_score
else:
improved = score < self.best_score
if improved:
self.best_score = score
self.save_checkpoint(model, metrics)
return True
return False
def save_checkpoint(self, model, metrics):
"""Save model checkpoint"""
torch.save({
'model_state_dict': model.state_dict(),
'metrics': metrics,
'best_score': self.best_score
}, self.filepath)