1- import numpy as np
21import tensorflow as tf
32from tensorflow .keras import layers
43
@@ -39,13 +38,12 @@ def build(self, input_shape):
3938 super ().build (input_shape )
4039
4140 self .gather = nfp .Gather ()
42- self .slice1 = nfp .Slice (np .s_ [:, :, 1 ])
43- self .slice0 = nfp .Slice (np .s_ [:, :, 0 ])
4441 self .concat = nfp .ConcatDense ()
4542
46- def call (self , inputs , mask = None ):
43+ def call (self , inputs , mask = None , ** kwargs ):
4744 """ Inputs: [atom_state, bond_state, connectivity]
4845 Outputs: bond_state
46+
4947 """
5048 if not self .use_global :
5149 atom_state , bond_state , connectivity = inputs
@@ -54,13 +52,15 @@ def call(self, inputs, mask=None):
5452 global_state = self .tile ([global_state , bond_state ])
5553
5654 # Get nodes at start and end of edge
57- source_atom = self .gather ([atom_state , self . slice1 ( connectivity ) ])
58- target_atom = self .gather ([atom_state , self . slice0 ( connectivity ) ])
55+ source_atom = self .gather ([atom_state , connectivity [:, :, 0 ] ])
56+ target_atom = self .gather ([atom_state , connectivity [:, :, 1 ] ])
5957
6058 if not self .use_global :
61- new_bond_state = self .concat ([bond_state , source_atom , target_atom ])
59+ new_bond_state = self .concat (
60+ [bond_state , source_atom , target_atom ])
6261 else :
63- new_bond_state = self .concat ([bond_state , source_atom , target_atom , global_state ])
62+ new_bond_state = self .concat (
63+ [bond_state , source_atom , target_atom , global_state ])
6464
6565 if self .dropout > 0. :
6666 new_bond_state = self .dropout_layer (new_bond_state )
@@ -84,26 +84,25 @@ def build(self, input_shape):
8484 num_features = input_shape [1 ][- 1 ]
8585
8686 self .gather = nfp .Gather ()
87- self .slice0 = nfp .Slice (np .s_ [:, :, 0 ])
88- self .slice1 = nfp .Slice (np .s_ [:, :, 1 ])
8987
9088 self .concat = nfp .ConcatDense ()
9189 self .reduce = nfp .Reduce (reduction = 'sum' )
9290
9391 self .dense1 = layers .Dense (2 * num_features , activation = 'relu' )
9492 self .dense2 = layers .Dense (num_features )
9593
96- def call (self , inputs , mask = None ):
94+ def call (self , inputs , mask = None , ** kwargs ):
9795 """ Inputs: [atom_state, bond_state, connectivity]
9896 Outputs: atom_state
97+
9998 """
10099 if not self .use_global :
101100 atom_state , bond_state , connectivity = inputs
102101 else :
103102 atom_state , bond_state , connectivity , global_state = inputs
104103 global_state = self .tile ([global_state , bond_state ])
105104
106- source_atom = self .gather ([atom_state , self . slice1 ( connectivity ) ])
105+ source_atom = self .gather ([atom_state , connectivity [:, :, 1 ] ])
107106
108107 if not self .use_global :
109108 messages = self .concat ([source_atom , bond_state ])
@@ -112,10 +111,11 @@ def call(self, inputs, mask=None):
112111
113112 if mask is not None :
114113 # Only works for sum, max
115- messages = tf .where (tf .expand_dims (mask [1 ], axis = - 1 ),
116- messages , tf .zeros_like (messages ))
114+ messages = tf .where (tf .expand_dims (mask [1 ], axis = - 1 ), messages ,
115+ tf .zeros_like (messages ))
117116
118- new_atom_state = self .reduce ([messages , self .slice0 (connectivity ), atom_state ])
117+ new_atom_state = self .reduce (
118+ [messages , connectivity [:, :, 0 ], atom_state ])
119119
120120 # Dense net after message reduction
121121 new_atom_state = self .dense1 (new_atom_state )
@@ -151,11 +151,13 @@ def build(self, input_shape):
151151
152152 def transpose_scores (self , input_tensor ):
153153 input_shape = tf .shape (input_tensor )
154- output_shape = [input_shape [0 ], input_shape [1 ], self .num_heads , self .units ]
154+ output_shape = [
155+ input_shape [0 ], input_shape [1 ], self .num_heads , self .units
156+ ]
155157 output_tensor = tf .reshape (input_tensor , output_shape )
156158 return tf .transpose (a = output_tensor , perm = [0 , 2 , 1 , 3 ]) # [B,N,S,H]
157159
158- def call (self , inputs , mask = None ):
160+ def call (self , inputs , mask = None , ** kwargs ):
159161 if not self .use_global :
160162 atom_state , bond_state , connectivity = inputs
161163 else :
@@ -168,17 +170,18 @@ def call(self, inputs, mask=None):
168170
169171 if mask is not None :
170172 graph_element_mask = tf .concat ([mask [0 ], mask [1 ]], axis = 1 )
171- query = tf .where (
172- tf .expand_dims (graph_element_mask , axis = - 1 ),
173- query ,
174- tf .ones_like (query ) * query .dtype .min )
173+ query = tf .where (tf .expand_dims (graph_element_mask , axis = - 1 ),
174+ query ,
175+ tf .ones_like (query ) * query .dtype .min )
175176
176177 query = tf .transpose (query , perm = [0 , 2 , 1 ])
177- value = self .transpose_scores (self .value_layer (graph_elements )) # [B,N,S,H]
178+ value = self .transpose_scores (
179+ self .value_layer (graph_elements )) # [B,N,S,H]
178180
179181 attention_probs = tf .nn .softmax (query )
180182 context = tf .matmul (tf .expand_dims (attention_probs , 2 ), value )
181- context = tf .reshape (context , [batch_size , self .num_heads * self .units ])
183+ context = tf .reshape (context ,
184+ [batch_size , self .num_heads * self .units ])
182185
183186 if self .dropout > 0. :
184187 context = self .dropout_layer (context )
@@ -187,7 +190,5 @@ def call(self, inputs, mask=None):
187190
188191 def get_config (self ):
189192 config = super (GlobalUpdate , self ).get_config ()
190- config .update (
191- {"units" : self .units ,
192- "num_heads" : self .num_heads })
193+ config .update ({"units" : self .units , "num_heads" : self .num_heads })
193194 return config
0 commit comments