-
Notifications
You must be signed in to change notification settings - Fork 19
/
gflownet.py
624 lines (504 loc) · 25.3 KB
/
gflownet.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import os, sys
import time
import copy
import random
import ipdb
from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributions as dists
import torchvision
from network import make_mlp
def get_GFlowNet(type, xdim, args, device, net=None):
if type == "tbrf":
return GFlowNet_Randf_TB(xdim=xdim, args=args, device=device, net=net)
elif type == "tblb":
return GFlowNet_LearnedPb_TB(xdim=xdim, args=args, device=device, net=net)
else:
raise NotImplementedError
class GFlowNet_Randf_TB:
# binary data, train w/ long DB loss
def __init__(self, xdim, args, device, net=None):
self.xdim = xdim
self._hops = 0.
# (bs, data_dim) -> (bs, data_dim)
if net is None:
self.model = make_mlp([xdim] + [args.hid] * args.hid_layers +
[3 * xdim], act=(nn.LeakyReLU() if args.leaky else nn.ReLU()), with_bn=args.gfn_bn)
else:
self.model = net
self.model.to(device)
self.logZ = nn.Parameter(torch.tensor(0.))
self.logZ.to(device)
self.device = device
self.exp_temp = args.temp
self.rand_coef = args.rand_coef # involving exploration
self.init_zero = args.init_zero
self.clip = args.clip
self.l1loss = args.l1loss
self.replay = None
self.tau = args.tau if hasattr(args, "tau") else -1
self.train_steps = args.train_steps
param_list = [{'params': self.model.parameters(), 'lr': args.glr},
{'params': self.logZ, 'lr': args.zlr}]
if args.opt == "adam":
self.optimizer = torch.optim.Adam(param_list, weight_decay=args.gfn_weight_decay)
elif args.opt == "sgd":
self.optimizer = torch.optim.SGD(param_list, momentum=args.momentum, weight_decay=args.gfn_weight_decay)
def backforth_sample(self, x, K, rand_coef=0.):
assert K > 0
batch_size = x.size(0)
# "backward"
logp_xprime2x = torch.zeros(batch_size).to(self.device)
for step in range(K + 1):
del_val_logits = self.model(x)[:, :2 * self.xdim]
if step > 0:
del_val_logits = del_val_logits.reshape(-1, self.xdim, 2)
log_del_val_prob = del_val_logits.gather(1, del_locs.unsqueeze(2).repeat(1, 1, 2)).squeeze().log_softmax(1)
logp_xprime2x = logp_xprime2x + log_del_val_prob.gather(1, deleted_val).squeeze(1)
if step < K:
if self.init_zero:
# mask = (x == 0).float()
mask = (x.abs() < 1e-8).float()
else:
mask = (x < -0.5).float()
del_locs = (0 - 1e9 * mask).softmax(1).multinomial(1) # row sum not need to be 1
deleted_val = x.gather(1, del_locs).long()
del_values = torch.ones(batch_size, 1).to(self.device) * (0 if self.init_zero else -1)
x = x.scatter(1, del_locs, del_values)
# forward
logp_x2xprime = torch.zeros(batch_size).to(self.device)
for step in range(K):
logits = self.model(x)
add_logits = logits[:, :2 * self.xdim]
# those have been edited
if self.init_zero:
mask = (x != 0).float()
else:
mask = (x > -0.5).float()
add_prob = (1 - mask) / (1e-9 + (1 - mask).sum(1)).unsqueeze(1)
add_locs = add_prob.multinomial(1)
add_val_logits = add_logits.reshape(-1, self.xdim, 2)
add_val_prob = add_val_logits.gather(1, add_locs.unsqueeze(2).repeat(1, 1, 2)).squeeze().softmax(1)
add_values = add_val_prob.multinomial(1)
if rand_coef > 0:
updates = torch.bernoulli(rand_coef * torch.ones(x.shape[0])).int().to(x.device)
add_values = (1 - add_values) * updates[:, None] + add_values * (1 - updates[:, None])
logp_x2xprime = logp_x2xprime + add_val_prob.log().gather(1, add_values).squeeze(1) # (bs, 1) -> (bs,)
if self.init_zero:
add_values = 2 * add_values - 1
x = x.scatter(1, add_locs, add_values.float())
return x, logp_xprime2x - logp_x2xprime # leave MH step to out loop code
def sample(self, batch_size):
self.model.eval()
if self.init_zero:
x = torch.zeros((batch_size, self.xdim)).to(self.device)
else:
x = -1 * torch.ones((batch_size, self.xdim)).to(self.device)
for step in range(self.xdim + 1):
if step < self.xdim:
logits = self.model(x)
add_logits, _ = logits[:, :2 * self.xdim], logits[:, 2 * self.xdim:]
if self.init_zero:
mask = (x != 0).float()
else:
mask = (x > -0.5).float()
add_prob = (1 - mask) / (1e-9 + (1 - mask).sum(1)).unsqueeze(1)
add_locs = add_prob.multinomial(1) # row sum not need to be 1
add_val_logits = add_logits.reshape(-1, self.xdim, 2)
add_val_prob = add_val_logits.gather(1, add_locs.unsqueeze(2).repeat(1, 1, 2)).squeeze().softmax(1)
add_values = add_val_prob.multinomial(1)
if self.init_zero:
add_values = 2 * add_values - 1
x = x.scatter(1, add_locs, add_values.float())
return x
def cal_logp(self, data, num: int):
logp_ls = []
for _ in range(num):
_, _, _, mle_loss, = tb_mle_randf_loss(lambda inp: torch.tensor(0.).to(self.device),
self, data.shape[0], back_ratio=1, data=data)
logpj = - mle_loss.detach().cpu() - torch.tensor(num).log()
logp_ls.append(logpj.reshape(logpj.shape[0], -1))
batch_logp = torch.logsumexp(torch.cat(logp_ls, dim=1), dim=1) # (bs,)
return batch_logp.mean()
def evaluate(self, loader, preprocess, num, use_tqdm=False):
logps = []
if use_tqdm:
pbar = tqdm(loader)
else:
pbar = loader
if hasattr(pbar, "set_description"):
pbar.set_description("Calculating likelihood")
self.model.eval()
for x, _ in pbar:
x = preprocess(x.to(self.device))
logp = self.cal_logp(x, num)
logps.append(logp.reshape(-1))
if hasattr(pbar, "set_postfix"):
pbar.set_postfix({"logp": f"{torch.cat(logps).mean().item():.2f}"})
return torch.cat(logps).mean()
def train(self, batch_size, scorer, silent=False, data=None, back_ratio=0.,): #mle_coef=0., kl_coef=0., kl2_coef=0., pdb=False):
# scorer: x -> logp
if silent:
pbar = range(self.train_steps)
else:
pbar = tqdm(range(self.train_steps))
curr_lr = self.optimizer.param_groups[0]['lr']
pbar.set_description(f"Lr={curr_lr:.1e}")
train_loss = []
train_mle_loss = []
train_logZ = []
# train_kl_loss = []
self.model.train()
self.model.zero_grad()
torch.cuda.empty_cache()
for _ in pbar:
gfn_loss, forth_loss, back_loss, mle_loss = \
tb_mle_randf_loss(scorer, self, batch_size, back_ratio=back_ratio, data=data)
gfn_loss, forth_loss, back_loss, mle_loss = \
gfn_loss.mean(), forth_loss.mean(), back_loss.mean(), mle_loss.mean()
loss = gfn_loss
self.optimizer.zero_grad()
loss.backward()
if self.clip > 0:
torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=self.clip, norm_type="inf")
self.optimizer.step()
train_loss.append(gfn_loss.item())
train_mle_loss.append(mle_loss.item())
train_logZ.append(self.logZ.item())
if not silent:
pbar.set_postfix({"MLE": "{:.2e}".format(mle_loss.item()),
"GFN": "{:.2e}".format(gfn_loss.item()),
"Forth": "{:.2e}".format(forth_loss.item()),
"Back": "{:.2e}".format(back_loss.item()),
"LogZ": "{:.2e}".format(self.logZ.item()),
})
return np.mean(train_loss), np.mean(train_logZ)
def tb_mle_randf_loss(ebm_model, gfn, batch_size, back_ratio=0., data=None):
if back_ratio < 1.:
if gfn.init_zero:
x = torch.zeros((batch_size, gfn.xdim)).to(gfn.device)
else:
x = -1 * torch.ones((batch_size, gfn.xdim)).to(gfn.device)
log_pf = 0.
for step in range(gfn.xdim + 1):
logits = gfn.model(x)
add_logits, _ = logits[:, :2 * gfn.xdim], logits[:, 2 * gfn.xdim:]
if step < gfn.xdim:
# mask those that have been edited
if gfn.init_zero:
mask = (x != 0).float()
else:
mask = (x > -0.5).float()
add_prob = (1 - mask) / (1e-9 + (1 - mask).sum(1)).unsqueeze(1)
add_locs = add_prob.multinomial(1)
add_val_logits = add_logits.reshape(-1, gfn.xdim, 2)
add_val_prob = add_val_logits.gather(1, add_locs.unsqueeze(2).repeat(1, 1, 2)).squeeze().softmax(1)
add_values = add_val_prob.multinomial(1)
if gfn.rand_coef > 0:
# updates = torch.distributions.Bernoulli(probs=gfn.rand_coef).sample(sample_shape=torch.Size([x.shape[0]]))
updates = torch.bernoulli(gfn.rand_coef * torch.ones(x.shape[0])).int().to(x.device)
add_values = (1 - add_values) * updates[:, None] + add_values * (1 - updates[:, None])
log_pf = log_pf + add_val_prob.log().gather(1, add_values).squeeze(1) # (bs, 1) -> (bs,)
if gfn.init_zero:
add_values = 2 * add_values - 1
x = x.scatter(1, add_locs, add_values.float())
assert torch.all(x != 0) if gfn.init_zero else torch.all(x >= 0)
score_value = ebm_model(x)
if gfn.l1loss:
forth_loss = F.smooth_l1_loss(gfn.logZ + log_pf - score_value, torch.zeros_like(score_value))
else:
forth_loss = (gfn.logZ + log_pf - score_value) ** 2
else:
forth_loss = torch.tensor(0.).to(gfn.device)
# traj is from given data back to s0, sample w/ unif back prob
mle_loss = torch.tensor(0.).to(gfn.device)
if back_ratio <= 0.:
back_loss = torch.tensor(0.).to(gfn.device)
else:
assert data is not None
x = data
batch_size = x.size(0)
back_loss = torch.zeros(batch_size).to(gfn.device)
for step in range(gfn.xdim + 1):
logits = gfn.model(x)
del_val_logits, _ = logits[:, :2 * gfn.xdim], logits[:, 2 * gfn.xdim:]
if step > 0:
del_val_logits = del_val_logits.reshape(-1, gfn.xdim, 2)
log_del_val_prob = del_val_logits.gather(1, del_locs.unsqueeze(2).repeat(1, 1, 2)).squeeze().log_softmax(1)
mle_loss = mle_loss + log_del_val_prob.gather(1, deleted_val).squeeze(1)
if step < gfn.xdim:
if gfn.init_zero:
mask = (x.abs() < 1e-8).float()
else:
mask = (x < -0.5).float()
del_locs = (0 - 1e9 * mask).softmax(1).multinomial(1) # row sum not need to be 1
deleted_val = x.gather(1, del_locs).long()
del_values = torch.ones(batch_size, 1).to(gfn.device) * (0 if gfn.init_zero else -1)
x = x.scatter(1, del_locs, del_values)
# if back_ratio > 0.:
if gfn.l1loss:
back_loss = F.smooth_l1_loss(gfn.logZ + mle_loss - ebm_model(data).detach(), torch.zeros_like(mle_loss))
else:
back_loss = (gfn.logZ + mle_loss - ebm_model(data).detach()) ** 2
gfn_loss = (1 - back_ratio) * forth_loss + back_ratio * back_loss
return gfn_loss, forth_loss, back_loss, mle_loss
class GFlowNet_LearnedPb_TB:
def __init__(self, xdim, args, device, net=None):
self.xdim = xdim
self._hops = 0.
# (bs, data_dim) -> (bs, data_dim)
if net is None:
self.model = make_mlp([xdim] + [args.hid] * args.hid_layers +
[3 * xdim], act=(nn.LeakyReLU() if args.leaky else nn.ReLU()), with_bn=args.gfn_bn)
else:
self.model = net
self.model.to(device)
self.logZ = nn.Parameter(torch.tensor(0.))
self.logZ.to(device)
self.device = device
self.exp_temp = args.temp
self.rand_coef = args.rand_coef # involving exploration
self.init_zero = args.init_zero
self.clip = args.clip
self.l1loss = args.l1loss
self.replay = None
self.tau = args.tau if hasattr(args, "tau") else -1
self.train_steps = args.train_steps
param_list = [{'params': self.model.parameters(), 'lr': args.glr},
{'params': self.logZ, 'lr': args.zlr}]
if args.opt == "adam":
self.optimizer = torch.optim.Adam(param_list)
elif args.opt == "sgd":
self.optimizer = torch.optim.SGD(param_list, momentum=args.momentum)
def backforth_sample(self, x, K):
assert K > 0
batch_size = x.size(0)
logp_xprime2x = torch.zeros(batch_size).to(self.device)
logp_x2xprime = torch.zeros(batch_size).to(self.device)
# "backward"
for step in range(K + 1):
logits = self.model(x)
add_logits, del_logits = logits[:, :2 * self.xdim], logits[:, 2 * self.xdim:]
if step > 0:
if self.init_zero:
mask = (x != 0).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * self.xdim).float()
else:
mask = (x > -0.5).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * self.xdim).float()
add_sample = del_locs * 2 + (deleted_values == 1).long() # whether it's init_zero, this holds true
logp_xprime2x = logp_xprime2x + (add_logits - 1e9 * mask).float().log_softmax(1).gather(1,add_sample).squeeze(1)
if step < K:
if self.init_zero:
mask = (x.abs() < 1e-8).float()
else:
mask = (x < -0.5).float()
del_logits = (del_logits - 1e9 * mask).float()
del_locs = del_logits.softmax(1).multinomial(1) # row sum not need to be 1
del_values = torch.ones(batch_size, 1).to(self.device) * (0 if self.init_zero else -1)
deleted_values = x.gather(1, del_locs)
logp_x2xprime = logp_x2xprime + del_logits.float().log_softmax(1).gather(1, del_locs).squeeze(1)
x = x.scatter(1, del_locs, del_values)
# forward
for step in range(K + 1):
logits = self.model(x)
add_logits, del_logits = logits[:, :2 * self.xdim], logits[:, 2 * self.xdim:]
if step > 0:
if self.init_zero:
mask = (x.abs() < 1e-8).float()
else:
mask = (x < 0).float()
logp_xprime2x = logp_xprime2x + (del_logits - 1e9 * mask).log_softmax(1).gather(1, add_locs).squeeze(1)
if step < K:
# those have been edited
if self.init_zero:
mask = (x != 0).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * self.xdim).float()
else:
mask = (x > -0.5).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * self.xdim).float()
add_logits = (add_logits - 1e9 * mask).float()
add_prob = add_logits.softmax(1)
# haven't used rand coef here
add_sample = add_prob.multinomial(1) # row sum not need to be 1
if self.init_zero:
add_locs, add_values = add_sample // 2, 2 * (add_sample % 2) - 1
else:
add_locs, add_values = add_sample // 2, add_sample % 2
logp_x2xprime = logp_x2xprime + add_logits.log_softmax(1).gather(1, add_sample).squeeze(1)
x = x.scatter(1, add_locs, add_values.float())
return x, logp_xprime2x - logp_x2xprime # leave MH step to out loop code
def sample(self, batch_size):
self.model.eval()
if self.init_zero:
x = torch.zeros((batch_size, self.xdim)).to(self.device)
else:
x = -1 * torch.ones((batch_size, self.xdim)).to(self.device)
for step in range(self.xdim + 1):
logits = self.model(x)
add_logits, del_logits = logits[:, :2 * self.xdim], logits[:, 2 * self.xdim:]
# those have been edited
if self.init_zero:
mask = (x != 0).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * self.xdim).float()
else:
mask = (x > -0.5).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * self.xdim).float()
add_prob = (add_logits - 1e9 * mask).float().softmax(1)
if step < self.xdim:
# add_prob = add_prob ** (1 / self.exp_temp)
add_sample = add_prob.multinomial(1) # row sum not need to be 1
if self.init_zero:
add_locs, add_values = add_sample // 2, 2 * (add_sample % 2) - 1
else:
add_locs, add_values = add_sample // 2, add_sample % 2
x = x.scatter(1, add_locs, add_values.float())
return x
def cal_logp(self, data, num: int):
logp_ls = []
for _ in range(num):
_, _, _, mle_loss, data_log_pb = tb_mle_learnedpb_loss(lambda inp: torch.tensor(0.).to(self.device), self, data.shape[0], back_ratio=1, data=data)
logpj = - mle_loss.detach().cpu() - data_log_pb.detach().cpu()
logp_ls.append(logpj.reshape(logpj.shape[0], -1))
batch_logp = torch.logsumexp(torch.cat(logp_ls, dim=1), dim=1) # (bs,)
return batch_logp.mean() - torch.tensor(num).log()
def evaluate(self, loader, preprocess, num, use_tqdm=False):
logps = []
if use_tqdm:
pbar = tqdm(loader)
else:
pbar = loader
if hasattr(pbar, "set_description"):
pbar.set_description("Calculating likelihood")
self.model.eval()
for x, _ in pbar:
x = preprocess(x.to(self.device))
logp = self.cal_logp(x, num)
logps.append(logp.reshape(-1))
if hasattr(pbar, "set_postfix"):
pbar.set_postfix({"logp": f"{torch.cat(logps).mean().item():.2f}"})
return torch.cat(logps).mean()
def train(self, batch_size, scorer, silent=False,
data=None, back_ratio=0.):
if silent:
pbar = range(self.train_steps)
else:
pbar = tqdm(range(self.train_steps))
curr_lr = self.optimizer.param_groups[0]['lr']
pbar.set_description(f"Alg: GFN LongDB Training, Lr={curr_lr:.1e}")
train_loss = []
train_mle_loss = []
train_logZ = []
self.model.train()
self.model.zero_grad()
torch.cuda.empty_cache()
for _ in pbar:
gfn_loss, forth_loss, back_loss, mle_loss, data_log_pb = \
tb_mle_learnedpb_loss(scorer, self, batch_size, back_ratio=back_ratio, data=data)
gfn_loss, forth_loss, back_loss, mle_loss, data_log_pb = \
gfn_loss.mean(), forth_loss.mean(), back_loss.mean(), mle_loss.mean(), data_log_pb.mean()
loss = gfn_loss
self.optimizer.zero_grad()
loss.backward()
if self.clip > 0:
torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=self.clip, norm_type="inf")
self.optimizer.step()
train_loss.append(gfn_loss.item())
train_mle_loss.append(mle_loss.item())
train_logZ.append(self.logZ.item())
if not silent:
pbar.set_postfix({"MLE": "{:.2e}".format(mle_loss.item()),
"GFN": "{:.2e}".format(gfn_loss.item()),
"Forth": "{:.2e}".format(forth_loss.item()),
"Back": "{:.2e}".format(back_loss.item()),
"LogZ": "{:.2e}".format(self.logZ.item()),
})
return np.mean(train_loss), np.mean(train_logZ)
def tb_mle_learnedpb_loss(ebm_model, gfn, batch_size, back_ratio=0., data=None):
# traj is from s0 -> sf, sample by current gfn policy
if back_ratio < 1.:
if gfn.init_zero:
x = torch.zeros((batch_size, gfn.xdim)).to(gfn.device)
else:
# -1 denotes "have not been edited"
x = -1 * torch.ones((batch_size, gfn.xdim)).to(gfn.device)
# forth_loss = 0.
log_pb = 0.
log_pf = 0.
for step in range(gfn.xdim + 1):
logits = gfn.model(x)
add_logits, del_logits = logits[:, :2 * gfn.xdim], logits[:, 2 * gfn.xdim:]
if step > 0:
if gfn.init_zero:
mask = (x.abs() < 1e-8).float()
else:
mask = (x < 0).float()
log_pb = log_pb + (del_logits - 1e9 * mask).log_softmax(1).gather(1, add_locs).squeeze(1)
# log_pb = log_pb + torch.tensor(1 / step).log().to(gfn.device)
if step < gfn.xdim:
# mask those that have been edited
if gfn.init_zero:
mask = (x != 0).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * gfn.xdim).float()
else:
mask = (x > -0.5).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * gfn.xdim).float()
add_logits = (add_logits - 1e9 * mask).float()
add_prob = add_logits.softmax(1)
add_prob = add_prob ** (1 / gfn.exp_temp)
add_prob = add_prob / (1e-9 + add_prob.sum(1, keepdim=True))
add_prob = (1 - gfn.rand_coef) * add_prob + \
gfn.rand_coef * (1 - mask) / (1e-9 + (1 - mask).sum(1)).unsqueeze(1)
add_sample = add_prob.multinomial(1)
if gfn.init_zero:
add_locs, add_values = add_sample // 2, 2 * (add_sample % 2) - 1
else:
add_locs, add_values = add_sample // 2, add_sample % 2
# P_F
log_pf = log_pf + add_logits.log_softmax(1).gather(1, add_sample).squeeze(1)
# update x
x = x.scatter(1, add_locs, add_values.float())
assert torch.all(x != 0) if gfn.init_zero else torch.all(x >= 0)
score_value = ebm_model(x)
if gfn.l1loss:
forth_loss = F.smooth_l1_loss(gfn.logZ + log_pf - log_pb - score_value, torch.zeros_like(score_value))
else:
forth_loss = ((gfn.logZ + log_pf - log_pb - score_value) ** 2)
else:
forth_loss = torch.tensor(0.).to(gfn.device)
mle_loss = torch.tensor(0.).to(gfn.device) # log_pf
if back_ratio <= 0.:
data_log_pb = torch.tensor(0.).to(gfn.device)
back_loss = torch.tensor(0.).to(gfn.device)
else:
assert data is not None
x = data
batch_size = x.size(0)
data_log_pb = torch.zeros(batch_size).to(gfn.device)
for step in range(gfn.xdim + 1):
logits = gfn.model(x)
add_logits, del_logits = logits[:, :2 * gfn.xdim], logits[:, 2 * gfn.xdim:]
if step > 0:
if gfn.init_zero:
mask = (x != 0).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * gfn.xdim).float()
else:
mask = (x > -0.5).unsqueeze(2).repeat(1, 1, 2).reshape(batch_size, 2 * gfn.xdim).float()
add_sample = del_locs * 2 + (deleted_values == 1).long() # whether it's init_zero, this holds true
add_logits = (add_logits - 1e9 * mask).float()
mle_loss = mle_loss + add_logits.log_softmax(1).gather(1, add_sample).squeeze(1)
if step < gfn.xdim:
if gfn.init_zero:
# mask = (x == 0).float()
mask = (x.abs() < 1e-8).float()
else:
mask = (x < -0.5).float()
del_logits = (del_logits - 1e9 * mask).float()
del_prob = del_logits.softmax(1)
del_prob = (1 - gfn.rand_coef) * del_prob + gfn.rand_coef * (1 - mask) / (1e-9 + (1 - mask).sum(1)).unsqueeze(1)
del_locs = del_prob.multinomial(1) # row sum not need to be 1
deleted_values = x.gather(1, del_locs)
data_log_pb = data_log_pb + del_logits.log_softmax(1).gather(1, del_locs).squeeze(1)
del_values = torch.ones(batch_size, 1).to(gfn.device) * (0 if gfn.init_zero else -1)
x = x.scatter(1, del_locs, del_values)
if gfn.l1loss:
back_loss = F.smooth_l1_loss(gfn.logZ + mle_loss - data_log_pb - ebm_model(data).detach(), torch.zeros_like(mle_loss))
else:
back_loss = ((gfn.logZ + mle_loss - data_log_pb - ebm_model(data).detach()) ** 2)
gfn_loss = (1 - back_ratio) * forth_loss + back_ratio * back_loss
mle_loss = - mle_loss
return gfn_loss, forth_loss, back_loss, mle_loss, data_log_pb