-
Notifications
You must be signed in to change notification settings - Fork 2
/
infer.py
486 lines (435 loc) · 17.9 KB
/
infer.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import math
import pickle
import time
from typing import Any, Callable, Iterator, List, Tuple
import torch
from torch.distributions import Uniform, Laplace, Normal
from tqdm import tqdm
from ppl import ProbRun, T
torch.manual_seed(0) # makes executions deterministic
torch.set_printoptions(precision=10) # more precise printing for debugging
class State:
"""Describes a state in phase space (position q, momentum p) for NP-DHMC
The field `is_cont` stores which variables are continuous.
"""
def __init__(
self,
q: torch.Tensor,
p: torch.Tensor,
is_cont: torch.Tensor,
) -> None:
self.q = q
"""position"""
self.p = p
"""momentum"""
self.is_cont = is_cont
"""is_cont[i] == True if the density function is continuous in coordinate i.
If a branch (if-statement) in a program depends on self.q[i], it is discontinuous and is_cont[i] == False."""
def kinetic_energy(self) -> torch.Tensor:
"""Computes the kinetic energy of the particle.
In discontinuous HMC, discontinuous coordinates use Laplace momentum, not Gaussian momentum."""
gaussian = self.p * self.is_cont
laplace = self.p * ~self.is_cont
return gaussian.dot(gaussian) / 2 + torch.sum(torch.abs(laplace))
def importance_sample(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
count: int = 10_000,
) -> Iterator[Tuple[float, T]]:
"""Samples from a probabilistic program using importance sampling.
The resulting samples are weighted.
Note: This is not needed to reproduce the results, but hopefully makes the code easier to understand.
Args:
run_prog (Callable[[torch.Tensor], ProbRun[T]]): runs the probabilistic program on a trace.
count (int, optional): the desired number of samples. Defaults to 10_000.
Yields:
Iterator[Tuple[torch.Tensor, T]]: samples of the form (log_score, value)
"""
for _ in tqdm(range(count)):
result = run_prog(torch.tensor([]))
yield result.log_score.item(), result.value
def importance_resample(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
count: int = 10_000,
) -> Tuple[List[Tuple[float, T]], List[T]]:
"""Samples from a probabilistic program using importance sampling and systematic resampling.
It uses systematic resampling on the weighted importance samples to obtain unweighted samples.
Note: This is not needed to reproduce the results, but hopefully makes the code easier to understand.
Args:
run_prog (Callable[[torch.Tensor], ProbRun[T]]): runs the probabilistic program on a trace.
count (int, optional): the desired number of samples. Defaults to 10_000.
Returns:
Tuple[List[Tuple[float, T]], List[T]]: weighted samples, resamples
"""
weighted_samples = list(importance_sample(run_prog, count))
count = len(weighted_samples)
mx = max(log_weight for (log_weight, _) in weighted_samples)
weight_sum = sum(math.exp(log_weight - mx) for (log_weight, _) in weighted_samples)
# systematic resampling:
u_n = Uniform(0, 1).sample().item()
sum_acc = 0.0
resamples: List[T] = []
for (log_weight, value) in weighted_samples:
weight = math.exp(log_weight - mx) * count / weight_sum
sum_acc += weight
while u_n < sum_acc:
u_n += 1
resamples.append(value)
return weighted_samples, resamples
def coord_integrator(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
i: int,
t: float,
eps: float,
state: State,
state_0: State,
result: ProbRun,
) -> ProbRun[T]:
"""Coordinate integrator adapted from discontinuous HMC.
For NP-DHMC, it also has to deal with possible changes in dimension."""
U = -result.log_weight
q = state.q.clone().detach()
q[i] += eps * torch.sign(state.p[i])
new_result = run_prog(q)
new_U = -new_result.log_weight.item()
delta_U = new_U - U
if not math.isfinite(new_U) or torch.abs(state.p[i]) <= delta_U:
state.p[i] = -state.p[i]
else:
state.p[i] -= torch.sign(state.p[i]) * delta_U
N2 = new_result.len
N = result.len
result = new_result
if N2 > N:
# extend everything to the higher dimension
state.q = result.samples.clone().detach()
is_cont = result.is_cont.clone().detach()
# pad the momentum vector:
gauss = Normal(0, 1).sample([N2 - N])
laplace = Laplace(0, 1).sample([N2 - N])
p_padding = gauss * is_cont[N:N2] + laplace * ~is_cont[N:N2]
state_0.p = torch.cat((state_0.p, p_padding))
state_0.is_cont = torch.cat((state_0.is_cont, is_cont[N:N2]))
state.p = torch.cat((state.p, p_padding))
state.is_cont = is_cont
# adjust the position vector:
q0_padding = (
state.q[N:N2].clone().detach()
- t * state.p[N:N2] * is_cont[N:N2]
- t * torch.sign(state.p[N:N2]) * ~is_cont[N:N2]
)
state_0.q = torch.cat((state_0.q, q0_padding))
else:
# truncate everything to the lower dimension
state.q = result.samples[:N2].clone().detach()
state.p = state.p[:N2]
state.is_cont = result.is_cont[:N2]
state_0.q = state_0.q[:N2]
state_0.p = state_0.p[:N2]
state_0.is_cont = state_0.is_cont[:N2]
assert len(state.p) == len(state_0.p)
assert len(state.p) == len(state.q)
assert len(state.is_cont) == len(state.p)
assert len(state_0.is_cont) == len(state_0.p)
assert len(state_0.p) == len(state_0.q)
return result
def integrator_step(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
t: float,
eps: float,
state: State,
state_0: State,
) -> ProbRun[T]:
"""Performs one integrator step (called "leapfrog step" in standard HMC)."""
result = run_prog(state.q)
# first half of leapfrog step for continuous variables:
state.p = state.p - eps / 2 * result.gradU() * state.is_cont
state.q = state.q + eps / 2 * state.p * state.is_cont
result = run_prog(state.q)
# Integrate the discontinuous coordinates in a random order:
disc_indices = torch.flatten(torch.nonzero(~state.is_cont, as_tuple=False))
perm = torch.randperm(len(disc_indices))
disc_indices_permuted = disc_indices[perm]
for j in disc_indices_permuted:
if j >= len(state.q):
continue # out-of-bounds can happen if q changes length during the loop
result = coord_integrator(
run_prog, int(j.item()), t, eps, state, state_0, result
)
# second half of leapfrog step for continuous variables
state.q = state.q + eps / 2 * state.p * state.is_cont
result = run_prog(state.q)
state.p = state.p - eps / 2 * result.gradU() * state.is_cont
return result
def np_dhmc(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
count: int,
leapfrog_steps: int,
eps: float,
burnin: int = None,
) -> List[T]:
"""Samples from a probabilistic program using NP-DHMC.
Args:
run_prog (Callable[[torch.Tensor], ProbRun[T]]): runs the probabilistic program on a trace.
count (int, optional): the desired number of samples. Defaults to 10_000.
burnin (int): number of samples to discard at the start. Defaults to `count // 10`.
leapfrog_steps (int): number of leapfrog steps the integrator performs.
eps (float): the step size of the leapfrog steps.
Returns:
List[T]: list of samples
"""
if burnin is None:
burnin = count // 10
final_samples = []
result = run_prog(torch.tensor([]))
U = -result.log_weight
q = result.samples.clone().detach()
is_cont = result.is_cont.clone().detach()
count += burnin
accept_count = 0
for _ in tqdm(range(count)):
N = len(q)
dt = ((torch.rand(()) + 0.5) * eps).item()
gaussian = Normal(0, 1).sample([N]) * is_cont
laplace = Laplace(0, 1).sample([N]) * ~is_cont
p = gaussian + laplace
state_0 = State(q, p, is_cont)
state = State(q, p, is_cont)
prev_res = result
for step in range(leapfrog_steps):
if not math.isfinite(result.log_weight.item()):
break
result = integrator_step(run_prog, step * dt, dt, state, state_0)
# Note that the acceptance probability differs from the paper in the following way:
# In the implementation, we can have other continuous distributions than
# just normal distributions.
# We treat `x = sample(D)` as `x = sample(normal); score(pdf_D(x) / pdf_normal(x));`.
# this means that the `w(q) * pdfnormal(q)` in the acceptance probability just becomes
# `w(q) * pdf_D(q) = weight(q)`
# because the weight in the implementation includes the prior.
# (`w` refers to the weight as defined in the paper and
# `weight` to the weight as used in the implementation.)
# Similarly
# w(q0 after extension) * pdfnormal(q0 after extension)
# = w(q0 before extension) * pdfnormal(q0 before extension) * pdfnormal(extended part of q0)
# = weight(q0 before extensions) * pdfnormal(extended part of q0)
# For this reason, we add the factor pdfnormal(extended part of q0) in U_0 below.
K_0 = state_0.kinetic_energy()
N2 = len(state_0.q)
U_0 = -prev_res.log_weight - Normal(0, 1).log_prob(state_0.q[N:N2]).sum()
K = state.kinetic_energy()
U = -result.log_weight
accept_prob = torch.exp(U_0 + K_0 - U - K)
if U.item() != math.inf and torch.rand(()) < accept_prob:
q = state.q
is_cont = state.is_cont
accept_count += 1
final_samples.append(result.value)
else:
result = prev_res
final_samples.append(prev_res.value)
count = len(final_samples)
final_samples = final_samples[burnin:] # discard first samples (burn-in)
print(f"acceptance ratio: {accept_count / count * 100}%")
return final_samples
def np_lookahead_dhmc(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
count: int,
L: int,
eps: float,
K: int = 0,
alpha: float = 1,
burnin: int = None,
) -> Tuple[List[T], Any]:
"""Samples from a probabilistic program using "Lookahead" NP-DHMC.
Returns a list of samples and additional information, together as a pair.
The acceptance condition is taken from [1] (Figure 3).
They prove that it's equivalent to Sohl-Dickstein et al.'s version in Appendix C.
[1] Campos, Sanz-Serna: Extra Chance Generalized Hybrid Monte Carlo (https://arxiv.org/pdf/1407.8107.pdf)
Args:
run_prog (Callable[[torch.Tensor], ProbRun[T]]): runs the probabilistic program on a trace.
count (int, optional): the desired number of samples. Defaults to 10_000.
L (int): number of leapfrog steps the integrator performs.
eps (float): the step size of the leapfrog steps.
K (int): number of "extra chances" for Lookahead HMC (0: standard HMC)
alpha (float): persistence factor (0: full persistence, 1: stanard HMC)
burnin (int): number of samples to discard at the start. Defaults to `count // 10`.
Returns:
List[T], Any: list of samples, stats
"""
if burnin is None:
burnin = count // 10
final_samples = []
lookahead_stats = [0] * (K + 2)
result = run_prog(torch.tensor([]))
q = result.samples.clone().detach()
N = len(q)
is_cont = result.is_cont.clone().detach()
gaussian = Normal(0, 1).sample([N]) * is_cont
laplace = Laplace(0, 1).sample([N]) * ~is_cont
p = gaussian + laplace
count += burnin
accept_count = 0
for _ in tqdm(range(count)):
N = len(q)
dt = ((torch.rand(()) + 0.5) * eps).item()
p_cont = p * math.sqrt(1 - alpha * alpha) + Normal(0, alpha).sample([N])
p_disc = p * math.sqrt(1 - alpha * alpha) + Laplace(0, alpha).sample([N])
p = p_cont * is_cont + p_disc * ~is_cont
state_0 = State(q, p, is_cont)
state = State(q, p, is_cont)
prev_res = result
rand_uniform = torch.rand(())
for k in range(K + 1):
for step in range(L):
if not math.isfinite(result.log_weight.item()):
break
result = integrator_step(run_prog, step * dt, dt, state, state_0)
# Note that the acceptance probability differs from the paper in the following way:
# In the implementation, we can have other continuous distributions than
# just normal distributions.
# We treat `x = sample(D)` as `x = sample(normal); score(pdf_D(x) / pdf_normal(x));`.
# this means that the `w(q) * pdfnormal(q)` in the acceptance probability just becomes
# `w(q) * pdf_D(q) = weight(q)`
# because the weight in the implementation includes the prior.
# (`w` refers to the weight as defined in the paper and
# `weight` to the weight as used in the implementation.)
# Similarly
# w(q0 after extension) * pdfnormal(q0 after extension)
# = w(q0 before extension) * pdfnormal(q0 before extension) * pdfnormal(extended part of q0)
# = weight(q0 before extensions) * pdfnormal(extended part of q0)
# For this reason, we add the factor pdfnormal(extended part of q0) in U_0 below.
K_0 = state_0.kinetic_energy()
N2 = len(state_0.q)
U_0 = -prev_res.log_weight - Normal(0, 1).log_prob(state_0.q[N:N2]).sum()
K_new = state.kinetic_energy()
U_new = -result.log_weight
accept_prob = torch.exp(U_0 + K_0 - U_new - K_new)
if U_new.item() != math.inf and rand_uniform < accept_prob:
q = state.q
p = state.p
is_cont = state.is_cont
accept_count += 1
final_samples.append(result.value)
lookahead_stats[k + 1] += 1
break
else: # if we didn't accept the loop and exit before
result = prev_res
p = -p # momentum flip
final_samples.append(prev_res.value)
lookahead_stats[0] += 1
count = len(final_samples)
final_samples = final_samples[burnin:] # discard first samples (burn-in)
print(f"acceptance ratio: {accept_count / count * 100}%")
print(f"lookahead stats: {lookahead_stats}")
return final_samples, lookahead_stats
def run_inference(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
name: str,
count: int,
eps: float,
leapfrog_steps: int,
burnin: int = None,
seed: int = None,
**kwargs,
) -> dict:
"""Runs importance sampling and NP-DHMC, then saves the samples to a .pickle file.
The file is located in the `samples_produced/` folder.
Note: This is not needed to reproduce the results, but hopefully makes the code easier to understand.
"""
def run(sampler: Callable) -> dict:
if seed is not None:
torch.manual_seed(seed)
start = time.time()
results = sampler()
stop = time.time()
elapsed = stop - start
return {
"time": elapsed,
"samples": results,
}
adjusted_count = count * leapfrog_steps
samples = {}
print("Running NP-DHMC...")
samples["hmc"] = run(
lambda: np_dhmc(
run_prog,
count=count,
eps=eps,
leapfrog_steps=leapfrog_steps,
burnin=burnin,
**kwargs,
),
)
samples["hmc"]["burnin"] = burnin
samples["hmc"]["eps"] = eps
samples["hmc"]["leapfrog_steps"] = leapfrog_steps
print("Running importance sampling...")
samples["is"] = run(
lambda: importance_resample(run_prog, count=adjusted_count),
)
weighted, values = samples["is"]["samples"]
samples["is"]["samples"] = values
samples["is"]["weighted"] = weighted
filename = f"{name}__count{count}_eps{eps}_leapfrogsteps{leapfrog_steps}"
samples["filename"] = filename
with open(f"samples_produced/{filename}.pickle", "wb") as f:
pickle.dump(samples, f)
return samples
def run_inference_icml2022(
run_prog: Callable[[torch.Tensor], ProbRun[T]],
name: str,
count: int,
eps: float,
L: int,
K: int = 0,
alpha: float = 1,
burnin: int = None,
seed: int = None,
**kwargs,
) -> dict:
"""Runs NP-LA-DHMC with persistence, then saves the samples to a .pickle file.
The file is located in the `samples_produced/` folder.
Note: This is not needed to reproduce the results, but hopefully makes the code easier to understand.
"""
def run(sampler: Callable) -> dict:
if seed is not None:
torch.manual_seed(seed)
start = time.time()
results, stats = sampler()
stop = time.time()
elapsed = stop - start
return {
"time": elapsed,
"samples": results,
"stats": stats,
}
# adjusted_count = count * L
samples = {}
# NP-LA-DHMC with persistence
# print(f"Running NP-Lookahead-DHMC with (L = {L}, α = {alpha}, K = {K})...")
persistentstr = "" if alpha == 1.0 else "-persistent"
lastr = "" if K == 0 else "la"
method = f"np{lastr}dhmc{persistentstr}"
samples[method] = run(
lambda: np_lookahead_dhmc(
run_prog,
count=count,
eps=eps,
L=L,
K=K,
burnin=burnin,
alpha=alpha,
**kwargs,
)
)
samples[method]["burnin"] = burnin
samples[method]["eps"] = eps
samples[method]["L"] = L
if alpha != 1.0:
samples[method]["alpha"] = alpha
if K != 0:
samples[method]["K"] = K
filename = f"{name}__count{count}_eps{eps}_L{L}_alpha{alpha}_K{K}"
with open(f"lookahead_samples/{filename}.pickle", "wb") as f:
pickle.dump(samples, f)
return samples