-
Notifications
You must be signed in to change notification settings - Fork 28
/
probability.py
468 lines (375 loc) · 16.4 KB
/
probability.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# Copyright 2023 NNAISENSE SA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import functools
from abc import abstractmethod
from torch.distributions.normal import Normal
from torch.distributions.categorical import Categorical as torch_Categorical
from torch.distributions.bernoulli import Bernoulli as torch_Bernoulli
from torch.distributions.mixture_same_family import MixtureSameFamily
from torch.distributions.uniform import Uniform
from math import log
from utils_model import (
safe_exp,
safe_log,
idx_to_float,
float_to_idx,
quantize, sandwich,
)
class CtsDistribution:
@abstractmethod
def log_prob(self, x):
pass
@abstractmethod
def sample(self):
pass
class DiscreteDistribution:
@property
@abstractmethod
def probs(self):
pass
@functools.cached_property
def log_probs(self):
return safe_log(self.probs)
@functools.cached_property
def mean(self):
pass
@functools.cached_property
def mode(self):
pass
@abstractmethod
def log_prob(self, x):
pass
@abstractmethod
def sample(self):
pass
class DiscretizedDistribution(DiscreteDistribution):
def __init__(self, num_bins, device):
self.num_bins = num_bins
self.bin_width = 2.0 / num_bins
self.half_bin_width = self.bin_width / 2.0
self.device = device
@functools.cached_property
def class_centres(self):
return torch.arange(self.half_bin_width - 1, 1, self.bin_width, device=self.device)
@functools.cached_property
def class_boundaries(self):
return torch.arange(self.bin_width - 1, 1 - self.half_bin_width, self.bin_width, device=self.device)
@functools.cached_property
def mean(self):
return (self.probs * self.class_centres).sum(-1)
@functools.cached_property
def mode(self):
mode_idx = self.probs.argmax(-1).flatten()
return self.class_centres[mode_idx].reshape(self.probs.shape[:-1])
class DiscretizedCtsDistribution(DiscretizedDistribution):
def __init__(self, cts_dist, num_bins, device, batch_dims, clip=True, min_prob=1e-5):
super().__init__(num_bins, device)
self.cts_dist = cts_dist
self.log_bin_width = log(self.bin_width)
self.batch_dims = batch_dims
self.clip = clip
self.min_prob = min_prob
@functools.cached_property
def probs(self):
bdry_cdfs = self.cts_dist.cdf(self.class_boundaries.reshape([-1] + ([1] * self.batch_dims)))
bdry_slice = bdry_cdfs[:1]
if self.clip:
cdf_min = torch.zeros_like(bdry_slice)
cdf_max = torch.ones_like(bdry_slice)
bdry_cdfs = torch.cat([cdf_min, bdry_cdfs, cdf_max], 0)
return (bdry_cdfs[1:] - bdry_cdfs[:-1]).moveaxis(0, -1)
else:
cdf_min = self.cts_dist.cdf(torch.zeros_like(bdry_slice) - 1)
cdf_max = self.cts_dist.cdf(torch.ones_like(bdry_slice))
bdry_cdfs = torch.cat([cdf_min, bdry_cdfs, cdf_max], 0)
cdf_range = cdf_max - cdf_min
cdf_mask = cdf_range < self.min_prob
cdf_range = torch.where(cdf_mask, (cdf_range * 0) + 1, cdf_range)
probs = (bdry_cdfs[1:] - bdry_cdfs[:-1]) / cdf_range
probs = torch.where(cdf_mask, (probs * 0) + (1 / self.num_bins), probs)
return probs.moveaxis(0, -1)
def prob(self, x):
class_idx = float_to_idx(x, self.num_bins)
centre = idx_to_float(class_idx, self.num_bins)
cdf_lo = self.cts_dist.cdf(centre - self.half_bin_width)
cdf_hi = self.cts_dist.cdf(centre + self.half_bin_width)
if self.clip:
cdf_lo = torch.where(class_idx <= 0, torch.zeros_like(centre), cdf_lo)
cdf_hi = torch.where(class_idx >= (self.num_bins - 1), torch.ones_like(centre), cdf_hi)
return cdf_hi - cdf_lo
else:
cdf_min = self.cts_dist.cdf(torch.zeros_like(centre) - 1)
cdf_max = self.cts_dist.cdf(torch.ones_like(centre))
cdf_range = cdf_max - cdf_min
cdf_mask = cdf_range < self.min_prob
cdf_range = torch.where(cdf_mask, (cdf_range * 0) + 1, cdf_range)
prob = (cdf_hi - cdf_lo) / cdf_range
return torch.where(cdf_mask, (prob * 0) + (1 / self.num_bins), prob)
def log_prob(self, x):
prob = self.prob(x)
return torch.where(
prob < self.min_prob,
self.cts_dist.log_prob(quantize(x, self.num_bins)) + self.log_bin_width,
safe_log(prob),
)
def sample(self, sample_shape=torch.Size([])):
if self.clip:
return quantize(self.cts_dist.sample(sample_shape), self.num_bins)
else:
assert hasattr(self.cts_dist, "icdf")
cdf_min = self.cts_dist.cdf(torch.zeros_like(self.cts_dist.mean) - 1)
cdf_max = self.cts_dist.cdf(torch.ones_like(cdf_min))
u = Uniform(cdf_min, cdf_max, validate_args=False).sample(sample_shape)
cts_samp = self.cts_dist.icdf(u)
return quantize(cts_samp, self.num_bins)
class GMM(MixtureSameFamily):
def __init__(self, mix_wt_logits, means, std_devs):
mix_wts = torch_Categorical(logits=mix_wt_logits, validate_args=False)
components = Normal(means, std_devs, validate_args=False)
super().__init__(mix_wts, components, validate_args=False)
class DiscretizedGMM(DiscretizedCtsDistribution):
def __init__(self, params, num_bins, clip=False, min_std_dev=1e-3, max_std_dev=10, min_prob=1e-5, log_dev=True):
assert params.size(-1) % 3 == 0
if min_std_dev < 0:
min_std_dev = 1.0 / (num_bins * 5)
mix_wt_logits, means, std_devs = params.chunk(3, -1)
if log_dev:
std_devs = safe_exp(std_devs)
std_devs = std_devs.clamp(min=min_std_dev, max=max_std_dev)
super().__init__(
cts_dist=GMM(mix_wt_logits, means, std_devs),
num_bins=num_bins,
device=params.device,
batch_dims=params.ndim - 1,
clip=clip,
min_prob=min_prob,
)
class DiscretizedNormal(DiscretizedCtsDistribution):
def __init__(self, params, num_bins, clip=False, min_std_dev=1e-3, max_std_dev=10, min_prob=1e-5, log_dev=True):
assert params.size(-1) == 2
if min_std_dev < 0:
min_std_dev = 1.0 / (num_bins * 5)
mean, std_dev = params.split(1, -1)[:2]
if log_dev:
std_dev = safe_exp(std_dev)
std_dev = std_dev.clamp(min=min_std_dev, max=max_std_dev)
super().__init__(
cts_dist=Normal(mean.squeeze(-1), std_dev.squeeze(-1), validate_args=False),
num_bins=num_bins,
device=params.device,
batch_dims=params.ndim - 1,
clip=clip,
min_prob=min_prob,
)
class Bernoulli(DiscreteDistribution):
def __init__(self, logits):
self.bernoulli = torch_Bernoulli(logits=logits, validate_args=False)
@functools.cached_property
def probs(self):
p = self.bernoulli.probs.unsqueeze(-1)
return torch.cat([1 - p, p], -1)
@functools.cached_property
def mode(self):
return self.bernoulli.mode
def log_prob(self, x):
return self.bernoulli.log_prob(x.float())
def sample(self, sample_shape=torch.Size([])):
return self.bernoulli.sample(sample_shape)
class DiscretizedBernoulli(DiscretizedDistribution):
def __init__(self, logits):
super().__init__(2, logits.device)
self.bernoulli = torch_Bernoulli(logits=logits, validate_args=False)
@functools.cached_property
def probs(self):
p = self.bernoulli.probs.unsqueeze(-1)
return torch.cat([1 - p, p], -1)
@functools.cached_property
def mode(self):
return idx_to_float(self.bernoulli.mode, 2)
def log_prob(self, x):
return self.bernoulli.log_prob(float_to_idx(x, 2).float())
def sample(self, sample_shape=torch.Size([])):
return idx_to_float(self.bernoulli.sample(sample_shape), 2)
class DeltaDistribution(CtsDistribution):
def __init__(self, mean, clip_range=1.0):
if clip_range > 0:
mean = mean.clip(min=-clip_range, max=clip_range)
self.mean = mean
@functools.cached_property
def mode(self):
return self.mean
@functools.cached_property
def mean(self):
return self.mean
def sample(self, sample_shape=torch.Size([])):
return self.mean
class Categorical(DiscreteDistribution):
def __init__(self, logits):
self.categorical = torch_Categorical(logits=logits, validate_args=False)
self.n_classes = logits.size(-1)
@functools.cached_property
def probs(self):
return self.categorical.probs
@functools.cached_property
def mode(self):
return self.categorical.mode
def log_prob(self, x):
return self.categorical.log_prob(x)
def sample(self, sample_shape=torch.Size([])):
return self.categorical.sample(sample_shape)
class DiscretizedCategorical(DiscretizedDistribution):
def __init__(self, logits=None, probs=None):
assert (logits is not None) or (probs is not None)
if logits is not None:
super().__init__(logits.size(-1), logits.device)
self.categorical = torch_Categorical(logits=logits, validate_args=False)
else:
super().__init__(probs.size(-1), probs.device)
self.categorical = torch_Categorical(probs=probs, validate_args=False)
@functools.cached_property
def probs(self):
return self.categorical.probs
@functools.cached_property
def mode(self):
return idx_to_float(self.categorical.mode, self.num_bins)
def log_prob(self, x):
return self.categorical.log_prob(float_to_idx(x, self.num_bins))
def sample(self, sample_shape=torch.Size([])):
return idx_to_float(self.categorical.sample(sample_shape), self.num_bins)
class CtsDistributionFactory:
@abstractmethod
def get_dist(self, params: torch.Tensor, input_params=None, t=None) -> CtsDistribution:
"""Note: input_params and t are not used but kept here to be consistency with DiscreteDistributionFactory."""
pass
class GMMFactory(CtsDistributionFactory):
def __init__(self, min_std_dev=1e-3, max_std_dev=10, log_dev=True):
self.min_std_dev = min_std_dev
self.max_std_dev = max_std_dev
self.log_dev = log_dev
def get_dist(self, params, input_params=None, t=None):
mix_wt_logits, means, std_devs = params.chunk(3, -1)
if self.log_dev:
std_devs = safe_exp(std_devs)
std_devs = std_devs.clamp(min=self.min_std_dev, max=self.max_std_dev)
return GMM(mix_wt_logits, means, std_devs)
class NormalFactory(CtsDistributionFactory):
def __init__(self, min_std_dev=1e-3, max_std_dev=10):
self.min_std_dev = min_std_dev
self.max_std_dev = max_std_dev
def get_dist(self, params, input_params=None, t=None):
mean, log_std_dev = params.split(1, -1)[:2]
std_dev = safe_exp(log_std_dev).clamp(min=self.min_std_dev, max=self.max_std_dev)
return Normal(mean.squeeze(-1), std_dev.squeeze(-1), validate_args=False)
class DeltaFactory(CtsDistributionFactory):
def __init__(self, clip_range=1.0):
self.clip_range = clip_range
def get_dist(self, params, input_params=None, t=None):
return DeltaDistribution(params.squeeze(-1), self.clip_range)
class DiscreteDistributionFactory:
@abstractmethod
def get_dist(self, params: torch.Tensor, input_params=None, t=None) -> DiscreteDistribution:
"""Note: input_params and t are only required by PredDistToDataDistFactory."""
pass
class BernoulliFactory(DiscreteDistributionFactory):
def get_dist(self, params, input_params=None, t=None):
return Bernoulli(logits=params.squeeze(-1))
class CategoricalFactory(DiscreteDistributionFactory):
def get_dist(self, params, input_params=None, t=None):
return Categorical(logits=params)
class DiscretizedBernoulliFactory(DiscreteDistributionFactory):
def get_dist(self, params, input_params=None, t=None):
return DiscretizedBernoulli(logits=params.squeeze(-1))
class DiscretizedCategoricalFactory(DiscreteDistributionFactory):
def get_dist(self, params, input_params=None, t=None):
return DiscretizedCategorical(logits=params)
class DiscretizedGMMFactory(DiscreteDistributionFactory):
def __init__(self, num_bins, clip=True, min_std_dev=1e-3, max_std_dev=10, min_prob=1e-5, log_dev=True):
self.num_bins = num_bins
self.clip = clip
self.min_std_dev = min_std_dev
self.max_std_dev = max_std_dev
self.min_prob = min_prob
self.log_dev = log_dev
def get_dist(self, params, input_params=None, t=None):
return DiscretizedGMM(
params,
num_bins=self.num_bins,
clip=self.clip,
min_std_dev=self.min_std_dev,
max_std_dev=self.max_std_dev,
min_prob=self.min_prob,
log_dev=self.log_dev,
)
class DiscretizedNormalFactory(DiscreteDistributionFactory):
def __init__(self, num_bins, clip=True, min_std_dev=1e-3, max_std_dev=10, min_prob=1e-5, log_dev=True):
self.num_bins = num_bins
self.clip = clip
self.min_std_dev = min_std_dev
self.max_std_dev = max_std_dev
self.min_prob = min_prob
self.log_dev = log_dev
def get_dist(self, params, input_params=None, t=None):
return DiscretizedNormal(
params,
num_bins=self.num_bins,
clip=self.clip,
min_std_dev=self.min_std_dev,
max_std_dev=self.max_std_dev,
min_prob=self.min_prob,
log_dev=self.log_dev,
)
def noise_pred_params_to_data_pred_params(noise_pred_params: torch.Tensor, input_mean: torch.Tensor, t: torch.Tensor, min_variance: float, min_t=1e-6):
"""Convert output parameters that predict the noise added to data, to parameters that predict the data."""
data_shape = list(noise_pred_params.shape)[:-1]
noise_pred_params = sandwich(noise_pred_params)
input_mean = input_mean.flatten(start_dim=1)
if torch.is_tensor(t):
t = t.flatten(start_dim=1)
else:
t = (input_mean * 0) + t
alpha_mask = (t < min_t).unsqueeze(-1)
posterior_var = torch.pow(min_variance, t.clamp(min=min_t))
gamma = 1 - posterior_var
A = (input_mean / gamma).unsqueeze(-1)
B = (posterior_var / gamma).sqrt().unsqueeze(-1)
data_pred_params = []
if noise_pred_params.size(-1) == 1:
noise_pred_mean = noise_pred_params
elif noise_pred_params.size(-1) == 2:
noise_pred_mean, noise_pred_log_dev = noise_pred_params.chunk(2, -1)
else:
assert noise_pred_params.size(-1) % 3 == 0
mix_wt_logits, noise_pred_mean, noise_pred_log_dev = noise_pred_params.chunk(3, -1)
data_pred_params.append(mix_wt_logits)
data_pred_mean = A - (B * noise_pred_mean)
data_pred_mean = torch.where(alpha_mask, 0 * data_pred_mean, data_pred_mean)
data_pred_params.append(data_pred_mean)
if noise_pred_params.size(-1) >= 2:
noise_pred_dev = safe_exp(noise_pred_log_dev)
data_pred_dev = B * noise_pred_dev
data_pred_dev = torch.where(alpha_mask, 1 + (0 * data_pred_dev), data_pred_dev)
data_pred_params.append(data_pred_dev)
data_pred_params = torch.cat(data_pred_params, -1)
data_pred_params = data_pred_params.reshape(data_shape + [-1])
return data_pred_params
class PredDistToDataDistFactory(DiscreteDistributionFactory):
def __init__(self, data_dist_factory, min_variance, min_t=1e-6):
self.data_dist_factory = data_dist_factory
self.data_dist_factory.log_dev = False
self.min_variance = min_variance
self.min_t = min_t
def get_dist(self, params, input_params, t):
data_pred_params = noise_pred_params_to_data_pred_params(params, input_params[0], t, self.min_variance, self.min_t)
return self.data_dist_factory.get_dist(data_pred_params)