-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
78 lines (65 loc) · 2.63 KB
/
utils.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
import torch
import torch.optim as optim
def save_checkpoint(epoch, program, mask, optimizer, best_val, lr_scheduler, file_path='models/checkpoint'):
torch.save({
'epoch': epoch,
'program': program,
'mask': mask,
'optimizer': optimizer.state_dict(),
'lr_scheduler': lr_scheduler.state_dict(),
'best_val': best_val
}, file_path)
print('Checkpoint created: {}'.format(file_path))
def save_model(program, mask=None, file_path='models/model'):
if mask is not None:
torch.save({
'program': program,
'mask': mask,
}, file_path)
else:
torch.save({
'program': program,
}, file_path)
print('Model saved: {}'.format(file_path))
def load_checkpoint(optimizer=None, lr_scheduler=None, file_path='models/checkpoint'):
state = torch.load(file_path)
if optimizer is not None:
optimizer.load_state_dict(state['optimizer'])
if lr_scheduler is not None:
lr_scheduler.load_state_dict(state['lr_scheduler'])
print('Checkpoint restored: {}'.format(file_path))
return state['program'], state['epoch'], state['best_val']
class LRScheduler(optim.lr_scheduler.ReduceLROnPlateau):
def __init__(self, optimizer, patience=2, verbose=True, factor=0.96, mode='max'):
super(LRScheduler, self).__init__(optimizer, patience=patience, verbose=verbose, factor=factor, min_lr=1.6e-6, mode=mode)
def is_impatient(self, metrics, epoch=None):
current = metrics
if epoch is None:
epoch = self.last_epoch = self.last_epoch + 1
self.last_epoch = epoch
if self.is_better(current, self.best):
self.best = current
self.num_bad_epochs = 0
else:
self.num_bad_epochs += 1
if self.in_cooldown:
self.cooldown_counter -= 1
self.num_bad_epochs = 0
if self.num_bad_epochs > self.patience:
# self._reduce_lr(epoch)
self.cooldown_counter = self.cooldown
self.num_bad_epochs = 0
return True
return False
def reduce_lr(self, epoch=None):
reduced = False
for i, param_group in enumerate(self.optimizer.param_groups):
old_lr = float(param_group['lr'])
new_lr = max(old_lr * self.factor, self.min_lrs[i])
if old_lr - new_lr > self.eps:
param_group['lr'] = new_lr
reduced = True
if self.verbose:
print('Epoch {:5d}: reducing learning rate'
' of group {} to {:.4e}.'.format(epoch, i, new_lr))
return reduced