-
Notifications
You must be signed in to change notification settings - Fork 41
/
decode.py
executable file
·60 lines (48 loc) · 1.94 KB
/
decode.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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import math, random, sys
import numpy as np
import argparse
import rdkit
from tqdm import tqdm
from fuseprop import *
lg = rdkit.RDLogger.logger()
lg.setLevel(rdkit.RDLogger.CRITICAL)
parser = argparse.ArgumentParser()
parser.add_argument('--rationale', default=None)
parser.add_argument('--atom_vocab', default=common_atom_vocab)
parser.add_argument('--model', required=True)
parser.add_argument('--num_decode', type=int, default=100)
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--rnn_type', type=str, default='LSTM')
parser.add_argument('--hidden_size', type=int, default=400)
parser.add_argument('--embed_size', type=int, default=400)
parser.add_argument('--batch_size', type=int, default=20)
parser.add_argument('--latent_size', type=int, default=20)
parser.add_argument('--depth', type=int, default=10)
parser.add_argument('--diter', type=int, default=3)
args = parser.parse_args()
random.seed(1)
model = AtomVGNN(args).cuda()
model_ckpt = torch.load(args.model)
if type(model_ckpt) is tuple:
print('loading model with rationale distribution', file=sys.stderr)
testdata = list(model_ckpt[0].keys())
model.load_state_dict(model_ckpt[1])
else:
print('loading pre-trained model', file=sys.stderr)
testdata = [line.split()[1] for line in open(args.rationale)]
testdata = unique_rationales(testdata)
model.load_state_dict(model_ckpt)
print('total # rationales:', len(testdata), file=sys.stderr)
model.eval()
dataset = SubgraphDataset(testdata, args.atom_vocab, args.batch_size, args.num_decode)
loader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=0, collate_fn=lambda x:x[0])
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
with torch.no_grad():
for init_smiles in tqdm(loader):
final_smiles = model.decode(init_smiles)
for x,y in zip(init_smiles, final_smiles):
print(x, y)