-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathanneal_samplers.py
242 lines (180 loc) · 7.32 KB
/
anneal_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
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
import torch
import numpy as np
class AnnealedMALASampler:
"""Implements AIS with ULA"""
def __init__(self,
num_steps,
num_samples_per_step,
step_sizes,
gradient_function,
):
assert len(step_sizes) == num_steps, "Must have as many stepsizes as intermediate distributions."
self._step_sizes = step_sizes
self._num_steps = num_steps
self._num_samples_per_step = num_samples_per_step
self._gradient_function = gradient_function
def sample_step(self, x, t,ts, model_args):
e_old,grad = self._gradient_function(x, ts, **model_args)
for i in range(self._num_samples_per_step):
ss = self._step_sizes[t]
std = (2 * ss) ** .5
noise = torch.randn_like(grad) * std
x_proposal = x + grad * ss + noise
# Compute Energy of the samples
e_new,grad_new = self._gradient_function(x_proposal, ts, **model_args)
log_xhat_given_x = -1.0*((x_proposal - x - ss * grad) ** 2).sum() / (2 * ss**2)
log_x_given_xhat = -1.0 * ((x - x_proposal - ss * grad_new) ** 2).sum() / (2 * ss**2)
log_alpha = e_new-e_old +log_x_given_xhat - log_xhat_given_x
# Acceptance Ratio
if torch.log(torch.rand(1)) < log_alpha.detach().cpu():
x = x_proposal
e_old = e_new
grad = grad_new
return x
def leapfrog_step_c(x_0,
v_0,
gradient_target,
step_size,
mass_diag_sqrt,
num_steps,
grad_i):
"""Multiple leapfrog steps with no metropolis correction."""
x_k = x_0
v_k = v_0
grad_k = grad_i
if mass_diag_sqrt is None:
mass_diag_sqrt = torch.ones_like(x_k)
mass_diag = mass_diag_sqrt ** 2.
for _ in range(num_steps):
v_k += 0.5 * step_size *grad_k# # half step in v
x_k += step_size * v_k / mass_diag # Step in x
grad_k = gradient_target(x_k)[1]
v_k += 0.5 * step_size * grad_k # half step in v
return x_k, v_k
class AnnealedCHASampler:
def __init__(self,
num_steps,
num_samples_per_step,
step_sizes,
damping_coeff,
mass_diag_sqrt,
num_leapfrog_steps,
gradient_function,
):
assert len(step_sizes) == num_steps, "Must have as many stepsizes as intermediate distributions."
self._damping_coeff = damping_coeff
self._mass_diag_sqrt = mass_diag_sqrt
self._step_sizes = step_sizes
self._num_steps = num_steps
self._num_leapfrog_steps = num_leapfrog_steps
self._num_samples_per_step = num_samples_per_step
self._gradient_function = gradient_function
def leapfrog_step_(self, x, v, i, ts, grad_i,model_args):
step_size = self._step_sizes[i]
return leapfrog_step_c(x, v, lambda _x: self._gradient_function(_x, ts, **model_args), step_size, self._mass_diag_sqrt[i], self._num_leapfrog_steps,grad_i)
def sample_step(self, x, t,ts, model_args):
M=self._mass_diag_sqrt[t] # VAR
# Sample Momentum
v = torch.randn_like(x) * M
# v_dist = torch.distributions.normal.Normal(torch.zeros_like(x).cuda(), torch.ones_like(x).cuda() * M)
# v = v_dist.sample().cuda()
for i in range(self._num_samples_per_step):
# Partial Momentum Refreshment
eps = torch.randn_like(x)
v_prime = v * self._damping_coeff + np.sqrt(1. - self._damping_coeff**2) * eps * M
energy_i, grad_i = self._gradient_function(x,ts,**model_args)
x_old = x.clone()
v_old = v.clone()
x_new, v_new = self.leapfrog_step_(x, v_prime, t, ts,grad_i, model_args)
energy_new, grad_new = self._gradient_function(x_new,ts,**model_args)
energy_diff = energy_new-energy_i
# log_v = torch.sum(v_dist.log_prob(v_prime))
# log_v_new = torch.sum(v_dist.log_prob(v_new))
log_v_new = (-0.5*(1/M**2)) *torch.sum(v_new**2) # As mean 0 and Variance M**2
log_v = (-0.5*(1/M**2)) *torch.sum(v_prime**2)
logp_accept = energy_diff + (log_v_new - log_v)
alpha = torch.min(torch.tensor(1.0),torch.exp(logp_accept))
u = torch.rand(1)
if u <=alpha.cpu():
x = x_new
v = v_new
else:
x = x_old
v = v_old
# alpha = torch.exp(logp_accept)
# mask = (torch.rand(x.shape[0]).cuda() < alpha).float().unsqueeze(1).unsqueeze(2).unsqueeze(3)
# x = mask * x_new + (1 - mask) * x
# v = mask * v_new + (1 -mask) * v_prime
return x
def leapfrog_step(x_0,
v_0,
gradient_target,
step_size,
mass_diag_sqrt,
num_steps,
):
# """Multiple leapfrog steps with no metropolis correction."""
x_k = x_0
v_k = v_0
if mass_diag_sqrt is None:
mass_diag_sqrt = torch.ones_like(x_k)
mass_diag = mass_diag_sqrt ** 2.
grad = gradient_target(x_k)
for _ in range(num_steps): # Inefficient version - should combine half steps
v_k += 0.5 * step_size * grad#gradient_target(x_k) # half step in v
x_k += step_size * v_k / mass_diag # Step in x
grad = gradient_target(x_k)
v_k += 0.5 * step_size * grad # half step in v
return x_k, v_k
class AnnealedUHASampler:
"""Implements UHA Sampling"""
def __init__(self,
num_steps,
num_samples_per_step,
step_sizes,
damping_coeff,
mass_diag_sqrt,
num_leapfrog_steps,
gradient_function,
):
assert len(step_sizes) == num_steps, "Must have as many stepsizes as intermediate distributions."
self._damping_coeff = damping_coeff
self._mass_diag_sqrt = mass_diag_sqrt
self._step_sizes = step_sizes
self._num_steps = num_steps
self._num_leapfrog_steps = num_leapfrog_steps
self._num_samples_per_step = num_samples_per_step
self._gradient_function = gradient_function
def leapfrog_step_(self, x, v, i, ts, model_args):
step_size = self._step_sizes[i]
return leapfrog_step(x, v, lambda _x: self._gradient_function(_x, ts, **model_args), step_size, self._mass_diag_sqrt[i], self._num_leapfrog_steps)
def sample_step(self, x, t,ts, model_args):
# Sample Momentum
v = torch.randn_like(x) * self._mass_diag_sqrt[t]
for i in range(self._num_samples_per_step):
# Partial Momentum Refreshment
eps = torch.randn_like(x)
v = v * self._damping_coeff + np.sqrt(1. - self._damping_coeff**2) * eps * self._mass_diag_sqrt[t]
x, v = self.leapfrog_step_(x, v, t, ts, model_args)
return x
class AnnealedULASampler:
"""Implements AIS with ULA"""
def __init__(self,
num_steps,
num_samples_per_step,
step_sizes,
gradient_function,
):
assert len(step_sizes) == num_steps, "Must have as many stepsizes as intermediate distributions."
self._step_sizes = step_sizes
self._num_steps = num_steps
self._num_samples_per_step = num_samples_per_step
self._gradient_function = gradient_function
def sample_step(self, x, t,ts, model_args):
for i in range(self._num_samples_per_step):
ss = self._step_sizes[t]
std = (2 * ss) ** .5
grad = self._gradient_function(x, ts, **model_args)
noise = torch.randn_like(grad) * std
x = x + grad * ss + noise
return x