-
Notifications
You must be signed in to change notification settings - Fork 30
/
_model.py
193 lines (162 loc) · 6.66 KB
/
_model.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
from kgcnn.layers.modules import Embedding
from kgcnn.layers.geom import NodePosition, NodeDistanceEuclidean, GaussBasisLayer, ShiftPeriodicLattice
from kgcnn.layers.mlp import MLP, GraphMLP
from keras.layers import Dense, Dropout, Concatenate, Flatten, Add
from kgcnn.layers.pooling import PoolingNodes
from kgcnn.layers.set2set import PoolingSet2SetEncoder
from ._layers import MEGnetBlock
PoolingGlobalEdges = PoolingNodes
def model_disjoint(
inputs,
use_node_embedding,
use_graph_embedding,
input_node_embedding: dict = None,
input_graph_embedding: dict = None,
expand_distance: bool = None,
make_distance: bool = None,
gauss_args: dict = None,
meg_block_args: dict = None,
set2set_args: dict = None,
node_ff_args: dict = None,
edge_ff_args: dict = None,
state_ff_args: dict = None,
use_set2set: bool = None,
nblocks: int = None,
has_ff: bool = None,
dropout: float = None,
output_embedding: str = None,
output_mlp: dict = None,
):
# Make input
vp, x, edi, up, batch_id_node, batch_id_edge, count_nodes, count_edges = inputs
# Embedding, if no feature dimension
if use_node_embedding:
vp = Embedding(**input_node_embedding)(vp)
if use_graph_embedding:
up = Embedding(**input_graph_embedding)(up)
# Edge distance as Gauss-Basis
if make_distance:
pos1, pos2 = NodePosition()([x, edi])
ep = NodeDistanceEuclidean()([pos1, pos2])
else:
ep = x
if expand_distance:
ep = GaussBasisLayer(**gauss_args)(ep)
# Model
vp = GraphMLP(**node_ff_args)([vp, batch_id_node, count_nodes])
ep = GraphMLP(**edge_ff_args)([ep, batch_id_edge, count_edges])
up = MLP(**state_ff_args)(up)
vp2 = vp
ep2 = ep
up2 = up
for i in range(0, nblocks):
if has_ff and i > 0:
vp2 = GraphMLP(**node_ff_args)([vp, batch_id_node, count_nodes])
ep2 = GraphMLP(**edge_ff_args)([ep, batch_id_edge, count_edges])
up2 = MLP(**state_ff_args)(up)
# MEGnetBlock
vp2, ep2, up2 = MEGnetBlock(**meg_block_args)(
[vp2, ep2, edi, up2, batch_id_node, batch_id_edge, count_nodes, count_edges])
# skip connection
if dropout is not None:
vp2 = Dropout(dropout, name='dropout_atom_%d' % i)(vp2)
ep2 = Dropout(dropout, name='dropout_bond_%d' % i)(ep2)
up2 = Dropout(dropout, name='dropout_state_%d' % i)(up2)
vp = Add()([vp2, vp])
ep = Add()([ep2, ep])
up = Add()([up2, up])
if use_set2set:
vp = Dense(set2set_args["channels"], activation='linear')(vp) # to match units
ep = Dense(set2set_args["channels"], activation='linear')(ep) # to match units
vp = PoolingSet2SetEncoder(**set2set_args)([count_nodes, vp, batch_id_node])
ep = PoolingSet2SetEncoder(**set2set_args)([count_edges, ep, batch_id_edge])
else:
vp = PoolingNodes()([count_nodes, vp, batch_id_node])
ep = PoolingGlobalEdges()([count_edges, ep, batch_id_edge])
ep = Flatten()(ep)
vp = Flatten()(vp)
final_vec = Concatenate(axis=-1)([vp, ep, up])
if dropout is not None:
final_vec = Dropout(dropout, name='dropout_final')(final_vec)
# Only graph embedding for Megnet
if output_embedding != "graph":
raise ValueError("Unsupported output embedding for mode `Megnet`.")
main_output = MLP(**output_mlp)(final_vec)
return main_output
def model_disjoint_crystal(
inputs,
use_node_embedding,
use_graph_embedding,
input_node_embedding: dict = None,
input_graph_embedding: dict = None,
expand_distance: bool = None,
make_distance: bool = None,
gauss_args: dict = None,
meg_block_args: dict = None,
set2set_args: dict = None,
node_ff_args: dict = None,
edge_ff_args: dict = None,
state_ff_args: dict = None,
use_set2set: bool = None,
nblocks: int = None,
has_ff: bool = None,
dropout: float = None,
output_embedding: str = None,
output_mlp: dict = None,
):
vp, x, edi, up, edge_image, lattice, batch_id_node, batch_id_edge, count_nodes, count_edges = inputs
# Embedding, if no feature dimension
if use_node_embedding:
vp = Embedding(**input_node_embedding)(vp)
if use_graph_embedding:
up = Embedding(**input_graph_embedding)(up)
# Edge distance as Gauss-Basis
if make_distance:
pos1, pos2 = NodePosition()([x, edi])
pos2 = ShiftPeriodicLattice()([pos2, edge_image, lattice, batch_id_edge])
ep = NodeDistanceEuclidean()([pos1, pos2])
else:
ep = x
if expand_distance:
ep = GaussBasisLayer(**gauss_args)(ep)
# Model
vp = GraphMLP(**node_ff_args)([vp, batch_id_edge, count_edges])
ep = GraphMLP(**edge_ff_args)([ep, batch_id_edge, count_edges])
up = MLP(**state_ff_args)(up)
vp2 = vp
ep2 = ep
up2 = up
for i in range(0, nblocks):
if has_ff and i > 0:
vp2 = GraphMLP(**node_ff_args)([vp, batch_id_node, count_nodes])
ep2 = GraphMLP(**edge_ff_args)([ep, batch_id_edge, count_edges])
up2 = MLP(**state_ff_args)(up)
# MEGnetBlock
vp2, ep2, up2 = MEGnetBlock(**meg_block_args)(
[vp2, ep2, edi, up2, batch_id_node, batch_id_edge, count_nodes, count_edges])
# skip connection
if dropout is not None:
vp2 = Dropout(dropout, name='dropout_atom_%d' % i)(vp2)
ep2 = Dropout(dropout, name='dropout_bond_%d' % i)(ep2)
up2 = Dropout(dropout, name='dropout_state_%d' % i)(up2)
vp = Add()([vp2, vp])
ep = Add()([ep2, ep])
up = Add()([up2, up])
if use_set2set:
vp = Dense(set2set_args["channels"], activation='linear')(vp) # to match units
ep = Dense(set2set_args["channels"], activation='linear')(ep) # to match units
vp = PoolingSet2SetEncoder(**set2set_args)([count_nodes, vp, batch_id_node])
ep = PoolingSet2SetEncoder(**set2set_args)([count_edges, ep, batch_id_edge])
else:
vp = PoolingNodes()([count_nodes, vp, batch_id_node])
ep = PoolingGlobalEdges()([count_edges, ep, batch_id_edge])
ep = Flatten()(ep)
vp = Flatten()(vp)
final_vec = Concatenate(axis=-1)([vp, ep, up])
if dropout is not None:
final_vec = Dropout(dropout, name='dropout_final')(final_vec)
# Only graph embedding for Megnet
if output_embedding != "graph":
raise ValueError("Unsupported output embedding for mode `Megnet`.")
main_output = MLP(**output_mlp)(final_vec)
return main_output