-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathact_norm.py
75 lines (59 loc) · 2.17 KB
/
act_norm.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import torch
import torch.nn as nn
from array_util import mean_dim
class ActNorm(nn.Module):
"""Activation normalization for 2D inputs.
The bias and scale get initialized using the mean and variance of the
first mini-batch. After the init, bias and scale are trainable parameters.
Adapted from:
> https://github.com/openai/glow
"""
def __init__(self, num_features, scale=1., return_ldj=False):
super(ActNorm, self).__init__()
self.register_buffer('is_initialized', torch.zeros(1))
self.bias = nn.Parameter(torch.zeros(1, num_features, 1, 1))
self.logs = nn.Parameter(torch.zeros(1, num_features, 1, 1))
self.num_features = num_features
self.scale = float(scale)
self.eps = 1e-6
self.return_ldj = return_ldj
def initialize_parameters(self, x):
if not self.training:
return
with torch.no_grad():
bias = -mean_dim(x.clone(), dim=[0, 2, 3], keepdims=True)
v = mean_dim((x.clone() + bias) ** 2, dim=[0, 2, 3], keepdims=True)
logs = (self.scale / (v.sqrt() + self.eps)).log()
self.bias.data.copy_(bias.data)
self.logs.data.copy_(logs.data)
self.is_initialized += 1.
def _center(self, x, reverse=False):
if reverse:
return x - self.bias
else:
return x + self.bias
def _scale(self, x, sldj, reverse=False):
logs = self.logs
if reverse:
x = x * logs.mul(-1).exp()
else:
x = x * logs.exp()
if sldj is not None:
ldj = logs.sum() * x.size(2) * x.size(3)
if reverse:
sldj = sldj - ldj
else:
sldj = sldj + ldj
return x, sldj
def forward(self, x, ldj=None, reverse=False):
if not self.is_initialized:
self.initialize_parameters(x)
if reverse:
x, ldj = self._scale(x, ldj, reverse)
x = self._center(x, reverse)
else:
x = self._center(x, reverse)
x, ldj = self._scale(x, ldj, reverse)
if self.return_ldj:
return x, ldj
return x