-
Notifications
You must be signed in to change notification settings - Fork 0
/
ema.py
50 lines (43 loc) · 1.6 KB
/
ema.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
class EMA():
def __init__(self, model, decay, device=None):
self.model = model
self.decay = decay
self.shadow = {}
self.backup = {}
self.device = device
def register(self):
for name, param in self.model.named_parameters():
if param.requires_grad:
self.shadow[name] = param.data.clone()
def update(self):
for name, param in self.model.named_parameters():
if param.requires_grad:
assert name in self.shadow
if self.device is not None:
self.shadow[name] = self.shadow[name].to(self.device)
new_average = (1.0 - self.decay) * param.data + self.decay * self.shadow[name]
self.shadow[name] = new_average.clone()
def apply_shadow(self):
for name, param in self.model.named_parameters():
if param.requires_grad:
assert name in self.shadow
self.backup[name] = param.data
param.data = self.shadow[name]
def restore(self):
for name, param in self.model.named_parameters():
if param.requires_grad:
assert name in self.backup
param.data = self.backup[name]
self.backup = {}
# # 初始化
# ema = EMA(model, 0.999)
# ema.register()
# # 训练过程中,更新完参数后,同步update shadow weights
# def train():
# optimizer.step()
# ema.update()
# # eval前,apply shadow weights;eval之后,恢复原来模型的参数
# def evaluate():
# ema.apply_shadow()
# # evaluate
# ema.restore()