forked from Britefury/self-ensemble-visual-domain-adapt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optim_weight_ema.py
45 lines (34 loc) · 1.62 KB
/
optim_weight_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
from torch.optim import Optimizer
class OldWeightEMA (object):
"""
Exponential moving average weight optimizer for mean teacher model
"""
def __init__(self, target_net, source_net, alpha=0.999):
self.target_params = list(target_net.parameters())
self.source_params = list(source_net.parameters())
self.alpha = alpha
for p, src_p in zip(self.target_params, self.source_params):
p.data[:] = src_p.data[:]
def step(self):
one_minus_alpha = 1.0 - self.alpha
for p, src_p in zip(self.target_params, self.source_params):
p.data.mul_(self.alpha)
p.data.add_(src_p.data * one_minus_alpha)
class EMAWeightOptimizer (object):
def __init__(self, target_net, source_net, alpha=0.999):
self.target_net = target_net
self.source_net = source_net
self.ema_alpha = alpha
self.target_params = list(target_net.state_dict().values())
self.source_params = list(source_net.state_dict().values())
for tgt_p, src_p in zip(self.target_params, self.source_params):
tgt_p[:] = src_p[:]
target_keys = set(target_net.state_dict().keys())
source_keys = set(source_net.state_dict().keys())
if target_keys != source_keys:
raise ValueError('Source and target networks do not have the same state dict keys; do they have different architectures?')
def step(self):
one_minus_alpha = 1.0 - self.ema_alpha
for tgt_p, src_p in zip(self.target_params, self.source_params):
tgt_p.mul_(self.ema_alpha)
tgt_p.add_(src_p * one_minus_alpha)