-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnn.py
304 lines (253 loc) · 14.3 KB
/
gnn.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
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if len(gpus) > 0:
tf.config.experimental.set_memory_growth(gpus[0], True)
import os
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
os.environ['TF_MKL_REUSE_PRIMITIVE_MEMORY'] = '0'
from tensorflow import keras
from tensorflow.keras import layers
from dgl.nn.tensorflow import GATConv
import gc
import sys
import numpy as np
class GAT(tf.keras.Model):
def __init__(self,
num_layers,
in_dim,
num_hidden,
num_classes,
heads,
activation,
feat_drop,
attn_drop,
negative_slope,
residual,
equation):
super(GAT, self).__init__()
self.num_layers = num_layers
self.gat_layers = []
self.dense_layers = []
self.activation = activation
# input projection (no residual)
self.gat_layers.append(GATConv(
in_dim, num_hidden, heads[0],
feat_drop, attn_drop, negative_slope, False, self.activation, allow_zero_in_degree=True))
# hidden layers
for l in range(1, num_layers):
# due to multi-head, the in_dim = num_hidden * num_heads
self.gat_layers.append(GATConv(
num_hidden * heads[l-1], num_hidden, heads[l],
feat_drop, attn_drop, negative_slope, residual, self.activation, allow_zero_in_degree=True))
# output projection
self.gat_layers.append(GATConv(
num_hidden * heads[-2], num_classes, heads[-1],
feat_drop, attn_drop, negative_slope, residual, None, allow_zero_in_degree=True))
# Dense layers for updating atom and global feature vectors
# These dense layers are applied ...
# For atom feature update - 0-1: Atom feature term, 2-3: Global feature term
# For global feature update - 4-5: Averaged atom feature term, 6-7: Global feature term
for l in range(8):
self.dense_layers.append(layers.Dense(num_hidden))
# Readout layers after concatenating atom and global feature vectors
if equation == '':
self.readout_layers = [layers.Dense(2*num_hidden, activation='relu'), layers.Dense(num_hidden, activation='relu'), \
layers.Dense(1, activation='relu', kernel_initializer = tf.keras.initializers.Ones()) ]
elif equation == 'Watson':
self.readout_layers = [layers.Dense(num_hidden), layers.Dense(3) ]
# A dense layer for embedding of temperature as a global feature. (batch_size, 1) -> (batch_size, num_hidden)
self.T_embedding = layers.Dense(num_hidden)
def call(self, features, g, segment, Max_atoms, T, equation, num_mols, training, verbose=False, mu_s_NLR = []):
# Shape of T: (batch_size), T_part: (batch_size, 1)
if len(T.shape) == 1:
T_part = tf.reshape(T, (-1, 1))
else:
T_part = T
# (batch_size, 1) -> (batch_size, embed_size)
T_part = self.T_embedding(T_part)
# One-hot atom feature vectors as initial values
# Note: The DGL graph 'g' contains ALL molecular graphs in the batch in one variable.
# Thus the shape of 'features' is: (batch_size * Max_atoms, dim_one_hot_vector)
h = features
atom_features_each_layer = []
for l in range(self.num_layers):
# 'heads' dimension added after passing a GAT layer
h = self.gat_layers[l](g, h)
# (batch_size * Max_atoms, num_heads, dim_hidden_layer) -> (batch_size * Max_atoms, num_heads * dim_hidden_layer)
h = tf.reshape(h, (h.shape[0], -1))
if verbose:
a_feature_lth = tf.reduce_mean(self.gat_layers[l](g, h), axis=1)
atom_features_each_layer.append(a_feature_lth)
# Averaging over all heads
# (batch_size * Max_atoms, num_heads, dim_hidden_layer) -> (batch_size * Max_atoms, dim_hidden_layer)
h = tf.reduce_mean(self.gat_layers[-1](g, h), axis=1)
# (batch_size * Max_atoms, dim_hidden_layer) -> (batch_size, Max_atoms, dim_hidden_layer)
updated_atom_features = tf.reshape(h, (-1, Max_atoms, h.shape[-1]))
#reshape for tf.repeat: (batch_size, Max_atoms, dim_hidden_layer) -> (batch_size, Max_atoms*dim_hidden_layer)
updated_atom_features = tf.reshape(updated_atom_features, (updated_atom_features.shape[0], -1))
updated_atom_features = tf.repeat(input = updated_atom_features, repeats = num_mols, axis = 0)
#(batch_size in terms of each data point, Max_atoms*dim_hidden_layer) -> (batch_size, Max_atoms, dim_hidden_layer)
updated_atom_features = tf.reshape(updated_atom_features, (updated_atom_features.shape[0], Max_atoms, -1))
if equation == '':
update1 = self.dense_layers[1](self.dense_layers[0](updated_atom_features))
# (batch_size, embed_size) -> (batch_size, Max_atoms, embed_size)
update2 = layers.RepeatVector(Max_atoms)(self.dense_layers[3](self.dense_layers[2](T_part)))
# embed_size should equals to dim_hidden_layer
updated_atom_features = updated_atom_features + layers.ReLU()( update1+update2 )
# (batch_size, Max_atoms, dim_hidden_layer) -> (batch_size * Max_atoms, dim_hidden_layer)
# for removing dummy atoms by using unsorted_segment_mean
updated_atom_features = tf.reshape(updated_atom_features, ( updated_atom_features.shape[0] * updated_atom_features.shape[1], -1 ) )
# (batch_size * Max_atoms, dim_hidden_layer) -> (batch_size, dim_hidden_layer)
mean_atom_features = tf.math.unsorted_segment_mean( updated_atom_features, segment_ids = segment, num_segments = T_part.shape[0])
# (batch_size, embed_size)
update3 = self.dense_layers[5](self.dense_layers[4](mean_atom_features))
update4 = self.dense_layers[7](self.dense_layers[6](T_part))
updated_T = T_part + layers.ReLU()(update3 + update4)
# (batch_size, embed_size + dim_hidden_layer)
concat_vec = tf.concat([updated_T,mean_atom_features], -1)
elif equation == 'Watson':
updated_atom_features = tf.reshape(updated_atom_features, ( updated_atom_features.shape[0] * updated_atom_features.shape[1], -1 ) )
concat_vec = tf.math.unsorted_segment_mean( updated_atom_features, segment_ids = segment, num_segments = T_part.shape[0] )
# readout
h = self.readout_layers[0](concat_vec)
for l in range(1,len(self.readout_layers)):
h = self.readout_layers[l](h)
if equation == '':
# (batch_size, 1) -> (batch_size)
pred = tf.reshape(h, [-1])
elif equation == 'Watson':
# A,B,C = Z * sigma + mu. Sigma, mu determined from non-linear regression of training set
m_a, s_a, m_b, s_b, m_c, s_c = mu_s_NLR
Z_A, Z_B, Z_C = h[:,0], h[:,1], h[:,2]
A = s_a * Z_A + m_a
B = tf.nn.relu(s_b * Z_B + m_b - T)
C = s_c * Z_C + m_c
pred = A * ((B)** C)
if verbose:
if equation == 'Watson':
return pred, A, B, C
else:
return pred, atom_features_each_layer
else:
return pred
def save_model(self, name):
super(GAT, self).save_weights(name)
def load_model(self, name):
super(GAT, self).load_weights(name)
class GAT_unc(tf.keras.Model):
def __init__(self,
num_layers,
in_dim,
num_hidden,
num_classes,
heads,
activation,
feat_drop,
attn_drop,
negative_slope,
residual,
equation):
super(GAT_unc, self).__init__()
self.num_layers = num_layers
self.gat_layers = []
self.dense_layers = []
self.activation = activation
# input projection (no residual)
self.gat_layers.append(GATConv(
in_dim, num_hidden, heads[0],
feat_drop, attn_drop, negative_slope, False, self.activation, allow_zero_in_degree=True))
# hidden layers
for l in range(1, num_layers):
# due to multi-head, the in_dim = num_hidden * num_heads
self.gat_layers.append(GATConv(
num_hidden * heads[l-1], num_hidden, heads[l],
feat_drop, attn_drop, negative_slope, residual, self.activation, allow_zero_in_degree=True))
# output projection
self.gat_layers.append(GATConv(
num_hidden * heads[-2], num_classes, heads[-1],
feat_drop, attn_drop, negative_slope, residual, None, allow_zero_in_degree=True))
# Dense layers for updating atom and global feature vectors
# These dense layers are applied ...
# For atom feature update - 0-1: Atom feature term, 2-3: Global feature term
# For global feature update - 4-5: Averaged atom feature term, 6-7: Global feature term
for l in range(8):
self.dense_layers.append(layers.Dense(num_hidden))
# Readout layers after concatenating atom and global feature vectors
if equation == '':
self.readout_layers = [layers.Dense(2*num_hidden, activation='relu'),\
layers.Dense(num_hidden, activation='relu'), \
layers.Dense(2, activation='relu',\
kernel_initializer = tf.keras.initializers.Ones(), \
bias_initializer = tf.keras.initializers.GlorotNormal()) ]
# A dense layer for embedding of temperature as a global feature. (batch_size, 1) -> (batch_size, num_hidden)
self.T_embedding = layers.Dense(num_hidden)
def call(self, features, g, segment, Max_atoms, T, equation, num_mols, training, verbose=False, mu_s_NLR=[]):
# Shape of T: (batch_size), T_part: (batch_size, 1)
if len(T.shape) == 1:
T_part = tf.reshape(T, (-1, 1))
else:
T_part = T
# (batch_size, 1) -> (batch_size, embed_size)
T_part = self.T_embedding(T_part)
# One-hot atom feature vectors as initial values
# Note: The DGL graph 'g' contains ALL molecular graphs in the batch in one variable.
# Thus the shape of 'features' is: (batch_size * Max_atoms, dim_one_hot_vector)
h = features
atom_features_each_layer = []
Attention_each_layer = []
for l in range(self.num_layers):
# To extract atom feature vector and attention score matrix
if verbose:
print(l)
a_feature_lth, Attention_lth = self.gat_layers[l](g, h, True)
a_feature_lth = tf.reduce_mean(a_feature_lth, axis = 1)
Attention_lth = tf.reduce_mean(Attention_lth, axis = 1)
atom_features_each_layer.append(a_feature_lth)
Attention_each_layer.append(Attention_lth)
# 'heads' dimension added after passing a GAT layer
h = self.gat_layers[l](g, h)
# (batch_size * Max_atoms, num_heads, dim_hidden_layer) -> (batch_size * Max_atoms, num_heads * dim_hidden_layer)
h = tf.reshape(h, (h.shape[0], -1))
# Averaging over all heads
# (batch_size * Max_atoms, num_heads, dim_hidden_layer) -> (batch_size * Max_atoms, dim_hidden_layer)
h = tf.reduce_mean(self.gat_layers[-1](g, h), axis=1)
# (batch_size * Max_atoms, dim_hidden_layer) -> (batch_size, Max_atoms, dim_hidden_layer)
updated_atom_features = tf.reshape(h, (-1, Max_atoms, h.shape[-1]))
# reshape for tf.repeat: (batch_size, Max_atoms, dim_hidden_layer) -> (batch_size, Max_atoms*dim_hidden_layer)
updated_atom_features = tf.reshape(updated_atom_features, (updated_atom_features.shape[0], -1))
updated_atom_features = tf.repeat(input = updated_atom_features, repeats = num_mols, axis = 0)
#(batch_size in terms of each data point, Max_atoms*dim_hidden_layer) -> (batch_size, Max_atoms, dim_hidden_layer)
updated_atom_features = tf.reshape(updated_atom_features, (updated_atom_features.shape[0], Max_atoms, -1))
if equation == '':
update1 = self.dense_layers[1](self.dense_layers[0](updated_atom_features))
# (batch_size, embed_size) -> (batch_size, Max_atoms, embed_size)
update2 = layers.RepeatVector(Max_atoms)(self.dense_layers[3](self.dense_layers[2](T_part)))
a_feat_before_T_update = updated_atom_features
updated_atom_features = updated_atom_features + layers.ReLU()( update1+update2 )
a_feat_after_T_update = updated_atom_features
# (batch_size, Max_atoms, dim_hidden_layer) -> (batch_size * Max_atoms, dim_hidden_layer)
# for removing dummy atoms by using unsorted_segment_mean
updated_atom_features = tf.reshape(updated_atom_features, ( updated_atom_features.shape[0] * updated_atom_features.shape[1], -1 ) )
# (batch_size * Max_atoms, dim_hidden_layer) -> (batch_size, dim_hidden_layer)
mean_atom_features = tf.math.unsorted_segment_mean( updated_atom_features, segment_ids = segment, num_segments = T_part.shape[0])
# (batch_size, embed_size)
update3 = self.dense_layers[5](self.dense_layers[4](mean_atom_features))
update4 = self.dense_layers[7](self.dense_layers[6](T_part))
updated_T = T_part + layers.ReLU()(update3 + update4)
# (batch_size, embed_size + dim_hidden_layer)
concat_vec = tf.concat([updated_T,mean_atom_features], -1)
# readout
h = self.readout_layers[0](concat_vec)
for l in range(1,len(self.readout_layers)):
h = self.readout_layers[l](h)
if equation == '':
# (batch_size, 1) -> (batch_size)
pred = h
if verbose:
return pred, atom_features_each_layer, Attention_each_layer, a_feat_before_T_update, a_feat_after_T_update
else:
return pred
def save_model(self, name):
super(GAT_unc, self).save_weights(name)
def load_model(self, name):
super(GAT_unc, self).load_weights(name)