-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplers.py
274 lines (217 loc) · 8.61 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
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
### SAMPLERs.py
import torch
import numpy as np
import time
import math
from torch.autograd import Variable
from torch.autograd.variable import Variable
from torch import nn
import torch.nn.functional as F
#import compute as cp
#taken and modified from https://github.com/MichaelArbel/GeneralizedEBM/blob/master/samplers.py
#all credit goes to oriinal authors
class Latent_potential(nn.Module):
def __init__(self, generator,discriminator,latent_prior, temperature=100):
super().__init__()
self.generator = generator
self.discriminator = discriminator
self.latent_prior = latent_prior
self.temperature = temperature
def forward(self,Z):
if Z.size(dim=1) == 32:
with torch.backends.cudnn.flags(enabled=False):
g_states = self.generator.init_hidden(Z.shape[0])
d_state = self.discriminator.init_hidden(Z.shape[0])
out,_ = self.generator(Z,g_states)
out,_,_ = self.discriminator(out,d_state)
#should it be mean!!
out = -self.latent_prior.log_prob(Z).mean(dim=1) + self.temperature*out
else:
out = self.generator(Z)
out = self.discriminator(out)
out = -self.latent_prior.log_prob(Z) + self.temperature*out
## old
#out = self.generator(Z)
#out = self.discriminator(out)
#out = -self.latent_prior.log_prob(Z) + self.temperature*out
return out
class Cold_Latent_potential(nn.Module):
def __init__(self, generator,discriminator):
super().__init__()
self.generator = generator
self.discriminator = discriminator
def forward(self,Z):
out = self.generator(Z)
out = self.discriminator(out)
return out
class Independent_Latent_potential(nn.Module):
def __init__(self, generator,discriminator,latent_prior):
#super(Latent_potential).__init__()
super().__init__()
self.generator = generator
self.discriminator = discriminator
self.latent_prior = latent_prior
def forward(self,Z):
#with torch.no_grad():
out = self.generator(Z)
out = self.discriminator(out)
return out
class Dot_Latent_potential(nn.Module):
def __init__(self, generator,discriminator,latent_prior):
#super(Latent_potential).__init__()
super().__init__()
self.generator = generator
self.discriminator = discriminator
self.latent_prior = latent_prior
def forward(self,Z):
#with torch.no_grad():
out = self.generator(Z)
out = self.discriminator(out)
return torch.norm(Z, dim=1) + out
class Grad_potential(nn.Module):
def __init__(self, potential):
super().__init__()
self.potential = potential
def forward(self,X):
X.requires_grad_()
out = self.potential(X).sum()
out.backward()
return X.grad
class Grad_cond_potential(nn.Module):
def __init__(self, potential):
super().__init__()
self.potential = potential
def forward(self,X, labels):
X.requires_grad_()
Z = X,labels
out = self.potential(Z).sum()
out.backward()
return X.grad
class LangevinSampler(object):
def __init__(self, potential, T=100, gamma=1e-2):
self.potential = potential
#self.num_steps_min = num_steps_min
#self.num_steps_max = num_steps_max
self.gamma = gamma
self.grad_potential = Grad_potential(self.potential)
self.T = T
#self.grad_momentum = Grad_potential(self.momentum.log_prob)
#self.sampler_momentum = momentum_sampler
def sample(self,prior_z,sample_chain=False,T=None,thinning=10):
if T is None:
T = self.T
sampler = torch.distributions.Normal(torch.zeros_like(prior_z), 1.)
#self.momentum.eval()
#old
self.potential.eval()
#self.potential.train()
t_extract_list = []
Z_extract_list = []
accept_list = []
Z_t = prior_z.clone().detach()
gamma = 1.*self.gamma
#print(f'Initial lr: {gamma}')
for t in range(T):
if sample_chain and t > 0 and t % thinning == 0:
t_extract_list.append(t)
Z_extract_list.append(Z_t.clone().detach().cpu())
accept_list.append(1.)
# reset computation graph
Z_t = self.euler(Z_t,self.grad_potential,sampler,gamma=gamma)
# only if extracting the samples so we have a sequence of samples
if t>0 and t%200==0:
gamma *=0.1
print('decreasing lr for sampling')
#print('iteration: '+ str(t))
if not sample_chain:
return Z_t.clone().detach(),1.
return t_extract_list, Z_extract_list, accept_list
def euler(self,x,grad_x,sampler,gamma=1e-2):
x_t = x.clone().detach()
D = np.sqrt(gamma)
x_t = x_t - gamma / 2 * grad_x(x_t) + D * sampler.sample()
return x_t
class ZeroTemperatureSampler(object):
def __init__(self, potential, T=100, gamma=1e-2):
self.potential = potential
#self.num_steps_min = num_steps_min
#self.num_steps_max = num_steps_max
self.gamma = gamma
self.grad_potential = Grad_potential(self.potential)
self.T = T
#self.grad_momentum = Grad_potential(self.momentum.log_prob)
#self.sampler_momentum = momentum_sampler
def sample(self,prior_z,sample_chain=False,T=None,thinning=10):
if T is None:
T = self.T
sampler = torch.distributions.Normal(torch.zeros_like(prior_z), 1.)
#self.momentum.eval()
self.potential.eval()
t_extract_list = []
Z_extract_list = []
accept_list = []
Z_t = prior_z.clone().detach()
gamma = 1.*self.gamma
#print(f'Initial lr: {gamma}')
for t in range(T):
if sample_chain and t > 0 and t % thinning == 0:
t_extract_list.append(t)
Z_extract_list.append(Z_t.clone().detach().cpu())
accept_list.append(1.)
# reset computation graph
Z_t = self.euler(Z_t,self.grad_potential,sampler,gamma=gamma)
# only if extracting the samples so we have a sequence of samples
if t>0 and t%200==0:
gamma *=0.1
print('decreasing lr for sampling')
#print('iteration: '+ str(t))
if not sample_chain:
return Z_t.clone().detach(),1.
return t_extract_list, Z_extract_list, accept_list
def euler(self,x,grad_x,sampler,gamma=1e-2):
x_t = x.clone().detach()
D = np.sqrt(gamma)
x_t = x_t - gamma / 2 * grad_x(x_t) #+ D * sampler.sample()
return x_t
class SphereLangevinSampler(object):
def __init__(self, potential, T=100, gamma=1e-2):
self.potential = potential
self.gamma = gamma
self.grad_potential = Grad_potential(self.potential)
self.T = T
def sample(self,prior_z,sample_chain=False,T=None,thinning=10):
if T is None:
T = self.T
sampler = torch.distributions.Normal(torch.zeros_like(prior_z), 1.)
#self.momentum.eval()
self.potential.eval()
t_extract_list = []
Z_extract_list = []
accept_list = []
Z_t = prior_z.clone().detach()
gamma = 1.*self.gamma
#print(f'Initial lr: {gamma}')
for t in range(T):
if sample_chain and t > 0 and t % thinning == 0:
t_extract_list.append(t)
Z_extract_list.append(Z_t.clone().detach().cpu())
accept_list.append(1.)
# reset computation graph
Z_t = self.euler(Z_t,self.grad_potential,sampler,gamma=gamma)
# only if extracting the samples so we have a sequence of samples
if t>0 and t%200==0:
gamma *=0.1
print('decreasing lr for sampling')
#print('iteration: '+ str(t))
if not sample_chain:
return Z_t.clone().detach(),1.
return t_extract_list, Z_extract_list, accept_list
def euler(self,x,grad_x,sampler,gamma=1e-2):
x_t = x.clone().detach()
D = np.sqrt(2.*gamma)
R = x_t.shape[1]
grad = gamma * grad_x(x_t)
dot = torch.sum(grad*x_t, dim=1)
#grad = grad - torch.einsum('n,nd->nd',dot,x_t)/np.sqrt(R)
x_t = x_t - grad + D * sampler.sample()
return x_t