-
Notifications
You must be signed in to change notification settings - Fork 1
/
samplers.py
145 lines (121 loc) · 4.26 KB
/
samplers.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
import torch
from torch.distributions import MultivariateNormal as MNormal
from torch.distributions import Categorical, Normal
from tqdm.notebook import tqdm, trange
import numpy as np
from pyro.infer import HMC as pyro_hmc
from pyro.infer import MCMC
from pyro.infer import NUTS as pyro_nuts
def ISIR(start: torch.FloatTensor,
target,
proposal,
n_samples: int,
burn_in: int,
*,
n_particles: int,
verbose: bool=False):
"""
Iterated Sampling Importance Resampling
Args:
start - strating points of shape [n_chains x dim]
target - target distribution instance with method "log_prob"
proposal - proposal distribution instance with methods "log_prob" and "sample"
n_samples - number of last samples from each chain to return
burn_in - number of first samples from each chain to throw away
n_particles - number of particles including one from previous step
verbose - whether to show iterations' bar
Returns:
tensor of chains with shape [n_samples, n_chains, dim], acceptance rates for each iteration
"""
chains = []
acceptance_rate = []
x = start.clone()
logp_x = target.log_prob(x)
logq_x = proposal.log_prob(x)
n_chains, dim = x.size()
range_ = trange if verbose else range
for step_id in range_(n_samples + burn_in):
particles = proposal.sample((x.shape[0], n_particles - 1))
logqs = torch.cat([logq_x[:, None], proposal.log_prob(particles)], 1)
logps = torch.cat([logp_x[:, None], target.log_prob(particles.reshape(-1, dim)).reshape(n_chains, -1)], 1)
#logps = torch.cat([logp_x[:, None], target.log_prob(particles)], 1)
particles = torch.cat([x[:, None, :], particles], 1)
log_weights = logps - logqs
indices = Categorical(logits=log_weights).sample()
x = particles[np.arange(x.shape[0]), indices]
logp_x = logps[np.arange(x.shape[0]), indices]
logq_x = logqs[np.arange(x.shape[0]), indices]
acceptance_rate.append((indices != 0).float().mean().item())
if step_id >= burn_in:
chains.append(x.detach().data.clone())
chains = torch.stack(chains, 0)
return chains, acceptance_rate
def NUTS(start,
target,
n_samples: int,
burn_in: int,
*,
verbose: bool = False) -> torch.FloatTensor:
x = start.clone()
x.requires_grad_(False)
def energy(z):
z = z["points"]
return -target.log_prob(z).sum()
kernel = pyro_nuts(potential_fn=energy, full_mass=False)
init_params = {"points": x}
mcmc_true = MCMC(
kernel=kernel,
num_samples=n_samples,
initial_params=init_params,
warmup_steps=burn_in,
)
mcmc_true.run()
q_true = mcmc_true.get_samples(group_by_chain=True)["points"]
samples_true = q_true.view(-1, *start.shape)
return samples_true, _
def HMC(
start,
target,
n_samples: int,
burn_in: int,
*,
step_size: float,
num_leapfrog_steps: float = 1,
verbose: bool = False,
) -> torch.FloatTensor:
"""
Hamiltonian Monte Carlo
Args:
start - strating points of shape [n_chains x dim]
target - target distribution instance with method "log_prob"
n_samples - number of last samples from each chain to return
burn_in - number of first samples from each chain to throw away
step_size - step size for drift term
verbose - whether to show iterations' bar
Returns:
tensor of chains with shape [n_samples, n_chains, dim], acceptance rates
for each iteration
"""
x = start.clone().detach()
#print("X is leaf? ", x.is_leaf)
x.requires_grad_(False)
def energy(z):
z = z["points"]
return -target.log_prob(z).sum()
kernel = pyro_hmc(
potential_fn=energy,
step_size=step_size,
num_steps=num_leapfrog_steps,
full_mass=False,
)
init_params = {"points": x}
mcmc_true = MCMC(
kernel=kernel,
num_samples=n_samples,
initial_params=init_params,
warmup_steps=burn_in,
)
mcmc_true.run()
q_true = mcmc_true.get_samples(group_by_chain=True)["points"]
samples_true = q_true.view(-1, *start.shape)
return samples_true