-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path_layers.py
342 lines (294 loc) · 14.2 KB
/
_layers.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import keras as ks
from keras import ops
from kgcnn.layers.aggr import AggregateLocalEdges
from keras.layers import Add, Multiply, Dense, Concatenate
from kgcnn.layers.geom import EuclideanNorm, ScalarProduct
from kgcnn.layers.gather import GatherNodesOutgoing
from kgcnn.layers.modules import ExpandDims
class PAiNNconv(ks.layers.Layer):
"""Continuous filter convolution block of `PAiNN <https://arxiv.org/pdf/2102.03150.pdf>`__ .
Args:
units (int): Units for Dense layer.
conv_pool (str): Pooling method. Default is 'sum'.
use_bias (bool): Use bias. Default is True.
activation (str): Activation function. Default is 'kgcnn>shifted_softplus'.
kernel_regularizer: Kernel regularization. Default is None.
bias_regularizer: Bias regularization. Default is None.
activity_regularizer: Activity regularization. Default is None.
kernel_constraint: Kernel constrains. Default is None.
bias_constraint: Bias constrains. Default is None.
kernel_initializer: Initializer for kernels. Default is 'glorot_uniform'.
bias_initializer: Initializer for bias. Default is 'zeros'.
"""
def __init__(self, units,
conv_pool='scatter_sum',
use_bias=True,
activation='swish',
cutoff=None,
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
**kwargs):
"""Initialize Layer."""
super(PAiNNconv, self).__init__(**kwargs)
self.conv_pool = conv_pool
self.units = units
self.use_bias = use_bias
self.cutoff = cutoff
kernel_args = {"kernel_regularizer": kernel_regularizer, "activity_regularizer": activity_regularizer,
"bias_regularizer": bias_regularizer, "kernel_constraint": kernel_constraint,
"bias_constraint": bias_constraint, "kernel_initializer": kernel_initializer,
"bias_initializer": bias_initializer}
# Layer
self.lay_dense1 = Dense(units=self.units, activation=activation, use_bias=self.use_bias, **kernel_args)
self.lay_phi = Dense(units=self.units * 3, activation='linear', use_bias=self.use_bias, **kernel_args)
self.lay_w = Dense(units=self.units * 3, activation='linear', use_bias=self.use_bias, **kernel_args)
self.lay_split = SplitEmbedding(3, axis=-1)
self.lay_sum = AggregateLocalEdges(pooling_method=conv_pool)
self.lay_sum_v = AggregateLocalEdges(pooling_method=conv_pool)
self.gather_n = GatherNodesOutgoing()
self.gather_v = GatherNodesOutgoing()
self.lay_mult = Multiply()
if self.cutoff is not None:
self.lay_mult_cutoff = Multiply()
self.lay_exp_vv = ExpandDims(axis=-2)
self.lay_exp_vw = ExpandDims(axis=-2)
self.lay_exp_r = ExpandDims(axis=-1)
self.lay_mult_vv = Multiply()
self.lay_mult_vw = Multiply()
self.lay_add = Add()
def build(self, input_shape):
"""Build layer."""
super(PAiNNconv, self).build(input_shape)
def call(self, inputs, **kwargs):
"""Forward pass: Calculate edge update.
Args:
inputs: [nodes, equivariant, rbf, envelope, r_ij, edge_index]
- nodes (Tensor): Node embeddings of shape ([N], F)
- equivariant (Tensor): Equivariant node embedding of shape ([N], 3, F)
- rdf (Tensor): Radial basis expansion pair-wise distance of shape ([M], #Basis)
- envelope (Tensor): Distance envelope of shape ([N], 1)
- r_ij (Tensor): Normalized pair-wise distance of shape ([M], 3)
- edge_index (Tensor): Edge indices referring to nodes of shape (2, [M])
Returns:
tuple: [ds, dv]
- ds (Tensor) Updated node features of shape ([N], F)
- dv (Tensor) Updated equivariant features of shape ([N], F, 3)
"""
node, equivariant, rbf, envelope, r_ij, indexlist = inputs
s = self.lay_dense1(node)
s = self.lay_phi(s)
s = self.gather_n([s, indexlist])
w = self.lay_w(rbf)
if self.cutoff is not None:
w = self.lay_mult_cutoff([w, envelope])
sw = self.lay_mult([s, w])
sw1, sw2, sw3 = self.lay_split(sw, **kwargs)
ds = self.lay_sum([node, sw1, indexlist])
vj = self.gather_v([equivariant, indexlist])
sw2 = self.lay_exp_vv(sw2)
dv1 = self.lay_mult_vv([sw2, vj])
sw3 = self.lay_exp_vw(sw3)
r_ij = self.lay_exp_r(r_ij)
dv2 = self.lay_mult_vw([sw3, r_ij])
dv = self.lay_add([dv1, dv2])
dv = self.lay_sum_v([node, dv, indexlist])
return ds, dv
def get_config(self):
"""Update layer config."""
config = super(PAiNNconv, self).get_config()
config.update({"conv_pool": self.conv_pool, "units": self.units, "cutoff": self.cutoff})
config_dense = self.lay_dense1.get_config()
for x in ["kernel_regularizer", "activity_regularizer", "bias_regularizer", "kernel_constraint",
"bias_constraint", "kernel_initializer", "bias_initializer", "activation", "use_bias"]:
if x in config_dense:
config.update({x: config_dense[x]})
return config
class PAiNNUpdate(ks.layers.Layer):
"""Continuous filter convolution of `PAiNN <https://arxiv.org/pdf/2102.03150.pdf>`__ .
Args:
units (int): Units for Dense layer.
conv_pool (str): Pooling method. Default is 'sum'.
use_bias (bool): Use bias. Default is True.
activation (str): Activation function. Default is 'kgcnn>shifted_softplus'.
kernel_regularizer: Kernel regularization. Default is None.
bias_regularizer: Bias regularization. Default is None.
activity_regularizer: Activity regularization. Default is None.
kernel_constraint: Kernel constrains. Default is None.
bias_constraint: Bias constrains. Default is None.
kernel_initializer: Initializer for kernels. Default is 'glorot_uniform'.
bias_initializer: Initializer for bias. Default is 'zeros'.
add_eps: Whether to add eps in the norm.
"""
def __init__(self, units,
use_bias=True,
activation='swish',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
add_eps: bool = False,
**kwargs):
"""Initialize Layer."""
super(PAiNNUpdate, self).__init__(**kwargs)
self.units = units
self.use_bias = use_bias
self.add_eps = add_eps
kernel_args = {"kernel_regularizer": kernel_regularizer, "activity_regularizer": activity_regularizer,
"bias_regularizer": bias_regularizer, "kernel_constraint": kernel_constraint,
"bias_constraint": bias_constraint, "kernel_initializer": kernel_initializer,
"bias_initializer": bias_initializer}
# Layer
self.lay_dense1 = Dense(units=self.units, activation=activation, use_bias=self.use_bias, **kernel_args)
self.lay_lin_u = Dense(self.units, activation='linear', use_bias=False, **kernel_args)
self.lay_lin_v = Dense(self.units, activation='linear', use_bias=False, **kernel_args)
self.lay_a = Dense(units=self.units * 3, activation='linear', use_bias=self.use_bias, **kernel_args)
self.lay_scalar_prod = ScalarProduct(axis=1)
self.lay_norm = EuclideanNorm(axis=1, add_eps=self.add_eps)
self.lay_concat = Concatenate(axis=-1)
self.lay_split = SplitEmbedding(3, axis=-1)
self.lay_mult = Multiply()
self.lay_exp_v = ExpandDims(axis=-2)
self.lay_mult_vv = Multiply()
self.lay_add = Add()
def build(self, input_shape):
"""Build layer."""
super(PAiNNUpdate, self).build(input_shape)
def call(self, inputs, **kwargs):
"""Forward pass: Calculate edge update.
Args:
inputs: [nodes, equivariant]
- nodes (Tensor): Node embeddings of shape ([N], F)
- equivariant (Tensor): Equivariant node embedding of shape ([N], 3, F)
Returns:
tuple: [ds, dv]
- ds (Tensor) Updated node features of shape ([N], F)
- dv (Tensor) Updated equivariant features of shape ([N], 3, F)
"""
node, equivariant = inputs
v_v = self.lay_lin_v(equivariant, **kwargs)
v_u = self.lay_lin_u(equivariant, **kwargs)
v_prod = self.lay_scalar_prod([v_u, v_v], **kwargs)
v_norm = self.lay_norm(v_v, **kwargs)
a = self.lay_concat([node, v_norm], **kwargs)
a = self.lay_dense1(a, **kwargs)
a = self.lay_a(a, **kwargs)
a_vv, a_sv, a_ss = self.lay_split(a, **kwargs)
a_vv = self.lay_exp_v(a_vv, **kwargs)
dv = self.lay_mult_vv([a_vv, v_u], **kwargs)
ds = self.lay_mult([v_prod, a_sv], **kwargs)
ds = self.lay_add([ds, a_ss], **kwargs)
return ds, dv
def get_config(self):
"""Update layer config."""
config = super(PAiNNUpdate, self).get_config()
config.update({"units": self.units, "add_eps": self.add_eps})
config_dense = self.lay_dense1.get_config()
for x in ["kernel_regularizer", "activity_regularizer", "bias_regularizer", "kernel_constraint",
"bias_constraint", "kernel_initializer", "bias_initializer", "activation", "use_bias"]:
if x in config_dense.keys():
config.update({x: config_dense[x]})
return config
class EquivariantInitialize(ks.layers.Layer):
"""Equivariant initializer of `PAiNN <https://arxiv.org/pdf/2102.03150.pdf>`__ .
Args:
dim (int): Dimension of equivariant features. Default is 3.
method (str): How to initialize equivariant tensor. Default is "zeros".
"""
def __init__(self, dim=3, units=128, method: str = "zeros", value: float = 1.0, stddev: float = 1.0,
seed: int = 42, **kwargs):
"""Initialize Layer."""
super(EquivariantInitialize, self).__init__(**kwargs)
self.dim = int(dim)
self.units = units
self.method = str(method)
self.value = float(value)
self.stddev = float(stddev)
self.seed = seed
def build(self, input_shape):
"""Build layer."""
super(EquivariantInitialize, self).build(input_shape)
def call(self, inputs, **kwargs):
"""Forward pass: Calculate edge update.
Args:
inputs: nodes
- nodes (Tensor): Node embeddings of shape ([N], F) or ([N], ).
Returns:
Tensor: Equivariant tensor of shape ([N], dim, F) or ([N], dim, units).
"""
if len(ops.shape(inputs)) < 2:
inputs = ops.expand_dims(inputs, axis=-1)
inputs = ops.repeat(inputs, self.units, axis=-1)
if self.method == "zeros":
out = ops.zeros_like(inputs, dtype=self.dtype)
out = ops.expand_dims(out, axis=1)
out = ops.repeat(out, self.dim, axis=1)
elif self.method == "eps":
out = ops.zeros_like(inputs, dtype=self.dtype) + ks.backend.epsilon()
out = ops.expand_dims(out, axis=1)
out = ops.repeat(out, self.dim, axis=1)
elif self.method == "normal":
out = ks.random.normal((self.dim, ops.shape(inputs)[-1]), seed=self.seed)
out = ops.expand_dims(out, axis=0)
out = ops.repeat(out, ops.shape(inputs)[0], axis=0)
elif self.method == "ones":
out = ops.ones_like(inputs, dtype=self.dtype)
out = ops.expand_dims(out, axis=1)
out = ops.repeat(out, self.dim, axis=1)
elif self.method == "eye":
out = ops.eye(self.dim, ops.shape(inputs)[1], dtype=self.dtype)
out = ops.expand_dims(out, axis=0)
out = ops.repeat(out, ops.shape(inputs)[0], axis=0)
elif self.method == "const":
out = ops.ones_like(inputs, dtype=self.dtype)*self.value
out = ops.expand_dims(out, axis=1)
out = ops.repeat(out, self.dim, axis=1)
elif self.method == "node":
out = ops.expand_dims(inputs, axis=1)
out = ops.repeat(out, self.dim, axis=1)
out = ops.cast(out, dtype=self.dtype)
else:
raise ValueError("Unknown initialization method %s" % self.method)
return out
def get_config(self):
"""Update layer config."""
config = super(EquivariantInitialize, self).get_config()
config.update({"dim": self.dim, "method": self.method, "value": self.value, "stddev": self.stddev,
"units": self.units, "seed": self.seed})
return config
class SplitEmbedding(ks.layers.Layer):
"""Split embeddings of `PAiNN <https://arxiv.org/pdf/2102.03150.pdf>`__ .
Args:
num_or_size_splits: Number or size of splits.
axis (int): Axis to split.
num (int): Number of output splits.
"""
def __init__(self,
indices_or_sections,
axis=-1,
**kwargs):
super(SplitEmbedding, self).__init__(**kwargs)
self.indices_or_sections = indices_or_sections
self.axis = axis
def build(self, input_shape):
super(SplitEmbedding, self).build(input_shape)
def call(self, inputs, **kwargs):
r"""Forward pass: Split embeddings across feature dimension e.g. `axis=-1` .
Args:
inputs (Tensor): Embeddings of shape ([N], F)
Returns:
list: List of tensor splits of shape ([N], F/num)
"""
outs = ops.split(inputs, self.indices_or_sections, axis=self.axis)
return outs
def get_config(self):
config = super(SplitEmbedding, self).get_config()
config.update({"indices_or_sections": self.indices_or_sections, "axis": self.axis})
return config