-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBBSVI.py
More file actions
523 lines (375 loc) · 20.1 KB
/
BBSVI.py
File metadata and controls
523 lines (375 loc) · 20.1 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import torch
import operator
import numpy as np
from collections import defaultdict
class SVI():
'''Class for black box stochastic variational inference
https://arxiv.org/abs/1401.0118
'''
def __init__(self, data, prior_distr, var_distr, opt, scheduler=None):
'''Initialization
Args:
data: oserved data
prior_distr: class for prior probabilistic model
var_distr: class for variational distribution
opt: optimizer
scheduler: scheduler for an optimizer
'''
self.data = data
self.prior_distr = prior_distr
self.var_distr = var_distr
self.opt = opt
self.scheduler = scheduler
def check_methods(self, loss):
"""Check if provided model supports loss
Args:
loss: string, name of loss
Returns:
flag: boolean, if model supports loss
message: string, error source if model does not support loss
methods_to_implement: dict, methods to implement if model does not support loss
"""
methods = {
'bb1': {'prior_distr' : ['log_likelihood_global', 'log_likelihood_local', 'log_likelihood_joint'],
'var_distr' : ['log_likelihood_global', 'log_likelihood_local', 'sample_global', 'sample_local']},
'bb2': {'prior_distr' : ['log_likelihood_global', 'log_likelihood_local', 'log_likelihood_joint'],
'var_distr' : ['log_likelihood_global', 'log_likelihood_local', 'sample_global', 'sample_local',
'global_parameters', 'local_parameters']},
'entropy': {'prior_distr' : ['log_likelihood_global', 'log_likelihood_joint'],
'var_distr' : ['entropy', 'sample_global', 'sample_local']},
'kl': {'prior_distr' : ['log_likelihood_cond'],
'var_distr' : ['sample_global', 'sample_local']}
}
flag = True
message = "OK"
methods_to_implement = defaultdict(list)
try:
cur_loss = methods[loss]
for prior_method in cur_loss['prior_distr']:
if not hasattr(self.prior_distr, prior_method):
methods_to_implement['prior_distr'].append(prior_method)
for var_method in cur_loss['var_distr']:
if not hasattr(self.var_distr, var_method):
methods_to_implement['var_distr'].append(var_method)
except KeyError:
flag = False
message = "We do not support this loss: {0}".format(loss)
if methods_to_implement:
flag = False
message = "The following methods should be implemented:"
return flag, message, methods_to_implement
def make_inference(self, num_steps=100, num_samples=10, batch_size=10,
loss='bb1', discounter_schedule=None, kl=None,
shuffle=False, print_progress=True, callback=None,
retain_graph=False):
'''Making SVI
Args:
num_steps: int, maximum number of epoches
tol: required tolerance
num_samples: int, number of samples used for ELBO approximation
batch_size: int, size of one batch
loss: string, loss function, currently avaliable bb1 or bb2
discounter_schedule: used only for 'entropy' loss, None or torch tensor
of size num_steps, discounter_schedule[i] is a discounter for an
analytically-computed term at step i
kl: None or callable, compute KL divergency between variational and prior distributions,
required only for 'kl' loss
shuffle: boolean, if batch is shuffled every epoch or not
print_progress: boolean, if True then progrss bar is printed
callback: None or callable, if not None, applied to loss after every iteration
retain_graph: boolean, passed to loss.backward()
'''
flag, message, methods_to_implement = self.check_methods(loss)
if not flag:
raise Exception(message + '\n' + \
'\n'.join([key + ':\t' + \
', '.join(methods_to_implement[key]) for key in methods_to_implement.keys()]))
kwargs = lambda x: {}
if loss == 'bb1':
loss_func = self.bb1_loss_
elif loss == 'bb2':
loss_func = self.bb2_loss_
elif loss == 'entropy':
loss_func = self.entropy_form_loss_
if discounter_schedule is not None:
kwargs = lambda x: {'discounter': discounter_schedule[x]}
elif loss == 'kl':
loss_func = self.kl_form_loss_
if kl is not None:
if discounter_schedule is not None:
kwargs = lambda x: {'kl': kl, 'discounter': discounter_schedule[x]}
else:
kwargs = lambda x: {'kl': kl}
else:
raise Exception('You should provide a function computing KL-divergency to use \'kl\' loss')
for step in range(num_steps):
if shuffle:
indices = np.random.choice(self.data.shape[0], self.data.shape[0], False)
else:
indices = np.arange(self.data.shape[0])
indices = np.split(indices, np.arange(batch_size, self.data.shape[0], batch_size))
for batch_indices in indices:
self.opt.zero_grad()
loss = loss_func(num_samples, batch_indices, **kwargs(step))
if callback is not None:
callback(loss)
loss.backward(retain_graph=retain_graph)
self.opt.step()
if self.scheduler is not None:
self.scheduler.step()
if print_progress:
if (int(100 * step / num_steps) != int(100 * (step - 1) / num_steps)):
print('.', end='')
if print_progress:
print()
def bb1_loss_(self, num_samples, batch_indices):
'''Computing loss of BB SVI 1
prior_distr requred methods:
log_likelihood_global(beta)
log_likelihood_local(z, beta)
log_likelihood_joint(x, z, beta)
var_distr required methods:
log_likelihood_global(beta)
log_likelihood_local(z, idx)
sample_global()
sample_local(beta, idx)
Args:
num_samples: number of samples used for approximation
batch_indices: indices of batch
Returns:
loss: Black Box loss function
'''
global_loss = torch.zeros(1, requires_grad=True)
local_loss = torch.zeros(1, requires_grad=True)
for i in range(num_samples):
beta = self.var_distr.sample_global()
global_const_term = torch.zeros(1, requires_grad=True)
for idx in batch_indices:
x = self.data[idx]
z = self.var_distr.sample_local(beta, idx)
local_const_term = self.prior_distr.log_likelihood_local(z, beta) + \
self.prior_distr.log_likelihood_joint(x, z, beta) - \
self.var_distr.log_likelihood_local(z, idx).data
local_const_term = local_const_term * self.data.shape[0]
local_var_term = self.var_distr.log_likelihood_local(z, idx)
local_loss = local_loss + local_var_term * local_const_term
global_const_term = global_const_term + \
self.prior_distr.log_likelihood_local(z, beta) + \
self.prior_distr.log_likelihood_joint(x, z, beta)
global_const_term = global_const_term * self.data.shape[0] / batch_indices.shape[0]
global_const_term = global_const_term + \
self.prior_distr.log_likelihood_global(beta) - \
self.var_distr.log_likelihood_global(beta).data
global_var_term = self.var_distr.log_likelihood_global(beta)
global_loss = global_loss + global_var_term * global_const_term
loss = -(global_loss + local_loss) / num_samples
return loss
def bb2_loss_(self, num_samples, batch_indices):
'''Computing loss of BB SVI 2, which has lower variance compare to BB SVI 1
prior_distr requred methods:
log_likelihood_global(beta)
log_likelihood_local(z, beta)
log_likelihood_joint(x, z, beta)
var_distr required methods:
log_likelihood_global(beta)
log_likelihood_local(z, idx)
sample_global()
sample_local(beta, idx)
var_distr required attributes:
global_parameters: list of global parameters
local_parameters: list of local parameters,
i-th entry corresponds to i-th latent variable
Args:
num_samples: number of samples used for approximation
batch_indices: indices of batch
Returns:
loss: Black Box loss function
'''
if num_samples == 1:
raise Exception('BB2 loss is avaliable only with num_samples > 1')
global_samples = [self.var_distr.sample_global() for _ in range(num_samples)]
global_h = [handle_nones(torch.autograd.grad(self.var_distr.log_likelihood_global(global_samples[s]),
self.var_distr.global_parameters,
retain_graph=True,
allow_unused=True)) for s in range(num_samples)]
local_samples = []
for idx in batch_indices:
local_samples.append([self.var_distr.sample_local(global_samples[s], idx) for s in range(num_samples)])
global_f = []
local_f = [[] for idx in batch_indices]
local_h = [[] for idx in batch_indices]
for s, beta in enumerate(global_samples):
multiplier = torch.zeros(1, requires_grad=False)
for i, idx in enumerate(batch_indices):
multiplier += self.prior_distr.log_likelihood_local(local_samples[i][s], beta) + \
self.prior_distr.log_likelihood_joint(self.data[idx], local_samples[i][s], beta)
local_h[i].append(handle_nones(torch.autograd.grad(self.var_distr.log_likelihood_local(local_samples[i][s], idx),
self.var_distr.local_parameters[idx],
retain_graph=True,
allow_unused=True)))
local_multiplier = self.prior_distr.log_likelihood_local(local_samples[i][s], beta) + \
self.prior_distr.log_likelihood_joint(self.data[idx], local_samples[i][s], beta) - \
self.var_distr.log_likelihood_local(local_samples[i][s], idx)
local_f[i].append(tuple(local_multiplier.data * grad for grad in local_h[i][s]))
multiplier *= self.data.shape[0] / batch_indices.shape[0]
multiplier += self.prior_distr.log_likelihood_global(beta) - \
self.var_distr.log_likelihood_global(beta)
global_f.append(tuple(multiplier.data * grad for grad in global_h[s]))
global_a = self.count_a_(global_h, global_f)
local_a = [self.count_a_(local_h[i], local_f[i]) for i, idx in enumerate(batch_indices)]
global_loss = torch.zeros(1, requires_grad=True)
local_loss = torch.zeros(1, requires_grad=True)
for s, beta in enumerate(global_samples):
global_const_term = torch.zeros(1, requires_grad=False)
for i, idx in enumerate(batch_indices):
x = self.data[idx]
z = local_samples[i][s]
local_const_term = self.prior_distr.log_likelihood_local(z, beta) + \
self.prior_distr.log_likelihood_joint(x, z, beta) - \
self.var_distr.log_likelihood_local(z, idx).data
local_const_term = local_const_term * self.data.shape[0]
local_const_term = local_const_term - local_a[i]
local_var_term = self.var_distr.log_likelihood_local(z, idx)
local_loss = local_loss + local_var_term * local_const_term
global_const_term = global_const_term + \
self.prior_distr.log_likelihood_local(z, beta) + \
self.prior_distr.log_likelihood_joint(x, z, beta)
global_const_term = global_const_term * self.data.shape[0] / batch_indices.shape[0]
global_const_term = global_const_term + \
self.prior_distr.log_likelihood_global(beta) - \
self.var_distr.log_likelihood_global(beta).data - \
global_a
global_var_term = self.var_distr.log_likelihood_global(beta)
global_loss = global_loss + global_var_term * global_const_term
loss = -(global_loss + local_loss) / num_samples
return loss
def entropy_form_loss_(self, num_samples, batch_indices, discounter=1):
'''Computing ELBO estimator in entropy form
prior_distr requred methods:
log_likelihood_global(beta)
log_likelihood_joint(x, z, beta)
var_distr required methods:
entropy(batch_indices)
sample_global()
sample_local(beta, idx)
Args:
num_samples: number of samples used for approximation
batch_indices: indices of batch
discounter: coefficient of entropy term
Returns:
loss: ELBO in entropy form estimator
'''
mc_term = torch.zeros(1, requires_grad=True)
for _ in range(num_samples):
beta = self.var_distr.sample_global()
sample_log_likelihood = torch.zeros(1, requires_grad=True)
for idx in batch_indices:
z = self.var_distr.sample_local(beta, idx)
sample_log_likelihood = sample_log_likelihood + \
self.prior_distr.log_likelihood_joint(self.data[idx], z, beta)
sample_log_likelihood = sample_log_likelihood * self.data.shape[0] / batch_indices.size
sample_log_likelihood = sample_log_likelihood + self.prior_distr.log_likelihood_global(beta)
mc_term = mc_term + sample_log_likelihood
mc_term = mc_term / num_samples
entropy_term = self.var_distr.entropy(batch_indices)
loss = -mc_term - discounter * entropy_term
return loss
def kl_form_loss_(self, num_samples, batch_indices, kl, discounter=1):
'''Computing ELBO estimator in Kullback–Leibler divergence form
prior_distr requred methods:
log_likelihood_cond(x, z, beta)
var_distr required methods:
sample_global()
sample_local(beta, idx)
Args:
num_samples: number of samples used for approximation
batch_indices: indices of batch
kl: callable, function which computes KL divergence beetween
variational and prior based on based indices
discounter: coefficient of KL term
Returns:
loss: ELBO in KL form estimator
'''
mc_term = torch.zeros(1, requires_grad=True)
for _ in range(num_samples):
beta = self.var_distr.sample_global()
sample_log_likelihood = torch.zeros(1, requires_grad=True)
for idx in batch_indices:
z = self.var_distr.sample_local(beta, idx)
sample_log_likelihood = sample_log_likelihood + \
self.prior_distr.log_likelihood_cond(self.data[idx], z, beta)
sample_log_likelihood = sample_log_likelihood * self.data.shape[0] / batch_indices.size
mc_term = mc_term + sample_log_likelihood
mc_term = mc_term / num_samples
kl_term = kl(self.var_distr, self.prior_distr, batch_indices)
loss = -mc_term + discounter * kl_term
return loss
def count_a_(self, h, f):
'''Given f and h from BB SVI II, computes a* based on an unbiased
estimators of its components covariances
Args:
h: h computed for S samples
f: f computed for S samples
Returns:
a: a* from BB SVI II, given by sum(cov(f_i, h_i)) / sum(var(h_i))
'''
num_samples = len(h)
h_means = tuple(torch.zeros(grad.shape, requires_grad=False) for grad in h[0])
for sample_grads in h:
h_means = tuple(map(operator.add, h_means, sample_grads))
h_means = tuple(grads / num_samples for grads in h_means)
f_means = tuple(torch.zeros(grad.shape, requires_grad=False) for grad in f[0])
for sample_grads in f:
f_means = tuple(map(operator.add, f_means, sample_grads))
f_means = tuple(grads / num_samples for grads in f_means)
h_var = tuple(torch.zeros(grad.shape, requires_grad=False) for grad in h[0])
f_h_cov = tuple(torch.zeros(grad.shape, requires_grad=False) for grad in h[0])
for s in range(num_samples):
h_term = tuple(map(operator.sub, h[s], h_means))
f_term = tuple(map(operator.sub, f[s], f_means))
h_var = tuple(map(operator.add, h_var, tuple(map(operator.mul, h_term, h_term))))
f_h_cov = tuple(map(operator.add, f_h_cov, tuple(map(operator.mul, h_term, f_term))))
h_var = tuple(var / (num_samples - 1) for var in h_var)
f_h_cov = tuple(cov / (num_samples - 1) for cov in f_h_cov)
var_term = sum(torch.sum(var) for var in h_var)
cov_term = sum(torch.sum(cov) for cov in f_h_cov)
if var_term == 0:
a = 0
else:
a = cov_term / var_term
return a
def handle_nones(container):
'''Replace all Nones with torch tensors containing one zero
Args:
container: tuple
Returns:
handled_container: container with torch tensor containing
one zero instead of Nones
'''
handled_container = tuple(item if item is not None else torch.zeros(1) for item in container)
return handled_container
class HistoryCollector():
'''A simple class which is helpful to collect loss history of SVI
Args:
data_size: int, number of data points
batch_size: int, batch_size for SVI
'''
def __init__(self, data_size, batch_size=10):
self.data_size = data_size
self.batch_size = batch_size
self.size = self.data_size // self.batch_size + bool(self.data_size % self.batch_size)
self.counter = 0
self.history = []
self.history_per_epoch = []
def collect_history(self, loss):
'''Saving loss, can be passed as callback arg to SVI.make_inference
Args:
loss: torch.tensor, loss
'''
self.history_per_epoch.append(float(loss))
self.counter += 1
if self.counter == self.size:
self.counter = 0
self.history.append(np.mean(self.history_per_epoch))
self.history_per_epoch = []
pass