-
Notifications
You must be signed in to change notification settings - Fork 33
/
bnaf.py
281 lines (229 loc) · 8.86 KB
/
bnaf.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
import torch
import math
class Sequential(torch.nn.Sequential):
"""
Class that extends ``torch.nn.Sequential`` for computing the output of
the function alongside with the log-det-Jacobian of such transformation.
"""
def forward(self, inputs: torch.Tensor):
"""
Parameters
----------
inputs : ``torch.Tensor``, required.
The input tensor.
Returns
-------
The output tensor and the log-det-Jacobian of this transformation.
"""
log_det_jacobian = 0.0
for i, module in enumerate(self._modules.values()):
inputs, log_det_jacobian_ = module(inputs)
log_det_jacobian = log_det_jacobian + log_det_jacobian_
return inputs, log_det_jacobian
class BNAF(torch.nn.Sequential):
"""
Class that extends ``torch.nn.Sequential`` for constructing a Block Neural
Normalizing Flow.
"""
def __init__(self, *args, res: str = None):
"""
Parameters
----------
*args : ``Iterable[torch.nn.Module]``, required.
The modules to use.
res : ``str``, optional (default = None).
Which kind of residual connection to use. ``res = None`` is no residual
connection, ``res = 'normal'`` is ``x + f(x)`` and ``res = 'gated'`` is
``a * x + (1 - a) * f(x)`` where ``a`` is a learnable parameter.
"""
super(BNAF, self).__init__(*args)
self.res = res
if res == "gated":
self.gate = torch.nn.Parameter(torch.nn.init.normal_(torch.Tensor(1)))
def forward(self, inputs: torch.Tensor):
"""
Parameters
----------
inputs : ``torch.Tensor``, required.
The input tensor.
Returns
-------
The output tensor and the log-det-Jacobian of this transformation.
"""
outputs = inputs
grad = None
for module in self._modules.values():
outputs, grad = module(outputs, grad)
grad = grad if len(grad.shape) == 4 else grad.view(grad.shape + [1, 1])
assert inputs.shape[-1] == outputs.shape[-1]
if self.res == "normal":
return inputs + outputs, torch.nn.functional.softplus(grad.squeeze()).sum(
-1
)
elif self.res == "gated":
return self.gate.sigmoid() * outputs + (1 - self.gate.sigmoid()) * inputs, (
torch.nn.functional.softplus(grad.squeeze() + self.gate)
- torch.nn.functional.softplus(self.gate)
).sum(-1)
else:
return outputs, grad.squeeze().sum(-1)
def _get_name(self):
return "BNAF(res={})".format(self.res)
class Permutation(torch.nn.Module):
"""
Module that outputs a permutation of its input.
"""
def __init__(self, in_features: int, p: list = None):
"""
Parameters
----------
in_features : ``int``, required.
The number of input features.
p : ``list`` or ``str``, optional (default = None)
The list of indeces that indicate the permutation. When ``p`` is not a
list, if ``p = 'flip'``the tensor is reversed, if ``p = None`` a random
permutation is applied.
"""
super(Permutation, self).__init__()
self.in_features = in_features
if p is None:
self.p = np.random.permutation(in_features)
elif p == "flip":
self.p = list(reversed(range(in_features)))
else:
self.p = p
def forward(self, inputs: torch.Tensor):
"""
Parameters
----------
inputs : ``torch.Tensor``, required.
The input tensor.
Returns
-------
The permuted tensor and the log-det-Jacobian of this permutation.
"""
return inputs[:, self.p], 0
def __repr__(self):
return "Permutation(in_features={}, p={})".format(self.in_features, self.p)
class MaskedWeight(torch.nn.Module):
"""
Module that implements a linear layer with block matrices with positive diagonal blocks.
Moreover, it uses Weight Normalization (https://arxiv.org/abs/1602.07868) for stability.
"""
def __init__(
self, in_features: int, out_features: int, dim: int, bias: bool = True
):
"""
Parameters
----------
in_features : ``int``, required.
The number of input features per each dimension ``dim``.
out_features : ``int``, required.
The number of output features per each dimension ``dim``.
dim : ``int``, required.
The number of dimensions of the input of the flow.
bias : ``bool``, optional (default = True).
Whether to add a parametrizable bias.
"""
super(MaskedWeight, self).__init__()
self.in_features, self.out_features, self.dim = in_features, out_features, dim
weight = torch.zeros(out_features, in_features)
for i in range(dim):
weight[
i * out_features // dim : (i + 1) * out_features // dim,
0 : (i + 1) * in_features // dim,
] = torch.nn.init.xavier_uniform_(
torch.Tensor(out_features // dim, (i + 1) * in_features // dim)
)
self._weight = torch.nn.Parameter(weight)
self._diag_weight = torch.nn.Parameter(
torch.nn.init.uniform_(torch.Tensor(out_features, 1)).log()
)
self.bias = (
torch.nn.Parameter(
torch.nn.init.uniform_(
torch.Tensor(out_features),
-1 / math.sqrt(out_features),
1 / math.sqrt(out_features),
)
)
if bias
else 0
)
mask_d = torch.zeros_like(weight)
for i in range(dim):
mask_d[
i * (out_features // dim) : (i + 1) * (out_features // dim),
i * (in_features // dim) : (i + 1) * (in_features // dim),
] = 1
self.register_buffer("mask_d", mask_d)
mask_o = torch.ones_like(weight)
for i in range(dim):
mask_o[
i * (out_features // dim) : (i + 1) * (out_features // dim),
i * (in_features // dim) :,
] = 0
self.register_buffer("mask_o", mask_o)
def get_weights(self):
"""
Computes the weight matrix using masks and weight normalization.
It also compute the log diagonal blocks of it.
"""
w = torch.exp(self._weight) * self.mask_d + self._weight * self.mask_o
w_squared_norm = (w ** 2).sum(-1, keepdim=True)
w = self._diag_weight.exp() * w / w_squared_norm.sqrt()
wpl = self._diag_weight + self._weight - 0.5 * torch.log(w_squared_norm)
return w.t(), wpl.t()[self.mask_d.bool().t()].view(
self.dim, self.in_features // self.dim, self.out_features // self.dim
)
def forward(self, inputs, grad: torch.Tensor = None):
"""
Parameters
----------
inputs : ``torch.Tensor``, required.
The input tensor.
grad : ``torch.Tensor``, optional (default = None).
The log diagonal block of the partial Jacobian of previous transformations.
Returns
-------
The output tensor and the log diagonal blocks of the partial log-Jacobian of previous
transformations combined with this transformation.
"""
w, wpl = self.get_weights()
g = wpl.transpose(-2, -1).unsqueeze(0).repeat(inputs.shape[0], 1, 1, 1)
return (
inputs.matmul(w) + self.bias,
torch.logsumexp(g.unsqueeze(-2) + grad.transpose(-2, -1).unsqueeze(-3), -1)
if grad is not None
else g,
)
def __repr__(self):
return "MaskedWeight(in_features={}, out_features={}, dim={}, bias={})".format(
self.in_features,
self.out_features,
self.dim,
not isinstance(self.bias, int),
)
class Tanh(torch.nn.Tanh):
"""
Class that extends ``torch.nn.Tanh`` additionally computing the log diagonal
blocks of the Jacobian.
"""
def forward(self, inputs, grad: torch.Tensor = None):
"""
Parameters
----------
inputs : ``torch.Tensor``, required.
The input tensor.
grad : ``torch.Tensor``, optional (default = None).
The log diagonal blocks of the partial Jacobian of previous transformations.
Returns
-------
The output tensor and the log diagonal blocks of the partial log-Jacobian of previous
transformations combined with this transformation.
"""
g = -2 * (inputs - math.log(2) + torch.nn.functional.softplus(-2 * inputs))
return (
torch.tanh(inputs),
(g.view(grad.shape) + grad) if grad is not None else g,
)