-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
34 lines (27 loc) · 1.01 KB
/
Copy pathmodels.py
File metadata and controls
34 lines (27 loc) · 1.01 KB
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
import torch
from torch import nn
class GaussianNoise(nn.Module):
def __init__(self, sigma=1.0):
super().__init__()
self.sigma = sigma
self.noise = torch.tensor(0.0).cuda()
def forward(self, x):
if self.training:
sampled_noise = self.noise.repeat(*x.size()).normal_(mean=0, std=self.sigma)
x = x + sampled_noise
return x
class EMA:
def __init__(self, decay):
self.decay = decay
self.shadow = {}
def register(self, model):
for name, param in model.named_parameters():
if param.requires_grad:
self.shadow[name] = param.data.clone()
self.params = self.shadow.keys()
def __call__(self, model):
if self.decay > 0:
for name, param in model.named_parameters():
if name in self.params and param.requires_grad:
self.shadow[name] -= (1 - self.decay) * (self.shadow[name] - param.data)
param.data = self.shadow[name]