-
Notifications
You must be signed in to change notification settings - Fork 31
/
_model.py
160 lines (141 loc) · 6.12 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
from kgcnn.layers.mlp import MLP, GraphMLP, RelationalMLP
from keras.layers import Concatenate, Dense, Multiply, Add
from kgcnn.layers.pooling import PoolingNodes
from kgcnn.layers.norm import GraphBatchNormalization
from ._wacsf import wACSFRad, wACSFAng
from ._layers import CorrectPartialCharges
from ._acsf import ACSFG2, ACSFG4, ACSFConstNormalization
def model_disjoint_weighted(
inputs,
node_pooling_args: dict = None,
w_acsf_ang_kwargs: dict = None,
w_acsf_rad_kwargs: dict = None,
normalize_kwargs: dict = None,
const_normalize_kwargs: dict = None,
mlp_kwargs: dict = None,
output_embedding: str = None,
use_output_mlp: bool = None,
output_mlp: dict = None,
predict_dipole: bool = None
):
# Make input
node_input, xyz_input, edge_index_input, angle_index_input, tot_charge, batch_id_node, count_nodes = inputs
# ACSF representation.
rep_rad = wACSFRad(**w_acsf_rad_kwargs)([node_input, xyz_input, edge_index_input])
rep_ang = wACSFAng(**w_acsf_ang_kwargs)([node_input, xyz_input, angle_index_input])
rep = Concatenate()([rep_rad, rep_ang])
# Normalization
if normalize_kwargs:
rep = GraphBatchNormalization(**normalize_kwargs)([rep, batch_id_node, count_nodes])
if const_normalize_kwargs:
rep = ACSFConstNormalization(**const_normalize_kwargs)(rep)
# learnable NN.
n = RelationalMLP(**mlp_kwargs)([rep, node_input, batch_id_node, count_nodes])
# Output embedding choice
if output_embedding == 'graph':
out = PoolingNodes(**node_pooling_args)([count_nodes, n, batch_id_node])
if use_output_mlp:
out = MLP(**output_mlp)(out)
if predict_dipole:
pc = Dense(units=1)(n)
tc = PoolingNodes(pooling_method="sum")([count_nodes, pc, batch_id_node])
if tot_charge is not None:
pc_correct = CorrectPartialCharges()([tc, tot_charge, count_nodes, batch_id_node])
pc = Add()([pc, pc_correct])
p_dip = Multiply()([pc, xyz_input])
dip = PoolingNodes(pooling_method="sum")([count_nodes, p_dip, batch_id_node])
out = [out, dip]
if tot_charge is None:
out = out + [tc]
elif output_embedding == 'node':
out = n
if use_output_mlp:
out = GraphMLP(**output_mlp)([out, batch_id_node, count_nodes])
else:
raise ValueError("Unsupported output embedding for mode `HDNNP2nd` .")
return out
def model_disjoint_behler(
inputs,
node_pooling_args: dict = None,
normalize_kwargs: dict = None,
const_normalize_kwargs: dict = None,
g2_kwargs: dict = None,
g4_kwargs: dict = None,
mlp_kwargs: dict = None,
output_embedding: str = None,
use_output_mlp: bool = None,
output_mlp: dict = None,
predict_dipole: bool = None
):
# Make input
node_input, xyz_input, edge_index_input, angle_index_input, tot_charge, batch_id_node, count_nodes = inputs
# ACSF representation.
rep_g2 = ACSFG2(**ACSFG2.make_param_table(**g2_kwargs))([node_input, xyz_input, edge_index_input])
rep_g4 = ACSFG4(**ACSFG4.make_param_table(**g4_kwargs))([node_input, xyz_input, angle_index_input])
rep = Concatenate()([rep_g2, rep_g4])
# Normalization
if normalize_kwargs:
rep = GraphBatchNormalization(**normalize_kwargs)([rep, batch_id_node, count_nodes])
if const_normalize_kwargs:
rep = ACSFConstNormalization(**const_normalize_kwargs)(rep)
# learnable NN.
n = RelationalMLP(**mlp_kwargs)([rep, node_input, batch_id_node, count_nodes])
# Output embedding choice
if output_embedding == 'graph':
out = PoolingNodes(**node_pooling_args)([count_nodes, n, batch_id_node])
if use_output_mlp:
out = MLP(**output_mlp)(out)
if predict_dipole:
pc = Dense(units=1)(n)
tc = PoolingNodes(pooling_method="sum")([count_nodes, pc, batch_id_node])
if tot_charge is not None:
pc_correct = CorrectPartialCharges()([tc, tot_charge, count_nodes, batch_id_node])
pc = Add()([pc, pc_correct])
p_dip = Multiply()([pc, xyz_input])
dip = PoolingNodes(pooling_method="sum")([count_nodes, p_dip, batch_id_node])
out = [out, dip]
if tot_charge is None:
out = out + [tc]
elif output_embedding == 'node':
out = n
if use_output_mlp:
out = GraphMLP(**output_mlp)([out, batch_id_node, count_nodes])
else:
raise ValueError("Unsupported output embedding for mode `HDNNP2nd`")
return out
def model_disjoint_atom_wise(
inputs,
node_pooling_args: dict = None,
mlp_kwargs: dict = None,
output_embedding: str = None,
use_output_mlp: bool = None,
output_mlp: dict = None,
predict_dipole: bool = None
):
# Make input
node_input, rep_input, tot_charge, batch_id_node, count_nodes = inputs
# learnable NN.
n = RelationalMLP(**mlp_kwargs)([rep_input, node_input, batch_id_node, count_nodes])
# Output embedding choice
if output_embedding == 'graph':
out = PoolingNodes(**node_pooling_args)([count_nodes, n, batch_id_node])
if use_output_mlp:
out = MLP(**output_mlp)(out)
if predict_dipole:
pc = Dense(units=1)(n)
tc = PoolingNodes(pooling_method="sum")([count_nodes, pc, batch_id_node])
if tot_charge is not None:
pc_correct = CorrectPartialCharges()([tc, tot_charge, count_nodes, batch_id_node])
pc = Add()([pc, pc_correct])
p_dip = Multiply()([pc, xyz_input])
dip = PoolingNodes(pooling_method="sum")([count_nodes, p_dip, batch_id_node])
out = [out, dip]
if tot_charge is None:
out = out + [tc]
elif output_embedding == 'node':
out = n
if use_output_mlp:
out = GraphMLP(**output_mlp)([out, batch_id_node, count_nodes])
else:
raise ValueError("Unsupported output embedding for mode `HDNNP2nd`")
return out