-
Notifications
You must be signed in to change notification settings - Fork 12
/
rbm_sample.py
266 lines (221 loc) · 9.18 KB
/
rbm_sample.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
import argparse
import rbm
import torch
import numpy as np
import samplers
import mmd
import matplotlib.pyplot as plt
import os
device = torch.device('cuda:' + str(0) if torch.cuda.is_available() else 'cpu')
import utils
import tensorflow_probability as tfp
import block_samplers
import time
import pickle
def makedirs(dirname):
"""
Make directory only if it's not already there.
"""
if not os.path.exists(dirname):
os.makedirs(dirname)
def get_ess(chain, burn_in):
c = chain
l = c.shape[0]
bi = int(burn_in * l)
c = c[bi:]
cv = tfp.mcmc.effective_sample_size(c).numpy()
cv[np.isnan(cv)] = 1.
return cv
def main(args):
makedirs(args.save_dir)
torch.manual_seed(args.seed)
np.random.seed(args.seed)
model = rbm.BernoulliRBM(args.n_visible, args.n_hidden)
model.to(device)
print(device)
if args.data == "mnist":
assert args.n_visible == 784
train_loader, test_loader, plot, viz = utils.get_data(args)
init_data = []
for x, _ in train_loader:
init_data.append(x)
init_data = torch.cat(init_data, 0)
init_mean = init_data.mean(0).clamp(.01, .99)
model = rbm.BernoulliRBM(args.n_visible, args.n_hidden, data_mean=init_mean)
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=args.rbm_lr)
# train!
itr = 0
for x, _ in train_loader:
x = x.to(device)
xhat = model.gibbs_sample(v=x, n_steps=args.cd)
d = model.logp_v_unnorm(x)
m = model.logp_v_unnorm(xhat)
obj = d - m
loss = -obj.mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
if itr % args.print_every == 0:
print("{} | log p(data) = {:.4f}, log p(model) = {:.4f}, diff = {:.4f}".format(itr,d.mean(), m.mean(),
(d - m).mean()))
else:
model.W.data = torch.randn_like(model.W.data) * (.05 ** .5)
model.b_v.data = torch.randn_like(model.b_v.data) * 1.0
model.b_h.data = torch.randn_like(model.b_h.data) * 1.0
viz = plot = None
gt_samples = model.gibbs_sample(n_steps=args.gt_steps, n_samples=args.n_samples + args.n_test_samples, plot=True)
kmmd = mmd.MMD(mmd.exp_avg_hamming, False)
gt_samples, gt_samples2 = gt_samples[:args.n_samples], gt_samples[args.n_samples:]
if plot is not None:
plot("{}/ground_truth.png".format(args.save_dir), gt_samples2)
opt_stat = kmmd.compute_mmd(gt_samples2, gt_samples)
print("gt <--> gt log-mmd", opt_stat, opt_stat.log10())
new_samples = model.gibbs_sample(n_steps=0, n_samples=args.n_test_samples)
log_mmds = {}
log_mmds['gibbs'] = []
ars = {}
hops = {}
ess = {}
times = {}
chains = {}
chain = []
times['gibbs'] = []
start_time = time.time()
for i in range(args.n_steps):
if i % args.print_every == 0:
stat = kmmd.compute_mmd(new_samples, gt_samples)
log_stat = stat.log10().item()
log_mmds['gibbs'].append(log_stat)
print("gibbs", i, stat, stat.log10())
times['gibbs'].append(time.time() - start_time)
new_samples = model.gibbs_sample(new_samples, 1)
if i % args.subsample == 0:
if args.ess_statistic == "dims":
chain.append(new_samples.cpu().numpy()[0][None])
else:
xc = new_samples[0][None]
h = (xc != gt_samples).float().sum(-1)
chain.append(h.detach().cpu().numpy()[None])
chain = np.concatenate(chain, 0)
chains['gibbs'] = chain
ess['gibbs'] = get_ess(chain, args.burn_in)
print("ess = {} +/- {}".format(ess['gibbs'].mean(), ess['gibbs'].std()))
temps = ['bg-1', 'bg-2', 'hb-10-1', 'gwg', 'gwg-3', 'gwg-5']
for temp in temps:
if temp == 'dim-gibbs':
sampler = samplers.PerDimGibbsSampler(args.n_visible)
elif temp == "rand-gibbs":
sampler = samplers.PerDimGibbsSampler(args.n_visible, rand=True)
elif "bg-" in temp:
block_size = int(temp.split('-')[1])
sampler = block_samplers.BlockGibbsSampler(args.n_visible, block_size)
elif "hb-" in temp:
block_size, hamming_dist = [int(v) for v in temp.split('-')[1:]]
sampler = block_samplers.HammingBallSampler(args.n_visible, block_size, hamming_dist)
elif temp == "gwg":
sampler = samplers.DiffSampler(args.n_visible, 1,
fixed_proposal=False, approx=True, multi_hop=False, temp=2.)
elif "gwg-" in temp:
n_hops = int(temp.split('-')[1])
sampler = samplers.MultiDiffSampler(args.n_visible, 1,
approx=True, temp=2., n_samples=n_hops)
else:
raise ValueError("Invalid sampler...")
x = model.init_dist.sample((args.n_test_samples,)).to(device)
log_mmds[temp] = []
ars[temp] = []
hops[temp] = []
times[temp] = []
chain = []
cur_time = 0.
for i in range(args.n_steps):
# do sampling and time it
st = time.time()
xhat = sampler.step(x.detach(), model).detach()
cur_time += time.time() - st
# compute hamming dist
cur_hops = (x != xhat).float().sum(-1).mean().item()
# update trajectory
x = xhat
if i % args.subsample == 0:
if args.ess_statistic == "dims":
chain.append(x.cpu().numpy()[0][None])
else:
xc = x[0][None]
h = (xc != gt_samples).float().sum(-1)
chain.append(h.detach().cpu().numpy()[None])
if i % args.viz_every == 0 and plot is not None:
plot("/{}/temp_{}_samples_{}.png".format(args.save_dir, temp, i), x)
if i % args.print_every == 0:
hard_samples = x
stat = kmmd.compute_mmd(hard_samples, gt_samples)
log_stat = stat.log10().item()
log_mmds[temp].append(log_stat)
times[temp].append(cur_time)
hops[temp].append(cur_hops)
print("temp {}, itr = {}, log-mmd = {:.4f}, hop-dist = {:.4f}".format(temp, i, log_stat, cur_hops))
chain = np.concatenate(chain, 0)
ess[temp] = get_ess(chain, args.burn_in)
chains[temp] = chain
print("ess = {} +/- {}".format(ess[temp].mean(), ess[temp].std()))
ess_temps = temps
plt.clf()
plt.boxplot([ess[temp] for temp in ess_temps], labels=ess_temps, showfliers=False)
plt.savefig("{}/ess.png".format(args.save_dir))
plt.clf()
plt.boxplot([ess[temp] / times[temp][-1] / (1. - args.burn_in) for temp in ess_temps], labels=ess_temps, showfliers=False)
plt.savefig("{}/ess_per_sec.png".format(args.save_dir))
plt.clf()
for temp in temps + ['gibbs']:
plt.plot(log_mmds[temp], label="{}".format(temp))
plt.legend()
plt.savefig("{}/results.png".format(args.save_dir))
plt.clf()
for temp in temps:
plt.plot(ars[temp], label="{}".format(temp))
plt.legend()
plt.savefig("{}/ars.png".format(args.save_dir))
plt.clf()
for temp in temps:
plt.plot(hops[temp], label="{}".format(temp))
plt.legend()
plt.savefig("{}/hops.png".format(args.save_dir))
for temp in temps:
plt.clf()
plt.plot(chains[temp][:, 0])
plt.savefig("{}/trace_{}.png".format(args.save_dir, temp))
with open("{}/results.pkl".format(args.save_dir), 'wb') as f:
results = {
'ess': ess,
'hops': hops,
'log_mmds': log_mmds,
'chains': chains,
'times': times
}
pickle.dump(results, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--save_dir', type=str, default="/tmp/test_discrete")
parser.add_argument('--data', choices=['mnist', 'random'], type=str, default='random')
parser.add_argument('--n_steps', type=int, default=5000)
parser.add_argument('--n_samples', type=int, default=500)
parser.add_argument('--n_test_samples', type=int, default=100)
parser.add_argument('--gt_steps', type=int, default=10000)
parser.add_argument('--seed', type=int, default=1234567)
# rbm def
parser.add_argument('--n_hidden', type=int, default=25)
parser.add_argument('--n_visible', type=int, default=100)
parser.add_argument('--print_every', type=int, default=10)
parser.add_argument('--viz_every', type=int, default=100)
# for rbm training
parser.add_argument('--rbm_lr', type=float, default=.001)
parser.add_argument('--cd', type=int, default=10)
parser.add_argument('--img_size', type=int, default=28)
parser.add_argument('--batch_size', type=int, default=100)
# for ess
parser.add_argument('--subsample', type=int, default=1)
parser.add_argument('--burn_in', type=float, default=.1)
parser.add_argument('--ess_statistic', type=str, default="dims", choices=["hamming", "dims"])
args = parser.parse_args()
main(args)