-
Notifications
You must be signed in to change notification settings - Fork 15
/
flows.py
328 lines (243 loc) · 10.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import numpy as np
import sys
import math
import torch
from torch import nn
from torch.nn.parameter import Parameter
from torch.autograd import Variable
from MADE import MADE
from common import FeedForwardNet
# Transformation functions
class Affine():
num_params = 2
@staticmethod
def get_pseudo_params(nn_outp):
a = nn_outp[..., 0] # [B, D]
var_outp = nn_outp[..., 1]
b = torch.exp(0.5*var_outp)
logbsq = var_outp
return a, logbsq, b
@staticmethod
def standard(x, nn_outp):
a, logbsq, b = Affine.get_pseudo_params(nn_outp)
y = a + b*x
logdet = 0.5*logbsq.sum(-1)
return y, logdet
@staticmethod
def reverse(y, nn_outp):
a, logbsq, b = Affine.get_pseudo_params(nn_outp)
x = (y - a)/b
logdet = 0.5*logbsq.sum(-1)
return x, logdet
def arccosh(x):
return torch.log(x + torch.sqrt(x.pow(2)-1))
def arcsinh(x):
return torch.log(x + torch.sqrt(x.pow(2)+1))
class NLSq():
num_params = 5
logA = math.log(8*math.sqrt(3)/9-0.05) # 0.05 is a small number to prevent exactly 0 slope
@staticmethod
def get_pseudo_params(nn_outp):
a = nn_outp[..., 0] # [B, D]
logb = nn_outp[..., 1]*0.4
B = nn_outp[..., 2]*0.3
logd = nn_outp[..., 3]*0.4
f = nn_outp[..., 4]
b = torch.exp(logb)
d = torch.exp(logd)
c = torch.tanh(B)*torch.exp(NLSq.logA + logb - logd)
return a, b, c, d, f
@staticmethod
def standard(x, nn_outp):
a, b, c, d, f = NLSq.get_pseudo_params(nn_outp)
# double needed for stability. No effect on overall speed
a = a.double()
b = b.double()
c = c.double()
d = d.double()
f = f.double()
x = x.double()
aa = -b*d.pow(2)
bb = (x-a)*d.pow(2) - 2*b*d*f
cc = (x-a)*2*d*f - b*(1+f.pow(2))
dd = (x-a)*(1+f.pow(2)) - c
p = (3*aa*cc - bb.pow(2))/(3*aa.pow(2))
q = (2*bb.pow(3) - 9*aa*bb*cc + 27*aa.pow(2)*dd)/(27*aa.pow(3))
t = -2*torch.abs(q)/q*torch.sqrt(torch.abs(p)/3)
inter_term1 = -3*torch.abs(q)/(2*p)*torch.sqrt(3/torch.abs(p))
inter_term2 = 1/3*arccosh(torch.abs(inter_term1-1)+1)
t = t*torch.cosh(inter_term2)
tpos = -2*torch.sqrt(torch.abs(p)/3)
inter_term1 = 3*q/(2*p)*torch.sqrt(3/torch.abs(p))
inter_term2 = 1/3*arcsinh(inter_term1)
tpos = tpos*torch.sinh(inter_term2)
t[p > 0] = tpos[p > 0]
y = t - bb/(3*aa)
arg = d*y + f
denom = 1 + arg.pow(2)
x_new = a + b*y + c/denom
logdet = -torch.log(b - 2*c*d*arg/denom.pow(2)).sum(-1)
y = y.float()
logdet = logdet.float()
return y, logdet
@staticmethod
def reverse(y, nn_outp):
a, b, c, d, f = NLSq.get_pseudo_params(nn_outp)
arg = d*y + f
denom = 1 + arg.pow(2)
x = a + b*y + c/denom
logdet = -torch.log(b - 2*c*d*arg/denom.pow(2)).sum(-1)
return x, logdet
class SCFLayer(nn.Module):
def __init__(self, data_dim, n_hidden_layers, n_hidden_units, nonlinearity, transform_function, hidden_order=None, swap_trngen_dirs=False,
input_order=None, conditional_inp_dim=None, dropout=[0, 0]):
super().__init__()
self.net = FeedForwardNet(data_dim//2 + conditional_inp_dim, n_hidden_units, (data_dim-(data_dim//2))*transform_function.num_params, n_hidden_layers, nonlinearity, dropout=dropout[1])
self.train_func = transform_function.standard if swap_trngen_dirs else transform_function.reverse
self.gen_func = transform_function.reverse if swap_trngen_dirs else transform_function.standard
self.input_order = input_order
self.use_cond_inp = conditional_inp_dim is not None
def forward(self, inputs):
"""
Defines the reverse pass which is used during training
logdet means log det del_y/del_x
"""
data_dim = len(self.input_order)
assert data_dim == inputs[0].shape[-1]
first_indices = torch.arange(len(self.input_order))[self.input_order <= data_dim//2] # This is <= because input_order goes from 1 to data_dim+1
second_indices = torch.arange(len(self.input_order))[self.input_order > data_dim//2]
if self.use_cond_inp:
y, logdet, cond_inp = inputs
net_inp = torch.cat([y[..., first_indices], cond_inp], -1)
else:
y, logdet = inputs
net_inp = y[..., first_indices]
nn_outp = self.net(net_inp).view(*net_inp.shape[:-1], data_dim-(data_dim//2), -1) # [..., ~data_dim/2, num_params]
x = torch.tensor(y)
x[..., second_indices], change_logdet = self.train_func(y[..., second_indices], nn_outp)
return x, logdet + change_logdet, cond_inp
def generate(self, inputs):
"""
Defines the forward pass which is used during testing
logdet means log det del_y/del_x
"""
data_dim = len(self.input_order)
assert data_dim == inputs[0].shape[-1]
first_indices = torch.arange(len(self.input_order))[self.input_order <= data_dim//2] # This is <= because input_order goes from 1 to data_dim+1
second_indices = torch.arange(len(self.input_order))[self.input_order > data_dim//2]
if self.use_cond_inp:
x, logdet, cond_inp = inputs
net_inp = torch.cat([x[..., first_indices], cond_inp], -1)
else:
x, logdet = inputs
net_inp = x[..., first_indices]
nn_outp = self.net(net_inp).view(*net_inp.shape[:-1], data_dim-(data_dim//2), -1) # [..., ~data_dim/2, num_params]
y = torch.tensor(x)
y[..., second_indices], change_logdet = self.gen_func(x[..., second_indices], nn_outp)
return y, logdet + change_logdet, cond_inp
class AFLayer(nn.Module):
def __init__(self, data_dim, n_hidden_layers, n_hidden_units, nonlinearity, transform_function, hidden_order='sequential', swap_trngen_dirs=False,
input_order=None, conditional_inp_dim=None, dropout=[0, 0], coupling_level=0):
super().__init__()
self.made = MADE(data_dim, n_hidden_layers, n_hidden_units, nonlinearity, hidden_order,
out_dim_per_inp_dim=transform_function.num_params, input_order=input_order, conditional_inp_dim=conditional_inp_dim,
dropout=dropout)
self.train_func = transform_function.standard if swap_trngen_dirs else transform_function.reverse
self.gen_func = transform_function.reverse if swap_trngen_dirs else transform_function.standard
self.output_order = self.made.end_order
self.data_dim = data_dim
self.use_cond_inp = conditional_inp_dim is not None
def forward(self, inputs):
"""
Defines the reverse pass which is used during training
logdet means log det del_y/del_x
"""
if self.use_cond_inp:
y, logdet, cond_inp = inputs
nn_outp = self.made([y, cond_inp]) # [B, D, 2]
else:
y, logdet = inputs
nn_outp = self.made(y) # [B, D, 2]
x, change_logdet = self.train_func(y, nn_outp)
return x, logdet + change_logdet, cond_inp
def generate(self, inputs):
"""
Defines the forward pass which is used during testing
logdet means log det del_y/del_x
"""
if self.use_cond_inp:
x, logdet, cond_inp = inputs
else:
x, logdet = inputs
y = torch.tensor(x)
for idx in range(self.data_dim):
t = (self.output_order==idx).nonzero()[0][0]
if self.use_cond_inp:
nn_outp = self.made([y, cond_inp])
else:
nn_outp = self.made(y)
y[..., t:t+1], new_partial_logdet = self.gen_func(x[..., t:t+1], nn_outp[..., t:t+1, :])
logdet += new_partial_logdet
return y, logdet, cond_inp
# Full flow combining multiple layers
class Flow(nn.Module):
def __init__(self, data_dim, n_hidden_layers, n_hidden_units, nonlinearity, num_flow_layers, transform_function,
iaf_like=False, hidden_order='sequential',
swap_trngen_dirs=False, conditional_inp_dim=None, dropout=[0, 0], reverse_between_layers=True,
scf_layers=False, reverse_first_layer=False):
super().__init__()
if transform_function == 'affine':
transform_function = Affine
elif transform_function == 'nlsq':
transform_function = NLSq
elif transform_function != Affine and transform_function != NLSq: # Can pass string or actual class
raise NotImplementedError('Only the affine transformation function has been implemented')
if scf_layers:
AutoregressiveLayer = SCFLayer
else:
AutoregressiveLayer = AFLayer
# Note: This ordering is the ordering as applied to go from data -> base
flow_layers = []
input_order = torch.arange(data_dim)+1
if reverse_first_layer:
input_order = reversed(input_order)
for i in range(num_flow_layers):
flow_layers.append(AutoregressiveLayer(data_dim, n_hidden_layers, n_hidden_units, nonlinearity, transform_function,
hidden_order=hidden_order, swap_trngen_dirs=swap_trngen_dirs, input_order=input_order,
conditional_inp_dim=conditional_inp_dim, dropout=dropout))
if reverse_between_layers:
input_order = reversed(input_order)
self.flow = nn.Sequential(*flow_layers)
self.use_cond_inp = conditional_inp_dim is not None
def forward(self, inputs):
"""
Defines the reverse pass which is used during training
logdet means log det del_y/del_x
"""
if self.use_cond_inp:
y, cond_inp = inputs
else:
y = inputs
logdet = torch.zeros(y.shape[:-1], device=y.device)
if self.use_cond_inp:
x, logdet, _ = self.flow([y, logdet, cond_inp])
else:
x, logdet = self.flow([y, logdet])
return x, logdet
def generate(self, inputs):
"""
Defines the forward pass which is used during testing
logdet means log det del_y/del_x
"""
if self.use_cond_inp:
x, cond_inp = inputs
else:
x = inputs
logdet = torch.zeros(x.shape[:-1], device=x.device)
y = x
for flow_layer in reversed(self.flow):
if self.use_cond_inp:
y, logdet, _ = flow_layer.generate([y, logdet, cond_inp])
else:
y, logdet = flow_layer.generate([y, logdet])
return y, logdet