-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
349 lines (282 loc) · 15 KB
/
models.py
File metadata and controls
349 lines (282 loc) · 15 KB
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
343
344
345
346
347
348
349
import torch
from torch import nn
from torch_geometric.nn.aggr import SumAggregation
from torch_geometric.data import Batch
from torch_geometric.utils import remove_self_loops
from amcg_utils.gen_utils import get_true_mask
from losses import kl_loss
from pieces import GlobalEncoder, Decoder, AtomGenerator, Combiner, AtomEncoder, MolEncoder, ADJ_Dec, Bond_Pred, HeteroMLP, Bond_Pred_Zinc
# MODEL
class AMCG(nn.Module):
"""
AMCG model.
Args:
encoder (GlobalEncoder): The global encoder module.
decoder (Decoder): The shared decoder module.
generator (AtomGenerator): The molecular decoder - atomic generator module.
combiner (Combiner): The combiner module.
num_atom_types (int): The number of atom types.
num_bond_types (int): The number of bond types.
max_logstd (float, optional): The maximum value for the log standard deviation. Defaults to None.
"""
def __init__(self, encoder: GlobalEncoder, decoder: Decoder,
generator: AtomGenerator, combiner: Combiner, num_atom_types,
num_bond_types, max_logstd=None) -> None:
super().__init__()
self.encoder = encoder
self.decoder = decoder # shared decoder
self.generator = generator # molecular decoder - atomic generator
self.combiner = combiner
self.sumaggregator = SumAggregation()
if max_logstd is None:
self.max_logstd = 10
else:
self.max_logstd = max_logstd
self.cel_loss_fn = torch.nn.CrossEntropyLoss()
self.mse_loss_fn = torch.nn.MSELoss()
self.num_atom_types = num_atom_types
self.num_bond_types = num_bond_types
self.atom_channels = self.encoder.atom_encoder.out_channels
self.mol_channels = self.encoder.mol_encoder.out_channels
def encode_batch(self, data_batch, return_all=False):
"""
Encodes a batch of data.
Args:
data_batch (Batch): The input data batch.
return_all (bool, optional): Whether to return mus and sigmas. Defaults to False.
Returns:
tuple: Atomic encodings and molecular encodings.
"""
x = torch.cat((data_batch.x, data_batch.random_walk_pe), dim=-1)
edge_index = data_batch.edge_index
edge_attr = data_batch.edge_attr
batch = data_batch.batch
atom_types = torch.argmax(data_batch.x[:, :self.num_atom_types], dim=-1)
atom_mu, atom_logstd, mol_mu, mol_logstd, atom_z, mol_z = self.encoder(x=x, edge_index=edge_index,
edge_attr=edge_attr, batch=batch,
atom_types=atom_types)
if return_all:
return atom_mu, atom_logstd, mol_mu, mol_logstd, atom_z, mol_z
elif not return_all:
return atom_z, mol_z
def loss(self, x, pos_edge_index, neg_edge_index, edge_attr, batch,
y, atom_types=None, num_atoms=None):
"""
Computes the loss for the model.
Args:
x: The input features.
pos_edge_index: The positive edge indices.
neg_edge_index: The negative edge indices.
edge_attr: The edge attributes.
batch: The batch indices.
y: The target properties.
atom_types: The atom types. Defaults to None.
num_atoms: The number of atoms. Defaults to None.
Returns:
tuple: The computed losses.
"""
if atom_types is None:
atom_types = torch.argmax(x[:,:self.num_atom_types], dim=-1)
if num_atoms is None:
num_atoms = torch.unique(batch, return_counts=True)[1]
target_hist = self.sumaggregator(x[:, :self.num_atom_types], batch)
target_hydrogens = torch.argmax(x[:, self.num_atom_types+14:self.num_atom_types+19], dim=-1).to(torch.float)
_, _, mol_mu, mol_logstd, atom_z, mol_z = self.encoder(x=x, edge_index=pos_edge_index,
edge_attr=edge_attr, batch=batch,
atom_types=atom_types)
mol_kl_loss = kl_loss(mol_mu, mol_logstd)
mol_logstd = mol_logstd.clamp(max=self.max_logstd)
out = self.combiner(atom_z, mol_z, atom_types, batch)
true_edge_mask = get_true_mask(num_atoms, out.device)
target_bond_types = torch.argmax(edge_attr[:,:self.num_bond_types], dim=-1)
pos_loss, neg_loss, bond_loss, hs_loss = self.decoder.loss(x=out,
atom_types=atom_types,
pos_edge_index=pos_edge_index,
neg_edge_index=neg_edge_index,
true_edge_mask=true_edge_mask,
target_bond_types=target_bond_types,
target_hydrogens=target_hydrogens)
hist_loss, recon_m_loss, prop_loss, recon_atoms = self.generator.loss(mol_z=mol_z,
target_hist=target_hist,
target_atoms=out,
target_props=y,
return_atoms=True)
recon_p_loss, recon_n_loss, recon_b_loss, recon_hs_loss = self.decoder.loss(x=recon_atoms,
atom_types=atom_types,
pos_edge_index=pos_edge_index,
neg_edge_index=neg_edge_index,
true_edge_mask=true_edge_mask,
target_bond_types=target_bond_types,
target_hydrogens=target_hydrogens)
return mol_kl_loss, pos_loss, neg_loss, bond_loss, hist_loss, recon_p_loss, recon_n_loss, recon_b_loss, recon_m_loss, prop_loss, hs_loss, recon_hs_loss
def infer_from_z(self, mol_z, return_props=False, perturb_hist=False, perturb_mode=None):
"""
Inference step from the given latent embeddings via right branch.
Args:
mol_z: The molecular latent embeddings of shape (batch_size, mol_latent_dims).
return_props (bool, optional): Whether to return the predicted properties. Defaults to False.
perturb_hist (bool, optional): Whether to perturb the histogram. Defaults to False.
perturb_mode (str, optional): The perturbation mode. Defaults to None.
Returns:
tuple: The inferred adjacency matrix, atom types and bond types, batch indices, predicted hydrogens
and molecular properties (if required).
"""
_, out, props, atom_types, batch = self.generator(mol_z, perturb_hist=perturb_hist, perturb_mode=perturb_mode)
num_atoms = torch.unique(batch, return_counts=True)[1]
true_edge_mask = get_true_mask(num_atoms, mol_z.device)
_, bond_pred, hs_pred, new_edge_index = self.decoder(x=out,
atom_types=atom_types,
true_edge_mask=true_edge_mask)
if return_props:
return new_edge_index, atom_types, bond_pred, batch, hs_pred, props
return new_edge_index, atom_types, bond_pred, batch, hs_pred
def infer_right(self, data_list, return_latent=False, return_props=False):
"""
Inference step from data input to output, via right branch.
Args:
data_list: The list of Data objects.
return_latent (bool, optional): Whether to return the latent embeddings. Defaults to False.
return_props (bool, optional): Whether to return the predicted properties. Defaults to False.
Returns:
tuple: The inferred molecular tensors.
"""
data = Batch.from_data_list(data_list)
_, mol_z = self.encode_batch(data_batch=data, return_all=False)
infer_out = self.infer_from_z(mol_z, return_props=return_props)
if return_latent:
return *infer_out, mol_z
return infer_out
def forward(self, data_list, device, prop_indices=None):
"""
It computes the losses for the model. It is in the forward method to be used
with the DataParallel module.
Args:
data_list: The list of data objects.
device: The device to run the model on.
prop_indices: The indices of the properties to predict. Defaults to None.
Returns:
tuple: The computed losses.
"""
if prop_indices is None:
prop_indices = [0]
data = Batch.from_data_list(data_list).to(device)
x = torch.cat((data.x, data.random_walk_pe), dim=-1)
pos_edge_index=data.edge_index
neg_edge_index=remove_self_loops(data.neg_edge_index)[0]
edge_attr=data.edge_attr
batch=data.batch
if isinstance(prop_indices, list) and len(prop_indices) > 0:
y = [data.y[:, idx] for idx in prop_indices]
else:
y = []
(mol_kl_loss, pos_loss, neg_loss, bond_loss,
hist_loss, recon_p_loss, recon_n_loss,
recon_b_loss, recon_m_loss, prop_loss,
hs_loss, recon_hs_loss) = self.loss(x=x,
pos_edge_index=pos_edge_index,
neg_edge_index=neg_edge_index,
edge_attr=edge_attr,
batch=batch,
y=y)
return (mol_kl_loss, pos_loss, neg_loss, bond_loss,
hist_loss, recon_p_loss, recon_n_loss,
recon_b_loss, recon_m_loss, prop_loss,
hs_loss, recon_hs_loss)
def get_amcg_qm9(num_properties=1):
"""
Returns an instance of the AMCG model for QM9 dataset.
Args:
num_properties (int, optional): The number of properties to predict. Defaults to 1.
Returns:
AMCG: The AMCG model instance.
"""
num_atom_types = 4
num_bond_types = 4
num_in_channels = 54+num_atom_types
# build encoder
atom_encoder = AtomEncoder(in_channels=num_in_channels, embedding_dim=512,
hidden_channels=1024, out_channels=512,
num_atom_types=num_atom_types)
atom_n_channels = atom_encoder.out_channels
mol_n_channels = 1024+512
mol_encoder = MolEncoder(in_channels=atom_n_channels,
hidden_channels=1024,
out_channels=mol_n_channels)
encoder = GlobalEncoder(atom_encoder=atom_encoder,
mol_encoder=mol_encoder)
#build combiner
combiner = Combiner(in_channels=atom_n_channels+mol_n_channels,
hidden_channels=1024, out_channels=1024,
num_atom_types=num_atom_types)
#build generator
generator = AtomGenerator(in_channels=mol_n_channels,
num_atom_types=num_atom_types,
generator_latent_dims=combiner.out_channels*2, # can be changed
atom_latent_dims=combiner.out_channels,
num_properties=num_properties)
#build decoder
adj_dec = ADJ_Dec()
bond_predictor = Bond_Pred(in_channels=combiner.out_channels, embedding_dim=2048,
za_hc=2048, c_hc=2048, bond_types=num_bond_types)
hs_predictor = HeteroMLP(in_channels=combiner.out_channels,
hidden_channels=[1024,512,1], num_classes=num_atom_types)
decoder = Decoder(adj_decoder=adj_dec,
bond_classifier=bond_predictor,
hs_predictor=hs_predictor)
# BUILD NET
model = AMCG(encoder=encoder,
decoder=decoder,
generator=generator,
combiner=combiner,
num_atom_types=num_atom_types,
num_bond_types=num_bond_types)
return model
def get_amcg_zinc(num_properties=1):
"""
Returns an instance of the AMCG model for the ZINC dataset.
Parameters:
- num_properties (int): The number of properties to predict.
Returns:
- model (AMCG): An instance of the AMCG model.
"""
num_atom_types = 9
num_bond_types = 4
num_in_channels = 54+num_atom_types
# build encoder
atom_encoder = AtomEncoder(in_channels=num_in_channels, embedding_dim=512,
hidden_channels=1024, out_channels=512,
num_atom_types=num_atom_types)
atom_n_channels = atom_encoder.out_channels
mol_n_channels = 1024+512
mol_encoder = MolEncoder(in_channels=atom_n_channels,
hidden_channels=1024,
out_channels=mol_n_channels)
encoder = GlobalEncoder(atom_encoder=atom_encoder,
mol_encoder=mol_encoder)
# build combiner
combiner = Combiner(in_channels=atom_n_channels+mol_n_channels,
hidden_channels=1024, out_channels=1024,
num_atom_types=num_atom_types)
# build generator
generator = AtomGenerator(in_channels=mol_n_channels,
num_atom_types=num_atom_types,
generator_latent_dims=combiner.out_channels*2,
atom_latent_dims=combiner.out_channels,
num_properties=num_properties)
# build decoder
adj_dec = ADJ_Dec()
bond_predictor = Bond_Pred_Zinc(in_channels=combiner.out_channels, embedding_dim=2048,
za_hc=2048, c_hc=2048, bond_types=num_bond_types)
hs_predictor = HeteroMLP(in_channels=combiner.out_channels,
hidden_channels=[1024,512,1], num_classes=num_atom_types)
decoder = Decoder(adj_decoder=adj_dec,
bond_classifier=bond_predictor,
hs_predictor=hs_predictor)
# BUILD NET
model = AMCG(encoder=encoder,
decoder=decoder,
generator=generator,
combiner=combiner,
num_atom_types=num_atom_types,
num_bond_types=num_bond_types)
return model