forked from bayesiains/nflows
/
base.py
234 lines (172 loc) · 7.86 KB
/
base.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
"""Basic definitions for the transforms module."""
import numpy as np
import torch
from torch import nn
import nflows.utils.typechecks as check
class InverseNotAvailable(Exception):
"""Exception to be thrown when a transform does not have an inverse."""
pass
class InputOutsideDomain(Exception):
"""Exception to be thrown when the input to a transform is not within its domain."""
pass
class Transform(nn.Module):
"""Base class for all transform objects."""
def __init__(self):
super(Transform, self).__init__()
self.register_buffer('dummy_buffer', torch.tensor(1.0))
def forward(self, inputs, context=None):
raise NotImplementedError()
def inverse(self, inputs, context=None):
raise InverseNotAvailable()
class CompositeTransform(Transform):
"""Composes several transforms into one, in the order they are given."""
def __init__(self, transforms):
"""Constructor.
Args:
transforms: an iterable of `Transform` objects.
"""
super().__init__()
self._transforms = nn.ModuleList(transforms)
def _cascade(self, inputs, funcs, context):
batch_size = inputs.shape[0]
outputs = inputs
total_logabsdet = torch.zeros(batch_size, device=self._transforms[0].dummy_buffer.device)
for func in funcs:
outputs, logabsdet = func(outputs, context)
total_logabsdet += logabsdet
return outputs, total_logabsdet
def forward(self, inputs, context=None):
funcs = self._transforms
return self._cascade(inputs, funcs, context)
def inverse(self, inputs, context=None):
funcs = (transform.inverse for transform in self._transforms[::-1])
return self._cascade(inputs, funcs, context)
class MultiscaleCompositeTransform(Transform):
"""A multiscale composite transform as described in the RealNVP paper.
Splits the outputs along the given dimension after every transform, outputs one half, and
passes the other half to further transforms. No splitting is done before the last transform.
Note: Inputs could be of arbitrary shape, but outputs will always be flattened.
Reference:
> L. Dinh et al., Density estimation using Real NVP, ICLR 2017.
"""
def __init__(self, num_transforms, split_dim=1):
"""Constructor.
Args:
num_transforms: int, total number of transforms to be added.
split_dim: dimension along which to split.
"""
if not check.is_positive_int(split_dim):
raise TypeError("Split dimension must be a positive integer.")
super().__init__()
self._transforms = nn.ModuleList()
self._output_shapes = []
self._num_transforms = num_transforms
self._split_dim = split_dim
def add_transform(self, transform, transform_output_shape):
"""Add a transform. Must be called exactly `num_transforms` times.
Parameters:
transform: the `Transform` object to be added.
transform_output_shape: tuple, shape of transform's outputs, excl. the first batch
dimension.
Returns:
Input shape for the next transform, or None if adding the last transform.
"""
assert len(self._transforms) <= self._num_transforms
if len(self._transforms) == self._num_transforms:
raise RuntimeError(
"Adding more than {} transforms is not allowed.".format(
self._num_transforms
)
)
if (self._split_dim - 1) >= len(transform_output_shape):
raise ValueError("No split_dim in output shape")
if transform_output_shape[self._split_dim - 1] < 2:
raise ValueError(
"Size of dimension {} must be at least 2.".format(self._split_dim)
)
self._transforms.append(transform)
if len(self._transforms) != self._num_transforms: # Unless last transform.
output_shape = list(transform_output_shape)
output_shape[self._split_dim - 1] = (
output_shape[self._split_dim - 1] + 1
) // 2
output_shape = tuple(output_shape)
hidden_shape = list(transform_output_shape)
hidden_shape[self._split_dim - 1] = hidden_shape[self._split_dim - 1] // 2
hidden_shape = tuple(hidden_shape)
else:
# No splitting for last transform.
output_shape = transform_output_shape
hidden_shape = None
self._output_shapes.append(output_shape)
return hidden_shape
def forward(self, inputs, context=None):
if self._split_dim >= inputs.dim():
raise ValueError("No split_dim in inputs.")
if self._num_transforms != len(self._transforms):
raise RuntimeError(
"Expecting exactly {} transform(s) "
"to be added.".format(self._num_transforms)
)
batch_size = inputs.shape[0]
def cascade():
hiddens = inputs
for i, transform in enumerate(self._transforms[:-1]):
transform_outputs, logabsdet = transform(hiddens, context)
outputs, hiddens = torch.chunk(
transform_outputs, chunks=2, dim=self._split_dim
)
assert outputs.shape[1:] == self._output_shapes[i]
yield outputs, logabsdet
# Don't do the splitting for the last transform.
outputs, logabsdet = self._transforms[-1](hiddens, context)
yield outputs, logabsdet
all_outputs = []
total_logabsdet = torch.zeros(batch_size)
for outputs, logabsdet in cascade():
all_outputs.append(outputs.reshape(batch_size, -1))
total_logabsdet += logabsdet
all_outputs = torch.cat(all_outputs, dim=-1)
return all_outputs, total_logabsdet
def inverse(self, inputs, context=None):
if inputs.dim() != 2:
raise ValueError("Expecting NxD inputs")
if self._num_transforms != len(self._transforms):
raise RuntimeError(
"Expecting exactly {} transform(s) "
"to be added.".format(self._num_transforms)
)
batch_size = inputs.shape[0]
rev_inv_transforms = [transform.inverse for transform in self._transforms[::-1]]
split_indices = np.cumsum([np.prod(shape) for shape in self._output_shapes])
split_indices = np.insert(split_indices, 0, 0)
split_inputs = []
for i in range(len(self._output_shapes)):
flat_input = inputs[:, split_indices[i] : split_indices[i + 1]]
split_inputs.append(flat_input.view(-1, *self._output_shapes[i]))
rev_split_inputs = split_inputs[::-1]
total_logabsdet = torch.zeros(batch_size)
# We don't do the splitting for the last (here first) transform.
hiddens, logabsdet = rev_inv_transforms[0](rev_split_inputs[0], context)
total_logabsdet += logabsdet
for inv_transform, input_chunk in zip(
rev_inv_transforms[1:], rev_split_inputs[1:]
):
tmp_concat_inputs = torch.cat([input_chunk, hiddens], dim=self._split_dim)
hiddens, logabsdet = inv_transform(tmp_concat_inputs, context)
total_logabsdet += logabsdet
outputs = hiddens
return outputs, total_logabsdet
class InverseTransform(Transform):
"""Creates a transform that is the inverse of a given transform."""
def __init__(self, transform):
"""Constructor.
Args:
transform: An object of type `Transform`.
"""
super().__init__()
self._transform = transform
def forward(self, inputs, context=None):
return self._transform.inverse(inputs, context)
def inverse(self, inputs, context=None):
return self._transform(inputs, context)