-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
714 lines (529 loc) · 25.7 KB
/
generator.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import os
import json
import torch
import torch.nn as nn
from torch.distributions import Categorical
from util.function import smis_to_actions
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
import os
from tkinter import S
import torch
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch.utils import data
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
from util.function import LatentLoss, DiffLoss, SimLoss
from torch.nn.utils.rnn import pack_padded_sequence
from tqdm import tqdm
import torch.nn.functional as F
import argparse
class Block(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm3d(out_channels)
self.rl1 = nn.ReLU()
self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm3d(out_channels)
self.rl2 = nn.ReLU()
out_channels *= 2
self.max_pool = nn.MaxPool3d(kernel_size=(2, 2, 2))
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x1 = self.rl1(x)
x1 = self.conv2(x1)
x1 = self.bn2(x1)
x2 = self.rl2(x1)
return x2
class Block_Pool(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm3d(out_channels)
self.rl1 = nn.ReLU()
self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm3d(out_channels)
self.rl2 = nn.ReLU()
out_channels *= 2
self.max_pool = nn.MaxPool3d(kernel_size=(2, 2, 2))
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x1 = self.rl1(x)
x1 = self.conv2(x1)
x1 = self.bn2(x1)
x2 = self.rl2(x1)
return x2
class generator(nn.Module):
#RELATION generator
def __init__(self,in_channel_0, out_channel_0,in_channel_1,out_channel_1,in_channel_2,out_channel_2,
in_channel_3,out_channel_3):
super(generator).__init__()
self.in_channel_0=in_channel_0
self.out_channel_0=out_channel_0
self.in_channel_1=in_channel_1
self.out_channel_1=out_channel_1
self.in_channel_2=in_channel_2
self.out_channel_2=out_channel_2
self.in_channel_3=in_channel_3
self.out_channel_3=out_channel_3
# private source encoder(Ligand in ZINC dataset)
shr_enc = []
self.fc11 = nn.Linear(256, 512)
self.fc12 = nn.Linear(256, 512)
self.fc2 = nn.Linear(512, 512)
self.fc3 = nn.Linear(512, 1024)
self.fc4 = nn.Linear(1024, 39)
self.gru1 = nn.GRU(39, 256, 3, batch_first=True)
self.embedding = nn.Embedding(39, 39, 0)
self.gru = nn.GRU(551, 1024, 3, batch_first=True)
# pri_source_encoder
self.blocks_1 = nn.Sequential(*[Block(in_channel_0, out_channel_0), Block_Pool(out_channel_0, in_channel_1)])
self.blocks_2 = nn.Sequential(*[Block(in_channel_1, in_channel_1), Block_Pool(in_channel_1, out_channel_1)])
self.blocks3 = nn.Sequential(*[Block(in_channel_2, out_channel_1), Block_Pool(out_channel_1, out_channel_2)])
self.blocks4 = nn.Sequential(*[Block(in_channel_3, out_channel_2), Block(out_channel_2, out_channel_3)])
# pri_target_encoder
self.blocks_1_1 = nn.Sequential(*[Block(in_channel_0, out_channel_0), Block_Pool(out_channel_0, in_channel_1)])
self.blocks_2_2 = nn.Sequential(*[Block(in_channel_1, in_channel_1), Block_Pool(in_channel_1, out_channel_1)])
self.blocks_3_3 = nn.Sequential(*[Block(in_channel_2, out_channel_1), Block_Pool(out_channel_1, out_channel_2)])
self.blocks_4_4 = nn.Sequential(*[Block(in_channel_3, out_channel_2), Block(out_channel_2, out_channel_3)])
self.blocks_1_1_1 = nn.Sequential(*[Block(in_channel_0, out_channel_0), Block_Pool(out_channel_0, in_channel_1)])
self.blocks_2_2_2 = nn.Sequential(*[Block(in_channel_1, in_channel_1), Block_Pool(in_channel_1, out_channel_1)])
self.blocks_3_3_3 = nn.Sequential(*[Block(in_channel_2, out_channel_1), Block_Pool(out_channel_1, out_channel_2)])
self.blocks_4_4_4 = nn.Sequential(*[Block(in_channel_3, out_channel_2), Block(out_channel_2, out_channel_3)])
def reparametrize(self, mu, logvar):
std = logvar.mul(0.5).exp_()
# eps = torch.cuda.FloatTensor(std.size()).normal_()
eps = torch.FloatTensor(std.size()).normal_()
# eps = Variable(eps)
eps = Variable(eps).cuda()
return (eps.mul(std)).add_(mu)
def decode(self, z, captions):
captions = nn.utils.rnn.pad_sequence(captions, batch_first=True, padding_value=0)
lengths = [len(i_x) for i_x in captions]
x_emb = self.embedding(captions)
z_0 = z.unsqueeze(1).repeat(1, x_emb.size(1), 1)
x_input = torch.cat([x_emb, z_0], dim=-1)
x_input = pack_padded_sequence(x_input, lengths, batch_first=True)
h_0 = self.fc3(z)
h_0 = h_0.unsqueeze(0).repeat(3, 1, 1)
output, _ = self.gru(x_input, h_0)
output, _ = nn.utils.rnn.pad_packed_sequence(output, batch_first=True)
y = self.fc4(output)
return y
def forward(self, input_data, input_caption, mode, rec_scheme):
result = []
if mode == 'source':
# source private encoder
x = self.blocks_1(input_data)
x = torch.cat([self.blocks_2(x), x], dim=1)
x = torch.cat([self.blocks3(x), x], dim=1)
x = self.blocks4(x)
x = x.mean(2).mean(2).mean(2)
pri_feat_mu, pri_feat_logvar = self.fc11(x), self.fc12(x)
pri_latent = self.reparametrize(pri_feat_mu, pri_feat_logvar)
x_cap = [self.embedding(i_c.cuda()) for i_c in input_caption]
x_cap = nn.utils.rnn.pack_sequence(x_cap, enforce_sorted=False)
_, h = self.gru1(x_cap)
h = h[-(1 + int(self.gru1.bidirectional)):]
h = torch.cat(h.split(1), dim=-1).squeeze(0)
cap_mu, cap_logvar = self.fc11(h), self.fc12(h)
cap_pri_latent = self.reparametrize(cap_mu, cap_logvar)
elif mode == 'target':
# target private encoder
# x = pri_source_encoder(input_data)
# [1,52,16,16,16]
x = self.blocks_1_1(input_data)
x = torch.cat([self.blocks_2_2(x), x], dim=1)
x = torch.cat([self.blocks_3_3(x), x], dim=1)
x = self.blocks44(x)
# ==========修改的地方===============
x = x.mean(2).mean(2).mean(2)
pri_feat_mu, pri_feat_logvar = self.fc11(x), self.fc12(x)
pri_latent = self.reparametrize(pri_feat_mu, pri_feat_logvar)
x_cap = [self.embedding(i_c.cuda()) for i_c in input_caption]
x_cap = nn.utils.rnn.pack_sequence(x_cap, enforce_sorted=False)
_, h = self.gru1(x_cap)
h = h[-(1 + int(self.gru1.bidirectional)):]
h = torch.cat(h.split(1), dim=-1).squeeze(0)
cap_mu, cap_logvar = self.fc11(h), self.fc12(h)
cap_pri_latent = self.reparametrize(cap_mu, cap_logvar)
result.extend([pri_feat_mu, pri_feat_logvar, pri_latent, cap_mu, cap_logvar, cap_pri_latent])
# shared encoder
x = self.blocks_1_1_1(input_data)
x = torch.cat([self.blocks_2_2_2(x), x], dim=1)
x = torch.cat([self.blocks_3_3_3(x), x], dim=1)
x = self.blocks_4_4_4(x)
x = x.mean(2).mean(2).mean(2)
shr_feat_mu, shr_feat_logvar = self.fc11(x), self.fc12(x)
shr_latent = self.reparametrize(shr_feat_mu, shr_feat_logvar)
x_cap = [self.embedding(i_c.cuda()) for i_c in input_caption]
x_cap = nn.utils.rnn.pack_sequence(x_cap, enforce_sorted=False)
_, h = self.gru1(x_cap)
h = h[-(1 + int(self.gru1.bidirectional)):]
h = torch.cat(h.split(1), dim=-1).squeeze(0)
cap_mu, cap_logvar = self.fc11(h), self.fc12(h)
cap_shr_latent = self.reparametrize(cap_mu, cap_logvar)
result.extend([shr_latent, cap_shr_latent])
# shared decoder
if rec_scheme == 'share':
union_latent = cap_shr_latent
elif rec_scheme == 'all':
union_latent = cap_shr_latent + cap_pri_latent
elif rec_scheme == 'private':
union_latent = cap_pri_latent
# re_smi = self.decode(cap_pri_latent,caption,lengths)
cap = [i_c.cuda() for i_c in input_caption]
re_smi = self.decode(union_latent, cap)
result.append(re_smi)
return result
def load(cls, load_dir):
model_config_path = os.path.join(load_dir, "generator1_config.json")
with open(model_config_path, "r") as f:
config = json.load(f)
model = cls(**config)
model_weight_path = os.path.join(load_dir, "generator1_weight.pt")
try:
model_state_dict = torch.load(model_weight_path, map_location="cpu")
model.load_state_dict(model_state_dict)
except:
print("No pretrained weight for SmilesGenerator.")
return model
def save(self, save_dir):
model_config = self.config
model_config_path = os.path.join(save_dir, "generator_config1.json")
with open(model_config_path, "w") as f:
json.dump(model_config, f)
model_state_dict = self.state_dict()
model_weight_path = os.path.join(save_dir, "generator1_weight.pt")
torch.save(model_state_dict, model_weight_path)
@property
def config(self):
return dict(
in_channel_0=self.in_channel_0,
out_channel_0=self.in_channel_0,
in_channel_1=self.in_channel_1,
out_channel_1=self.in_channel_1,
in_channel_2=self.in_channel_2,
out_channel_2=self.in_channel_2,
in_channel_3=self.in_channel_3,
out_channel_3=self.in_channel_3
)
class generator_handler:
def __init__(self, model,char_dict,max_sampling_batch_size,optimizer):
self.model = model
self.max_sampling_batch_size = max_sampling_batch_size
self.char_dict = char_dict
self.max_seq_length = 121
def sample(self, num_samples, device):
action, log_prob, seq_length = self.sample_action(num_samples=num_samples, device=device)
smiles = self.char_dict.matrix_to_smiles(action, seq_length - 1)
return smiles, action, log_prob, seq_length
def sample_action(self, num_samples, device):
number_batches = (
num_samples + self.max_sampling_batch_size - 1
) // self.max_sampling_batch_size
remaining_samples = num_samples
action = torch.LongTensor(num_samples, self.max_seq_length).to(device)
log_prob = torch.FloatTensor(num_samples, self.max_seq_length).to(device)
seq_length = torch.LongTensor(num_samples).to(device)
batch_start = 0
for i in range(number_batches):
batch_size = min(self.max_sampling_batch_size, remaining_samples)
batch_end = batch_start + batch_size
action_batch, log_prob_batch, seq_length_batch = self._sample_action_batch(
batch_size, device
)
action[batch_start:batch_end, :] = action_batch
log_prob[batch_start:batch_end, :] = log_prob_batch
seq_length[batch_start:batch_end] = seq_length_batch
batch_start += batch_size
remaining_samples -= batch_size
return action, log_prob, seq_length
def train_on_batch(self, smis, device, weights=1.0):
actions, _ = smis_to_actions(self.char_dict, smis)
actions = torch.LongTensor(actions)
loss = self.train_on_action_batch(actions=actions, device=device, weights=weights)
return loss
def train_on_action_batch(self, actions, target, device, weights=1.0):
batch_size = actions.size(0)
batch_seq_length = actions.size(1)
actions = actions.to(device)
start_token_vector = self._get_start_token_vector(batch_size, device)
input_actions = torch.cat([start_token_vector, actions[:, :-1]], dim=1)
target_actions = actions
input_actions = input_actions.to(device)
target_actions = target_actions.to(device)
output, _ = self.model(input_actions, hidden=None)
output = output.view(batch_size * batch_seq_length, -1)
log_probs = torch.log_softmax(output, dim=1)
log_target_probs = log_probs.gather(dim=1, index=target_actions.reshape(-1, 1)).squeeze(
dim=1
)
log_target_probs = log_target_probs.view(batch_size, batch_seq_length).mean(dim=1)
loss = -(weights * log_target_probs).mean()
if self.entropy_factor > 0.0:
entropy = -torch.sum(torch.exp(log_probs) * log_probs, dim=1).mean()
loss -= self.entropy_factor * entropy
self.model.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
self.optimizer.step()
return loss.item()
def get_action_log_prob(self, actions, seq_lengths, device):
num_samples = actions.size(0)
actions_seq_length = actions.size(1)
log_probs = torch.FloatTensor(num_samples, actions_seq_length).to(device)
number_batches = (
num_samples + self.max_sampling_batch_size - 1
) // self.max_sampling_batch_size
remaining_samples = num_samples
batch_start = 0
for i in range(number_batches):
batch_size = min(self.max_sampling_batch_size, remaining_samples)
batch_end = batch_start + batch_size
log_probs[batch_start:batch_end, :] = self._get_action_log_prob_batch(
actions[batch_start:batch_end, :], seq_lengths[batch_start:batch_end], device
)
batch_start += batch_size
remaining_samples -= batch_size
return log_probs
def save(self, save_dir):
self.model.save(save_dir)
def _get_action_log_prob_batch(self, actions, seq_lengths, device):
batch_size = actions.size(0)
actions_seq_length = actions.size(1)
start_token_vector = self._get_start_token_vector(batch_size, device)
input_actions = torch.cat([start_token_vector, actions[:, :-1]], dim=1)
target_actions = actions
input_actions = input_actions.to(device)
target_actions = target_actions.to(device)
output, _ = self.model(input_actions, hidden=None)
output = output.view(batch_size * actions_seq_length, -1)
log_probs = torch.log_softmax(output, dim=1)
log_target_probs = log_probs.gather(dim=1, index=target_actions.reshape(-1, 1)).squeeze(
dim=1
)
log_target_probs = log_target_probs.view(batch_size, self.max_seq_length)
mask = torch.arange(actions_seq_length).expand(len(seq_lengths), actions_seq_length) > (
seq_lengths - 1
).unsqueeze(1)
log_target_probs[mask] = 0.0
return log_target_probs
def _sample_action_batch(self, batch_size, device):
hidden = None
inp = self._get_start_token_vector(batch_size, device)
action = torch.zeros((batch_size, self.max_seq_length), dtype=torch.long).to(device)
log_prob = torch.zeros((batch_size, self.max_seq_length), dtype=torch.float).to(device)
seq_length = torch.zeros(batch_size, dtype=torch.long).to(device)
ended = torch.zeros(batch_size, dtype=torch.bool).to(device)
for t in range(self.max_seq_length):
output, hidden = self.model(inp, hidden)
prob = torch.softmax(output, dim=2)
distribution = Categorical(probs=prob)
action_t = distribution.sample()
log_prob_t = distribution.log_prob(action_t)
inp = action_t
action[~ended, t] = action_t.squeeze(dim=1)[~ended]
log_prob[~ended, t] = log_prob_t.squeeze(dim=1)[~ended]
seq_length += (~ended).long()
ended = ended | (action_t.squeeze(dim=1) == self.char_dict.end_idx).bool()
if ended.all():
break
return action, log_prob, seq_length
def _get_start_token_vector(self, batch_size, device):
return torch.LongTensor(batch_size, 1).fill_(self.char_dict.begin_idx).to(device)
class generator_test(nn.Module):
def __init__(self, input_size, hidden_size, output_size, n_layers, lstm_dropout):
super(generator_test, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.lstm_dropout = lstm_dropout
self.encoder = nn.Embedding(input_size, hidden_size)
self.decoder = nn.Linear(hidden_size, output_size)
self.lstm = nn.LSTM(
hidden_size, hidden_size, batch_first=True, num_layers=n_layers, dropout=lstm_dropout
)
self.init_weights()
def init_weights(self):
nn.init.xavier_uniform_(self.encoder.weight)
nn.init.xavier_uniform_(self.decoder.weight)
nn.init.constant_(self.decoder.bias, 0)
for name, param in self.lstm.named_parameters():
if "weight" in name:
nn.init.orthogonal_(param)
elif "bias" in name:
nn.init.constant_(param, 0)
r_gate = param[int(0.25 * len(param)) : int(0.5 * len(param))]
nn.init.constant_(r_gate, 1)
def forward(self, x, hidden):
embeds = self.encoder(x)
output, hidden = self.lstm(embeds, hidden)
output = self.decoder(output)
return output, hidden
@classmethod
def load(cls, load_dir):
model_config_path = os.path.join(load_dir, "generator_config.json")
with open(model_config_path, "r") as f:
config = json.load(f)
model = cls(**config)
model_weight_path = os.path.join(load_dir, "generator_weight.pt")
try:
model_state_dict = torch.load(model_weight_path, map_location="cpu")
rnn_keys = [key for key in model_state_dict if key.startswith("rnn")]
for key in rnn_keys:
weight = model_state_dict.pop(key)
model_state_dict[key.replace("rnn", "lstm")] = weight
model.load_state_dict(model_state_dict)
except:
print("No pretrained weight for SmilesGenerator.")
return model
def save(self, save_dir):
model_config = self.config
model_config_path = os.path.join(save_dir, "generator_config.json")
with open(model_config_path, "w") as f:
json.dump(model_config, f)
model_state_dict = self.state_dict()
model_weight_path = os.path.join(save_dir, "generator_weight.pt")
torch.save(model_state_dict, model_weight_path)
@property
def config(self):
return dict(
input_size=self.input_size,
hidden_size=self.hidden_size,
output_size=self.output_size,
n_layers=self.n_layers,
lstm_dropout=self.lstm_dropout,
)
class generator_test_handler:
def __init__(self, model, optimizer, char_dict, max_sampling_batch_size, entropy_factor=0.0):
self.model = model
self.optimizer = optimizer
self.max_sampling_batch_size = max_sampling_batch_size
self.entropy_factor = entropy_factor
self.char_dict = char_dict
self.max_seq_length = 121
def sample(self, num_samples, device):
action, log_prob, seq_length = self.sample_action(num_samples=num_samples, device=device)
smiles = self.char_dict.matrix_to_smiles(action, seq_length - 1)
return smiles, action, log_prob, seq_length
def sample_action(self, num_samples, device):
number_batches = (
num_samples + self.max_sampling_batch_size - 1
) // self.max_sampling_batch_size
remaining_samples = num_samples
action = torch.LongTensor(num_samples, self.max_seq_length).to(device)
log_prob = torch.FloatTensor(num_samples, self.max_seq_length).to(device)
seq_length = torch.LongTensor(num_samples).to(device)
batch_start = 0
for i in range(number_batches):
batch_size = min(self.max_sampling_batch_size, remaining_samples)
batch_end = batch_start + batch_size
action_batch, log_prob_batch, seq_length_batch = self._sample_action_batch(
batch_size, device
)
action[batch_start:batch_end, :] = action_batch
log_prob[batch_start:batch_end, :] = log_prob_batch
seq_length[batch_start:batch_end] = seq_length_batch
batch_start += batch_size
remaining_samples -= batch_size
return action, log_prob, seq_length
def train_on_batch(self, smis, device, weights=1.0):
actions, _ = smis_to_actions(self.char_dict, smis)
actions = torch.LongTensor(actions)
loss = self.train_on_action_batch(actions=actions, device=device, weights=weights)
return loss
def train_on_action_batch(self, actions, device, weights=1.0):
batch_size = actions.size(0)
batch_seq_length = actions.size(1)
actions = actions.to(device)
start_token_vector = self._get_start_token_vector(batch_size, device)
input_actions = torch.cat([start_token_vector, actions[:, :-1]], dim=1)
target_actions = actions
input_actions = input_actions.to(device)
target_actions = target_actions.to(device)
output, _ = self.model(input_actions, hidden=None)
output = output.view(batch_size * batch_seq_length, -1)
log_probs = torch.log_softmax(output, dim=1)
log_target_probs = log_probs.gather(dim=1, index=target_actions.reshape(-1, 1)).squeeze(
dim=1
)
log_target_probs = log_target_probs.view(batch_size, batch_seq_length).mean(dim=1)
loss = -(weights * log_target_probs).mean()
if self.entropy_factor > 0.0:
entropy = -torch.sum(torch.exp(log_probs) * log_probs, dim=1).mean()
loss -= self.entropy_factor * entropy
self.model.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
self.optimizer.step()
return loss.item()
def get_action_log_prob(self, actions, seq_lengths, device):
num_samples = actions.size(0)
actions_seq_length = actions.size(1)
log_probs = torch.FloatTensor(num_samples, actions_seq_length).to(device)
number_batches = (
num_samples + self.max_sampling_batch_size - 1
) // self.max_sampling_batch_size
remaining_samples = num_samples
batch_start = 0
for i in range(number_batches):
batch_size = min(self.max_sampling_batch_size, remaining_samples)
batch_end = batch_start + batch_size
log_probs[batch_start:batch_end, :] = self._get_action_log_prob_batch(
actions[batch_start:batch_end, :], seq_lengths[batch_start:batch_end], device
)
batch_start += batch_size
remaining_samples -= batch_size
return log_probs
def save(self, save_dir):
self.model.save(save_dir)
def _get_action_log_prob_batch(self, actions, seq_lengths, device):
batch_size = actions.size(0)
actions_seq_length = actions.size(1)
start_token_vector = self._get_start_token_vector(batch_size, device)
input_actions = torch.cat([start_token_vector, actions[:, :-1]], dim=1)
target_actions = actions
input_actions = input_actions.to(device)
target_actions = target_actions.to(device)
output, _ = self.model(input_actions, hidden=None)
output = output.view(batch_size * actions_seq_length, -1)
log_probs = torch.log_softmax(output, dim=1)
log_target_probs = log_probs.gather(dim=1, index=target_actions.reshape(-1, 1)).squeeze(
dim=1
)
log_target_probs = log_target_probs.view(batch_size, self.max_seq_length)
mask = torch.arange(actions_seq_length).expand(len(seq_lengths), actions_seq_length) > (
seq_lengths - 1
).unsqueeze(1)
log_target_probs[mask] = 0.0
return log_target_probs
def _sample_action_batch(self, batch_size, device):
hidden = None
inp = self._get_start_token_vector(batch_size, device)
action = torch.zeros((batch_size, self.max_seq_length), dtype=torch.long).to(device)
log_prob = torch.zeros((batch_size, self.max_seq_length), dtype=torch.float).to(device)
seq_length = torch.zeros(batch_size, dtype=torch.long).to(device)
ended = torch.zeros(batch_size, dtype=torch.bool).to(device)
for t in range(self.max_seq_length):
output, hidden = self.model(inp, hidden)
prob = torch.softmax(output, dim=2)
distribution = Categorical(probs=prob)
action_t = distribution.sample()
log_prob_t = distribution.log_prob(action_t)
inp = action_t
action[~ended, t] = action_t.squeeze(dim=1)[~ended]
log_prob[~ended, t] = log_prob_t.squeeze(dim=1)[~ended]
seq_length += (~ended).long()
ended = ended | (action_t.squeeze(dim=1) == self.char_dict.end_idx).bool()
if ended.all():
break
return action, log_prob, seq_length
def _get_start_token_vector(self, batch_size, device):
return torch.LongTensor(batch_size, 1).fill_(self.char_dict.begin_idx).to(device)