forked from foamliu/InsightFace-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.py
33 lines (27 loc) · 1.1 KB
/
optimizer.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
class InsightFaceOptimizer(object):
"""A simple wrapper class for learning rate scheduling"""
def __init__(self, optimizer):
self.optimizer = optimizer
self.step_num = 0
self.lr = 0.1
def zero_grad(self):
self.optimizer.zero_grad()
def step(self):
self._update_lr()
self.optimizer.step()
def _update_lr(self):
self.step_num += 1
# divide the learning rate at 100K,160K iterations
if self.step_num in [100000, 160000]:
self.lr = self.lr / 10
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.lr
def clip_gradient(self, grad_clip):
for group in self.optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def adjust_learning_rate(self, new_lr):
for param_group in self.optimizer.param_groups:
param_group['lr'] = new_lr
print("The new learning rate is %f\n" % (self.optimizer.param_groups[0]['lr'],))