-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseparate_models.py
360 lines (270 loc) · 10.5 KB
/
separate_models.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
""" Here you will find the models used to predict latent space representation from molecular properties """
import torch
from torch import nn
import torch.nn.functional as F
from VAE import PositionalEncoding, embedder
#these are just usual numbers but in the actual script you will redeclare these variables
input_dim=528
latent_size=30
prop_size=30
extra_size=1
total_size=32
GELU=nn.ReLU()
#convolutional model
class prop_ls_NN_conv(nn.Module):
def __init__(self,latent_size=latent_size,prop_size=prop_size,extra_size=extra_size):
super().__init__()
#define a few variables
self.latent_size=latent_size
self.prop_size=prop_size
self.extra_size=extra_size
#this generates a set of extra_size properties that will be concatenated with the actual properties
self.enhancer=nn.Sequential(
nn.Linear(prop_size, 256),
nn.Tanh(),
nn.Linear(256,256),
nn.Tanh(),
nn.Linear(256,extra_size)
)
#convolutional module, takes in the properties 'image' which is just an outer product and outputs features
self.encConv0 = nn.Sequential(
#nn.BatchNorm2d(imgChannels),
nn.Conv2d(1, 16, 11),
nn.ReLU(),
nn.Conv2d(16, 32, 11),
nn.ReLU()
)
#feedforward module that takes in the convoluted features and outputs mean and logvar of the output
self.model=nn.Sequential(
nn.Linear(32*12*12,2048),
nn.Tanh(),
nn.Linear(2048,1024),
nn.Tanh(),
nn.Linear(1024,2*latent_size)
)
def forward(self,x):
#compute enhanced set of properties
z=self.enhancer(x)
#concatenate with original properties
y=torch.cat((x,z),1)
#outer product into an image
y=torch.einsum('bp,bq->bpq', y, y)
y=y.view(-1,1,32,32)
#convolutional step
y=self.encConv0(y)
#reshape and feed to the feedforward module
y=y.view(-1,32*12*12)
mu_logvar=self.model(y).view(-1,2,self.latent_size)
#output probabilistic prediction
mu_p=mu_logvar[:,0,:]
logvar_p=mu_logvar[:,1,:]
return mu_p,logvar_p
#feedforward model
class prop_ls_NN(nn.Module):
def __init__(self,latent_size=latent_size,prop_size=prop_size,extra_size=extra_size):
super().__init__()
#define a few variables
self.latent_size=latent_size
self.prop_size=prop_size
self.extra_size=extra_size
#this generates a set of extra_size properties that will be concatenated with the actual properties
self.enhancer=nn.Sequential(
nn.Linear(prop_size, 128),
nn.Tanh(),
nn.BatchNorm1d(128),
nn.Linear(128,128),
nn.Tanh(),
nn.BatchNorm1d(128),
nn.Linear(128,extra_size),
nn.BatchNorm1d(extra_size)
)
#feedforward module that takes in the properties and outputs mean and logvar of the output
self.model=nn.Sequential(
nn.Linear(prop_size+extra_size,2048),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(2048),
nn.Linear(2048,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,2*latent_size)
)
def forward(self,x):
#compute enhanced set of properties
z=self.enhancer(x)
#concatenate with original properties
y=torch.cat((x,z),1)
#compute output
mu_logvar=self.model(y).view(-1,2,self.latent_size)
mu_p=mu_logvar[:,0,:]
logvar_p=mu_logvar[:,1,:]
return mu_p,logvar_p
class mol_ls_NN(nn.Module):
def __init__(self, latent_size=latent_size, input_dim=input_dim):
super().__init__()
#define a few variables
self.latent_size=latent_size
self.prop_size=prop_size
#feedforward module that takes in the properties and outputs mean and logvar of the output
self.model=nn.Sequential(
nn.Linear(input_dim,2048),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(2048),
nn.Linear(2048,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,2*latent_size)
)
def forward(self,x):
#compute output
mu_logvar=self.model(x).view(-1,2,self.latent_size)
mu_p=mu_logvar[:,0,:]
logvar_p=mu_logvar[:,1,:]
return mu_p,logvar_p
class ls_mol_NN(nn.Module):
def __init__(self, latent_size=latent_size, input_dim=input_dim):
super().__init__()
#define a few variables
self.latent_size=latent_size
self.prop_size=prop_size
self.input_dim=input_dim
#feedforward module that takes in the properties and outputs mean and logvar of the output
self.model=nn.Sequential(
nn.Tanh(),
nn.Linear(latent_size,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,1024),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(1024),
nn.Linear(1024,2048),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.BatchNorm1d(2048),
nn.Linear(2048,input_dim*2)
)
def forward(self,x):
#compute output
mu_logvar=self.model(x).view(-1,2,self.input_dim)
mu_p=mu_logvar[:,0,:]
logvar_p=mu_logvar[:,1,:]
return mu_p,logvar_p
class prop_ls_Transformer(nn.Module):
def __init__(self,latent_size=latent_size,prop_size=prop_size,extra_size=extra_size):
super().__init__()
#define a few variables
self.latent_size=latent_size
self.prop_size=prop_size
self.extra_size=extra_size
#this generates a set of extra_size properties that will be concatenated with the actual properties
self.enhancer=nn.Sequential(
nn.Linear(prop_size, 128),
nn.Tanh(),
nn.BatchNorm1d(128),
nn.Linear(128,128),
nn.Tanh(),
nn.BatchNorm1d(128),
nn.Linear(128,extra_size),
nn.BatchNorm1d(extra_size)
)
self.embedd=embedder(3)
self.pos_encoder = PositionalEncoding(prop_size+extra_size, dropout=0.5)
encoder_layer = nn.TransformerEncoderLayer(d_model=prop_size+extra_size, nhead=8)
self.transformer_enc = nn.TransformerEncoder(encoder_layer, num_layers=6)
self.encoder = nn.Sequential(
nn.Linear(prop_size+extra_size,2*latent_size)
)
def forward(self,x):
#compute enhanced set of properties
z=self.enhancer(x)
#concatenate with original properties
y=torch.cat((x,z),1)
#compute output
y=self.embedd(y)
y=self.pos_encoder(y)
transformed=self.transformer_enc(y).mean(1)
mu_logvar=self.encoder(transformed.view(-1,self.prop_size+self.extra_size)).view(-1,2,self.latent_size)
mu_p=mu_logvar[:,0,:]
logvar_p=mu_logvar[:,1,:]
return mu_p,logvar_p
class corrector(nn.Module):
def __init__(self,latent_size=latent_size,prop_size=prop_size,extra_size=extra_size,output_size=528):
super().__init__()
#define a few variables
self.output_size=output_size
self.prop_size=prop_size
self.extra_size=extra_size
self.latent_size=latent_size
#this generates a set of extra_size properties that will be concatenated with the actual properties
self.enhancer=nn.Sequential(
nn.Linear(prop_size, 128),
nn.Tanh(),
nn.Linear(128,128),
nn.Tanh(),
nn.Linear(128,extra_size)
)
#feedforward module that takes in the properties and outputs mean and logvar of the output
self.model=nn.Sequential(
nn.Linear(prop_size+extra_size+latent_size, 512),
nn.Tanh(),
nn.Linear(512,1024),
nn.Tanh(),
nn.Linear(1024,output_size)
)
def forward(self,x,lat):
#compute enhanced set of properties
z=self.enhancer(x)
#concatenate with original properties
y=torch.cat((x,z),1)
y=torch.cat((y,lat),1)
#compute output
mu_logvar=self.model(y).view(-1,1,self.output_size)
mu_p=mu_logvar[:,0,:]
#logvar_p=mu_logvar[:,1,:]
return mu_p#,logvar_p
#model that finds the prior based on properties
class prior(nn.Module):
def __init__(self,latent_size=latent_size,prop_size=prop_size):
super().__init__()
#define a few variables
self.latent_size=latent_size
self.prop_size=prop_size
#feedforward module that takes in the properties and outputs mean and logvar of the output
self.model=nn.Sequential(
nn.Linear(prop_size,1024),
nn.Tanh(),
nn.Linear(1024,512),
nn.Tanh(),
nn.Linear(512,2*latent_size)
)
def forward(self,x):
#compute output
mu_logvar=self.model(x).view(-1,2,self.latent_size)
mu_p=mu_logvar[:,0,:]
logvar_p=mu_logvar[:,1,:]
return mu_p,logvar_p