forked from microsoft/gated-graph-neural-network-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chem_tensorflow_gcn.py
executable file
·209 lines (174 loc) · 9.79 KB
/
chem_tensorflow_gcn.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
#!/usr/bin/env/python
'''
Usage:
chem_tensorflow_gcn.py [options]
Options:
-h --help Show this screen.
--config-file FILE Hyperparameter configuration file path (in JSON format)
--config CONFIG Hyperparameter configuration dictionary (in JSON format)
--log_dir NAME log dir name
--data_dir NAME data dir name
--restore FILE File to restore weights from.
--freeze-graph-model Freeze weights of graph model components.
'''
from typing import Tuple, Sequence, Any
from docopt import docopt
import numpy as np
import tensorflow as tf
import sys, traceback
import pdb
from chem_tensorflow import ChemModel
from utils import glorot_init
class SparseGCNChemModel(ChemModel):
def __init__(self, args):
super().__init__(args)
@classmethod
def default_params(cls):
params = dict(super().default_params())
params.update({'batch_size': 100000,
'gcn_use_bias': False})
return params
def prepare_specific_graph_model(self) -> None:
h_dim = self.params['hidden_size']
self.placeholders['initial_node_representation'] = tf.placeholder(tf.float32, [None, h_dim],
name='node_features')
self.placeholders['adjacency_list'] = tf.placeholder(tf.int64, [None, 2], name='adjacency_list')
self.placeholders['adjacency_weights'] = tf.placeholder(tf.float32, [None], name='adjacency_weights')
self.placeholders['graph_nodes_list'] = tf.placeholder(tf.int64, [None, 2], name='graph_nodes_list')
with tf.variable_scope('gcn_scope'):
self.weights['edge_weights'] = [tf.Variable(glorot_init((h_dim, h_dim)), name="gcn_weights_%i" % i)
for i in range(self.params['num_timesteps'])]
if self.params['gcn_use_bias']:
self.weights['edge_biases'] = [tf.Variable(np.zeros([h_dim], dtype=np.float32), name="gcn_bias_%i" % i)
for i in range(self.params['num_timesteps'])]
def compute_final_node_representations(self):
with tf.variable_scope('gcn_scope'):
cur_node_states = self.placeholders['initial_node_representation'] # number of nodes in batch v x D
num_nodes = tf.shape(self.placeholders['initial_node_representation'], out_type=tf.int64)[0]
adjacency_matrix = tf.SparseTensor(indices=self.placeholders['adjacency_list'],
values=self.placeholders['adjacency_weights'],
dense_shape=[num_nodes, num_nodes])
for layer_idx in range(self.params['num_timesteps']):
scaled_cur_node_states = tf.sparse_tensor_dense_matmul(adjacency_matrix, cur_node_states) # v x D
new_node_states = tf.matmul(scaled_cur_node_states, self.weights['edge_weights'][layer_idx])
if self.params['gcn_use_bias']:
new_node_states += self.weights['edge_biases'][layer_idx] # v x D
# On all but final layer do ReLU and dropout:
if layer_idx < self.params['num_timesteps'] - 1:
new_node_states = tf.nn.relu(new_node_states)
new_node_states = tf.nn.dropout(new_node_states, keep_prob=self.placeholders['dropout_keep_prob'])
cur_node_states = new_node_states
return cur_node_states
def gated_regression(self, last_h, regression_gate, regression_transform):
# last_h: [v x h]
gate_input = tf.concat([last_h, self.placeholders['initial_node_representation']], axis=-1) # [v x 2h]
gated_outputs = tf.nn.sigmoid(regression_gate(gate_input)) * regression_transform(last_h) # [(b*v) x 1]
# Sum up all nodes per-graph
num_nodes = tf.shape(gate_input, out_type=tf.int64)[0]
graph_nodes = tf.SparseTensor(indices=self.placeholders['graph_nodes_list'],
values=tf.ones_like(self.placeholders['graph_nodes_list'][:, 0],
dtype=tf.float32),
dense_shape=[self.placeholders['num_graphs'], num_nodes]) # [g x v]
return tf.squeeze(tf.sparse_tensor_dense_matmul(graph_nodes, gated_outputs), axis=[-1]) # [g]
# ----- Data preprocessing and chunking into minibatches:
def process_raw_graphs(self, raw_data: Sequence[Any], is_training_data: bool) -> Any:
processed_graphs = []
for d in raw_data:
(adjacency_list, adjacency_weights) = self.__graph_to_adjacency_list(d['graph'], len(d["node_features"]))
processed_graphs.append({"adjacency_list": adjacency_list,
"adjacency_weights": adjacency_weights,
"init": d["node_features"],
"labels": [d["targets"][task_id][0] for task_id in self.params['task_ids']]})
if is_training_data:
np.random.shuffle(processed_graphs)
for task_id in self.params['task_ids']:
task_sample_ratio = self.params['task_sample_ratios'].get(str(task_id))
if task_sample_ratio is not None:
ex_to_sample = int(len(processed_graphs) * task_sample_ratio)
for ex_id in range(ex_to_sample, len(processed_graphs)):
processed_graphs[ex_id]['labels'][task_id] = None
return processed_graphs
def __graph_to_adjacency_list(self, graph, num_nodes: int) -> Tuple[np.ndarray, np.ndarray]:
# Step 1: Generate adjacency matrices:
adj_matrix = np.zeros((num_nodes, num_nodes))
for src, _, dest in graph:
adj_matrix[src, dest] = 1
adj_matrix[dest, src] = 1
# Step 2: Introduce self loops:
self_loops = np.eye(num_nodes)
adj_matrix += self_loops
# Step 3: Normalize adj_matrices so that scale of vectors doesn't explode:
row_sum = np.sum(adj_matrix, axis=-1)
D_inv_sqrt = np.diag(np.power(row_sum, -0.5).flatten() + 1e-7)
adj_matrix = D_inv_sqrt.dot(adj_matrix).dot(D_inv_sqrt)
# Step 4: Turn into sorted adjacency lists:
final_adj_list = []
final_adj_weights = []
for i in range(num_nodes):
for j in range(num_nodes):
w = adj_matrix[i, j]
if w != 0:
final_adj_list.append([i,j])
final_adj_weights.append(w)
return np.array(final_adj_list), np.array(final_adj_weights)
def make_minibatch_iterator(self, data: Any, is_training: bool):
"""Create minibatches by flattening adjacency matrices into a single adjacency matrix with
multiple disconnected components."""
if is_training:
np.random.shuffle(data)
# Pack until we cannot fit more graphs in the batch
num_graphs = 0
while num_graphs < len(data):
num_graphs_in_batch = 0
batch_node_features = []
batch_target_task_values = []
batch_target_task_mask = []
batch_adjacency_list = []
batch_adjacency_weights = []
batch_graph_nodes_list = []
node_offset = 0
while num_graphs < len(data) and node_offset + len(data[num_graphs]['init']) < self.params['batch_size']:
cur_graph = data[num_graphs]
num_nodes_in_graph = len(cur_graph['init'])
padded_features = np.pad(cur_graph['init'],
((0, 0), (0, self.params['hidden_size'] - self.annotation_size)),
mode='constant')
batch_node_features.extend(padded_features)
batch_graph_nodes_list.extend((num_graphs_in_batch, node_offset + i) for i in range(num_nodes_in_graph))
batch_adjacency_list.append(cur_graph['adjacency_list'] + node_offset)
batch_adjacency_weights.append(cur_graph['adjacency_weights'])
target_task_values = []
target_task_mask = []
for target_val in cur_graph['labels']:
if target_val is None: # This is one of the examples we didn't sample...
target_task_values.append(0.)
target_task_mask.append(0.)
else:
target_task_values.append(target_val)
target_task_mask.append(1.)
batch_target_task_values.append(target_task_values)
batch_target_task_mask.append(target_task_mask)
num_graphs += 1
num_graphs_in_batch += 1
node_offset += num_nodes_in_graph
batch_feed_dict = {
self.placeholders['initial_node_representation']: np.array(batch_node_features),
self.placeholders['adjacency_list']: np.concatenate(batch_adjacency_list, axis=0),
self.placeholders['adjacency_weights']: np.concatenate(batch_adjacency_weights, axis=0),
self.placeholders['graph_nodes_list']: np.array(batch_graph_nodes_list, dtype=np.int32),
self.placeholders['target_values']: np.transpose(batch_target_task_values, axes=[1,0]),
self.placeholders['target_mask']: np.transpose(batch_target_task_mask, axes=[1, 0]),
self.placeholders['num_graphs']: num_graphs_in_batch,
}
yield batch_feed_dict
def main():
args = docopt(__doc__)
try:
model = SparseGCNChemModel(args)
model.train()
except:
typ, value, tb = sys.exc_info()
traceback.print_exc()
pdb.post_mortem(tb)
if __name__ == "__main__":
main()