forked from ikostrikov/pytorch-flows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflows.py
284 lines (223 loc) · 9.5 KB
/
flows.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import types
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
def get_mask(in_features, out_features, in_flow_features, mask_type=None):
"""
mask_type: input | None | output
See Figure 1 for a better illustration:
https://arxiv.org/pdf/1502.03509.pdf
"""
if mask_type == 'input':
in_degrees = torch.arange(in_features) % in_flow_features
else:
in_degrees = torch.arange(in_features) % (in_flow_features - 1)
if mask_type == 'output':
out_degrees = torch.arange(out_features) % in_flow_features - 1
else:
out_degrees = torch.arange(out_features) % (in_flow_features - 1)
return (out_degrees.unsqueeze(-1) >= in_degrees.unsqueeze(0)).float()
class MaskedLinear(nn.Linear):
def __init__(self, in_features, out_features, mask, bias=True):
super(MaskedLinear, self).__init__(in_features, out_features, bias)
self.register_buffer('mask', mask)
def forward(self, inputs):
return F.linear(inputs, self.weight * self.mask, self.bias)
nn.MaskedLinear = MaskedLinear
class MADE(nn.Module):
""" An implementation of MADE
(https://arxiv.org/abs/1502.03509s).
"""
def __init__(self, num_inputs, num_hidden):
super(MADE, self).__init__()
input_mask = get_mask(
num_inputs, num_hidden, num_inputs, mask_type='input')
hidden_mask = get_mask(num_hidden, num_hidden, num_inputs)
output_mask = get_mask(
num_hidden, num_inputs * 2, num_inputs, mask_type='output')
self.main = nn.Sequential(
nn.MaskedLinear(num_inputs, num_hidden, input_mask), nn.ReLU(),
nn.MaskedLinear(num_hidden, num_hidden, hidden_mask), nn.ReLU(),
nn.MaskedLinear(num_hidden, num_inputs * 2, output_mask))
def forward(self, inputs, mode='direct'):
if mode == 'direct':
x = self.main(inputs)
m, a = x.chunk(2, 1)
u = (inputs - m) * torch.exp(a)
return u, a.sum(-1, keepdim=True)
else:
# TODO:
# Sampling with MADE is tricky.
# We need to perform N forward passes.
raise NotImplementedError
class BatchNormFlow(nn.Module):
""" An implementation of a batch normalization layer from
Density estimation using Real NVP
(https://arxiv.org/abs/1605.08803).
"""
def __init__(self, num_inputs, momentum=0.0, eps=1e-5):
super(BatchNormFlow, self).__init__()
self.log_gamma = nn.Parameter(torch.zeros(num_inputs))
self.beta = nn.Parameter(torch.zeros(num_inputs))
self.momentum = momentum
self.eps = eps
self.register_buffer('running_mean', torch.zeros(num_inputs))
self.register_buffer('running_var', torch.ones(num_inputs))
def forward(self, inputs, mode='direct'):
if mode == 'direct':
if self.training:
self.batch_mean = inputs.mean(0)
self.batch_var = (
inputs - self.batch_mean).pow(2).mean(0) + self.eps
self.running_mean.mul_(self.momentum)
self.running_var.mul_(self.momentum)
self.running_mean.add_(self.batch_mean.data *
(1 - self.momentum))
self.running_var.add_(self.batch_var.data *
(1 - self.momentum))
mean = self.batch_mean
var = self.batch_var
else:
mean = self.running_mean
var = self.running_var
x_hat = (inputs - mean) / var.sqrt()
y = torch.exp(self.log_gamma) * x_hat + self.beta
return y, (self.log_gamma - 0.5 * torch.log(var)).sum(
-1, keepdim=True)
else:
if self.training:
mean = self.batch_mean
var = self.batch_var
else:
mean = self.running_mean
var = self.running_var
x_hat = (inputs - self.beta) / torch.exp(self.log_gamma)
y = x_hat * var.sqrt() + mean
return y, (-self.log_gamma + 0.5 * torch.log(var)).sum(
-1, keepdim=True)
class ActNorm(nn.Module):
""" An implementation of a activation normalization layer
from Glow: Generative Flow with Invertible 1x1 Convolutions
(https://arxiv.org/abs/1807.03039).
"""
def __init__(self, num_inputs):
super(ActNorm, self).__init__()
self.weight = nn.Parameter(torch.ones(num_inputs))
self.bias = nn.Parameter(torch.zeros(num_inputs))
self.initialized = False
def forward(self, inputs, mode='direct'):
if self.initialized == False:
self.weight.data.copy_(torch.log(1.0 / (inputs.std(0) + 1e-12)))
self.bias.data.copy_(inputs.mean(0))
self.initialized = True
if mode == 'direct':
return (
inputs - self.bias) * torch.exp(self.weight), self.weight.sum(
-1, keepdim=True).unsqueeze(0).repeat(inputs.size(0), 1)
else:
return inputs * torch.exp(
-self.weight) + self.bias, -self.weight.sum(
-1, keepdim=True).unsqueeze(0).repeat(inputs.size(0), 1)
class InvertibleMM(nn.Module):
""" An implementation of a invertible matrix multiplication
layer from Glow: Generative Flow with Invertible 1x1 Convolutions
(https://arxiv.org/abs/1807.03039).
"""
def __init__(self, num_inputs):
super(InvertibleMM, self).__init__()
self.W = nn.Parameter(torch.Tensor(num_inputs, num_inputs))
nn.init.orthogonal_(self.W)
def forward(self, inputs, mode='direct'):
if mode == 'direct':
return inputs @ self.W, torch.log(torch.abs(torch.det(
self.W))).unsqueeze(0).unsqueeze(0).repeat(inputs.size(0), 1)
else:
return inputs @ torch.inverse(self.W), -torch.log(
torch.abs(torch.det(self.W))).unsqueeze(0).unsqueeze(0).repeat(
inputs.size(0), 1)
class Shuffle(nn.Module):
""" An implementation of a shuffling layer from
Density estimation using Real NVP
(https://arxiv.org/abs/1605.08803).
"""
def __init__(self, num_inputs):
super(Shuffle, self).__init__()
self.perm = np.random.permutation(num_inputs)
self.inv_perm = np.argsort(self.perm)
def forward(self, inputs, mode='direct'):
if mode == 'direct':
return inputs[:, self.perm], torch.zeros(
inputs.size(0), 1, device=inputs.device)
else:
return inputs[:, self.inv_perm], torch.zeros(
inputs.size(0), 1, device=inputs.device)
class Reverse(nn.Module):
""" An implementation of a reversing layer from
Density estimation using Real NVP
(https://arxiv.org/abs/1605.08803).
"""
def __init__(self, num_inputs):
super(Reverse, self).__init__()
self.perm = np.array(np.arange(0, num_inputs)[::-1])
self.inv_perm = np.argsort(self.perm)
def forward(self, inputs, mode='direct'):
if mode == 'direct':
return inputs[:, self.perm], torch.zeros(
inputs.size(0), 1, device=inputs.device)
else:
return inputs[:, self.inv_perm], torch.zeros(
inputs.size(0), 1, device=inputs.device)
class CouplingLayer(nn.Module):
""" An implementation of a coupling layer
from RealNVP (https://arxiv.org/abs/1605.08803).
"""
def __init__(self, num_inputs, num_hidden=64):
super(CouplingLayer, self).__init__()
self.num_inputs = num_inputs
self.main = nn.Sequential(
nn.Linear(num_inputs // 2, num_hidden), nn.ReLU(),
nn.Linear(num_hidden, num_hidden), nn.ReLU(),
nn.Linear(num_hidden, 2 * (self.num_inputs - num_inputs // 2)))
def init(m):
if isinstance(m, nn.Linear):
m.bias.data.fill_(0)
nn.init.orthogonal_(m.weight.data)
def forward(self, inputs, mode='direct'):
if mode == 'direct':
x_a, x_b = inputs.chunk(2, dim=-1)
log_s, t = self.main(x_b).chunk(2, dim=-1)
s = torch.exp(log_s)
y_a = x_a * s + t
y_b = x_b
return torch.cat([y_a, y_b], dim=-1), log_s.sum(-1, keepdim=True)
else:
y_a, y_b = inputs.chunk(2, dim=-1)
log_s, t = self.main(y_b).chunk(2, dim=-1)
s = torch.exp(-log_s)
x_a = (y_a - t) * s
x_b = y_b
return torch.cat([x_a, x_b], dim=-1), -log_s.sum(-1, keepdim=True)
class FlowSequential(nn.Sequential):
""" A sequential container for flows.
In addition to a forward pass it implements a backward pass and
computes log jacobians.
"""
def forward(self, inputs, mode='direct', logdets=None):
""" Performs a forward or backward pass for flow modules.
Args:
inputs: a tuple of inputs and logdets
mode: to run direct computation or inverse
"""
if logdets is None:
logdets = torch.zeros(inputs.size(0), 1, device=inputs.device)
assert mode in ['direct', 'inverse']
if mode == 'direct':
for module in self._modules.values():
inputs, logdet = module(inputs, mode)
logdets += logdet
else:
for module in reversed(self._modules.values()):
inputs, logdet = module(inputs, mode)
logdets += logdet
return inputs, logdets