-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.py
166 lines (138 loc) · 6.05 KB
/
test.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
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import, division, print_function
import math, os, random, sys, time
import cPickle, gzip
import progressbar
import pprint
import glob, shutil
import numpy as np
from six.moves import xrange
import tensorflow as tf
import matplotlib
matplotlib.use('pdf')
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from tensorflow.models.rnn.translate import data_utils
import seq2seq_model
from rdkit import Chem
from rdkit.Chem import AllChem
import parser.Smipar as Smipar
pp = pprint.PrettyPrinter()
flags = tf.app.flags
flags.DEFINE_float("learning_rate", 0.5, "Learning rate.")
flags.DEFINE_float("learning_rate_decay_factor", 0.99,
"Learning rate decays by this much.")
flags.DEFINE_float("max_gradient_norm", 5.0,
"Clip gradients to this norm.")
flags.DEFINE_integer("batch_size", 64,
"Batch size to use during training.")
flags.DEFINE_integer("size", 600, "Size of each model layer.")
flags.DEFINE_integer("num_layers", 3, "Number of layers in the model.")
flags.DEFINE_integer("reactant_vocab_size", 311, "Reactant vocabulary size.")
flags.DEFINE_integer("product_vocab_size", 180, "Product vocabulary size.")
flags.DEFINE_string("train_dir", "checkpoint/saved_models", "Training dir.")
flags.DEFINE_integer("max_train_data_size", 0,
"Limit on the size of training data (0: no limit).")
flags.DEFINE_integer("steps_per_checkpoint", 1000,
"How many training steps to do per checkpoint.")
flags.DEFINE_boolean("decode", False,
"Set to True for interactive decoding.")
flags.DEFINE_boolean("self_test", False,
"Run a self-test if this is set to True.")
FLAGS = flags.FLAGS
pp.pprint(flags.FLAGS.__flags)
_buckets = [(54, 54), (70, 60), (90, 65), (150, 80)]
# vocab loader
with gzip.open('data/vocab/vocab_list.pkl.gz', 'rb') as list_file:
reactants_token_list, products_token_list = cPickle.load(list_file)
def create_model(session, forward_only):
model = seq2seq_model.Seq2SeqModel(
FLAGS.reactant_vocab_size, FLAGS.product_vocab_size, _buckets,
FLAGS.size, FLAGS.num_layers, FLAGS.max_gradient_norm, FLAGS.batch_size,
FLAGS.learning_rate, FLAGS.learning_rate_decay_factor,
forward_only=forward_only)
ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
model.saver.restore(session, ckpt.model_checkpoint_path)
else:
print("Created model with fresh parameters.")
session.run(tf.initialize_all_variables())
return model
def cano(smiles): # canonicalize smiles by MolToSmiles function
return Chem.MolToSmiles(Chem.MolFromSmiles(smiles)) if (smiles != '') else ''
def decode():
with tf.Session() as sess:
# Create model and load parameters.
model = create_model(sess, True)
model.batch_size = 1
enc_embed = 'embedding_attention_seq2seq/RNN/EmbeddingWrapper/embedding:0'
dec_embed = 'embedding_attention_seq2seq/embedding_attention_decoder/embedding:0'
e, d = sess.run([enc_embed, dec_embed])
print(e.shape, d.shape)
with open('embedding.pkl', 'w') as f:
cPickle.dump((e, d), f, 2)
# plt.imshow(e, interpolation='nearest')
# Decode from standard input.
sys.stdout.write("> ")
sys.stdout.flush()
rsmi = sys.stdin.readline()
while rsmi:
reactant_list = []
agent_list = []
split_rsmi = rsmi.split('>')
reactants = cano(split_rsmi[0]).split('.')
agents = cano(split_rsmi[1]).split('.')
for reactant in reactants:
reactant_list += Smipar.parser_list(reactant)
reactant_list += '.'
for agent in agents:
agent_list += Smipar.parser_list(agent)
agent_list += '.'
reactant_list.pop() # to pop last '.'
agent_list.pop()
reactant_list += '>'
reactant_list += agent_list
token_ids = [reactants_token_list.index(r) for r in reactant_list]
# Which bucket does it belong to?
bucket_id = min([b for b in xrange(len(_buckets))
if _buckets[b][0] > len(token_ids)])
# Get a 1-element batch to feed the sentence to the model.
encoder_inputs, decoder_inputs, target_weights = model.get_batch(
{bucket_id: [(token_ids, [])]}, bucket_id)
# Get output logits for the sentence.
attn, _, output_logits = model.step(sess, encoder_inputs, decoder_inputs,
target_weights, bucket_id, True)
# This is a greedy decoder - outputs are just argmaxes of output_logits.
outputs = [int(np.argmax(logit, axis=1)) for logit in output_logits]
# If there is an EOS symbol in outputs, cut them at that point.
if data_utils.EOS_ID in outputs:
outputs = outputs[:outputs.index(data_utils.EOS_ID)]
# Print out the reaction smiles
products = ''.join([tf.compat.as_str(products_token_list[output])
for output in outputs])
print(products)
attn_matrix = np.squeeze(np.stack(attn))
print(attn_matrix.shape)
with open('attn_'+products+'.pkl', 'w') as f:
cPickle.dump(attn_matrix, f, 2)
print("> ", end="")
sys.stdout.flush()
rsmi = sys.stdin.readline()
def main(_):
decode()
if __name__ == "__main__":
tf.app.run()