forked from foamliu/MobileFaceNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizer.py
32 lines (27 loc) · 1.03 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
class MFNptimizer(object):
"""A simple wrapper class for learning rate scheduling"""
def __init__(self, optimizer):
self.optimizer = optimizer
self.lr = 0.1
self.step_num = 0
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
if self.step_num in [36000, 52000, 78000, 100000]:
self.lr = self.lr / 10
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.lr
def clip_gradient(self, grad_clip):
"""
Clips gradients computed during backpropagation to avoid explosion of gradients.
:param optimizer: optimizer with the gradients to be clipped
:param grad_clip: clip value
"""
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)