-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUTIls.py
61 lines (51 loc) · 1.85 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
import torch
import numpy as np
def clip_gradient(optimizer, grad_clip):
"""
For calibrating misalignment gradient via cliping gradient technique
:param optimizer:
:param grad_clip:
:return:
"""
for group in optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def adjust_lr(optimizer, init_lr, epoch, decay_rate=0.1, decay_epoch=30):
decay = decay_rate ** (epoch // decay_epoch)
for param_group in optimizer.param_groups:
param_group['lr'] = init_lr * decay
def poly_lr(optimizer, init_lr, curr_iter, max_iter, power=0.9):
lr = init_lr * (1 - float(curr_iter) / max_iter) ** power
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def warmup_poly(optimizer, init_lr, curr_iter, max_iter):
warm_start_lr = 1e-7
warm_steps = 1000
if curr_iter<= warm_steps:
warm_factor = (init_lr / warm_start_lr) ** (1 / warm_steps)
warm_lr = warm_start_lr * warm_factor ** curr_iter
for param_group in optimizer.param_groups:
param_group['lr'] = warm_lr
else:
lr = init_lr * (1 - (curr_iter - warm_steps) / (max_iter - warm_steps)) ** 0.9
for param_group in optimizer.param_groups:
param_group['lr'] = lr
class AvgMeter(object):
def __init__(self, num=40):
self.num = num
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
self.losses = []
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
self.losses.append(val)
def show(self):
return torch.mean(torch.stack(self.losses[np.maximum(len(self.losses)-self.num, 0):]))