-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeso_models.py
More file actions
374 lines (329 loc) · 13.5 KB
/
meso_models.py
File metadata and controls
374 lines (329 loc) · 13.5 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import numpy as np
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
from torch.optim.lr_scheduler import StepLR, OneCycleLR, CyclicLR
import torch
from copy import deepcopy
from numpy.random import randn # importing randn
from torch.nn import BatchNorm1d
from torch.nn import Sequential, Linear, ReLU
from torch_geometric.nn import (
GINConv,
EdgeConv,
DynamicEdgeConv,
global_add_pool,
global_mean_pool,
global_max_pool,
PANConv,
PANPooling,
global_sort_pool,
)
from torch_geometric.data import Data, DataLoader
"""MesoGraph model architecture definitions"""
class learnable_sig(torch.nn.Module):
def __init__(self, fsize, type="branched") -> None:
super(learnable_sig, self).__init__()
if type == "branched":
self.l1 = Sequential(Linear(fsize, fsize), ReLU(), Linear(fsize, 2))
self.l2 = Sequential(Linear(fsize, fsize), ReLU(), Linear(fsize, 2))
self.alpha = nn.parameter.Parameter(2 * torch.ones(1, 2))
self.beta = nn.parameter.Parameter(torch.zeros(1, 2))
self.gamma = nn.parameter.Parameter(torch.zeros(1, 2))
else:
self.l1 = Sequential(Linear(fsize, fsize), ReLU(), Linear(fsize, 1))
self.l2 = Sequential(Linear(fsize, fsize), ReLU(), Linear(fsize, 1))
self.alpha = nn.parameter.Parameter(torch.ones(1))
self.beta = nn.parameter.Parameter(torch.zeros(1))
self.gamma = nn.parameter.Parameter(torch.zeros(1))
def forward(self, x, xcore, batch):
y = []
for i in torch.unique(batch):
# last_ind=torch.sum(batch<=i)-1
# y.append(torch.sigmoid(x[batch==i,:]*self.alpha-self.beta+self.gamma*torch.mean(x[batch==i],dim=0,keepdim=True))) #1
# y.append(torch.sigmoid(x[batch==i,:]*self.alpha-self.beta)) #2
# y.append(torch.sigmoid(x[batch==i,:]-self.beta))
y.append(torch.sigmoid(x[batch == i, :] - self.l1(xcore.T[:, i])))
# y.append(torch.sigmoid(x[batch==i,:]*self.alpha-0.1*self.l1(xcore.T[:,i])))
# y.append(torch.sigmoid(x[batch==i,:]*self.l2(xcore.T[:,i])-self.l1(xcore.T[:,i])))
return torch.cat(y)
class MesoBranched(torch.nn.Module):
def __init__(
self,
dim_features,
dim_target,
layers=[16],
pooling="max",
dropout=0.0,
eps=0.0,
train_eps=False,
do_ls=False,
):
super(MesoBranched, self).__init__()
self.dropout = dropout
self.embeddings_dim = layers
self.do_ls = do_ls
if do_ls:
self.ls = learnable_sig(dim_features, type="branched")
# self.ls=learnable_sig(np.sum(layers)) #if on embed feats
self.no_layers = len(self.embeddings_dim)
self.first_h = []
self.nns = []
self.convs = []
self.linears = []
self.pooling = {
"max": global_max_pool,
"mean": global_mean_pool,
"add": global_add_pool,
"PAN": PANPooling(in_channels=1, ratio=0.5),
"topN": global_sort_pool,
}[pooling]
self.ecnns = []
self.ecs = []
self.last = None
if dim_target > 2:
self.last = nn.Sequential(ReLU(), Linear(dim_target, 2))
# train_eps = True#config['train_eps']
# TOTAL NUMBER OF PARAMETERS #
# first: dim_features*out_emb_dim + 4*out_emb_dim + out_emb_dim*out_emb_dim + 4*out_emb_dim + out_emb_dim*target
# l-th: input_emb_dim*out_emb_dim + 4*out_emb_dim + out_emb_dim*out_emb_dim + 4*out_emb_dim + out_emb_dim*target
# -------------------------- #
for layer, out_emb_dim in enumerate(self.embeddings_dim):
if layer == 0:
self.first_h = Sequential(
Linear(dim_features, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
Linear(out_emb_dim, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
)
self.linears.append(Linear(out_emb_dim, dim_target))
else:
input_emb_dim = self.embeddings_dim[layer - 1]
self.linears.append(Linear(out_emb_dim, dim_target))
subnet = Sequential(
Linear(2 * input_emb_dim, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
Linear(out_emb_dim, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
)
self.ecnns.append(subnet)
self.ecs.append(
EdgeConv(self.ecnns[-1], aggr="mean")
)
self.linears = torch.nn.ModuleList(
self.linears
) # has got one more for initial input
self.ecnns = torch.nn.ModuleList(self.ecnns)
self.ecs = torch.nn.ModuleList(self.ecs)
def forward(self, x, edge_index=None, edge_weight=None, batch=None):
if edge_index == None:
xfeat, edge_index, batch = x.x, x.edge_index, x.batch
explaining = False
else:
xfeat = x
batch = torch.zeros((xfeat.shape[0],), dtype=torch.long, device="cuda")
explaining = True
out = 0
pooling = self.pooling
Z = 0
last_ind = []
for i in torch.unique(batch):
last_ind.append(torch.sum(batch <= i) - 1)
core_x = []
for layer in range(self.no_layers):
# print(f'Forward: layer {l}')
if layer == 0:
# x, M = self.first_h(x, edge_index)
x = self.first_h(xfeat)
if self.do_ls:
core_x.append(x[last_ind, :])
z = F.dropout(
self.linears[layer](x), p=self.dropout, training=self.training
)
Z += z
# dout = F.dropout(pooling(z, batch), p=self.dropout, training=self.training)
# dout = F.dropout(torch.mean(pooling(z, batch, 1000),dim=1,keepdim=True), p=self.dropout, training=self.training)
# dout=pooling(z, M, batch)
# dout=global_mean_pool(dout[0],dout[3])
# out += dout
else:
# x = self.convs[layer-1](x, edge_index)
x = self.ecs[layer - 1](x, edge_index)
if self.do_ls:
core_x.append(x[last_ind, :])
# x = self.ecs[layer-1](x,batch)
z = F.dropout(
self.linears[layer](x), p=self.dropout, training=self.training
)
Z += z
# dout=pooling(z, M, batch)
# dout = F.dropout(torch.mean(pooling(z, batch, 1000),dim=1,keepdim=True), p=self.dropout, training=self.training)
# dout = F.dropout(pooling(z, batch), p=self.dropout, training=self.training)#F.dropout(self.linears[layer](pooling(x, batch)), p=self.dropout, training=self.training)
# out += dout
if self.last:
Z = self.last(Z)
if self.do_ls:
core_x = torch.cat(core_x, dim=1)
# ZZ=self.ls(Z,core_x.detach(),batch) #if want sig thresh learnt on embed feats but not backprop to edgeconv weights etc
# ZZ=self.ls(Z,core_x,batch)
ZZ = self.ls(Z, xfeat[last_ind, :], batch)
out = pooling(ZZ, batch)
if explaining:
return out
else:
return out, ZZ
else:
out = pooling(Z, batch)
if explaining:
return out
else:
return out, Z
class MesoSep(torch.nn.Module):
def __init__(
self,
dim_features,
dim_target,
layers=[16],
pooling="max",
dropout=0.0,
eps=0.0,
train_eps=False,
do_ls=False,
feats=[],
):
super(MesoSep, self).__init__()
self.dropout = dropout
self.embeddings_dim = layers
self.do_ls = do_ls
self.no_layers = len(self.embeddings_dim)
self.first_h = []
self.nns = []
self.convs = []
self.linears = []
self.pooling = {
"max": global_max_pool,
"mean": global_mean_pool,
"add": global_add_pool,
"PAN": PANPooling(in_channels=1, ratio=0.5),
"topN": global_sort_pool,
}[pooling]
self.ecnns = []
self.ecs = []
self.dim_features = dim_features
self.subE = self.make_subnet()
self.subS = self.make_subnet()
self.feats = feats
# train_eps = True#config['train_eps']
# TOTAL NUMBER OF PARAMETERS #
# first: dim_features*out_emb_dim + 4*out_emb_dim + out_emb_dim*out_emb_dim + 4*out_emb_dim + out_emb_dim*target
# l-th: input_emb_dim*out_emb_dim + 4*out_emb_dim + out_emb_dim*out_emb_dim + 4*out_emb_dim + out_emb_dim*target
# -------------------------- #
def make_subnet(self):
ecnns = []
ecs = []
linears = []
for layer, out_emb_dim in enumerate(self.embeddings_dim):
if layer == 0:
first_h = Sequential(
Linear(self.dim_features, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
Linear(out_emb_dim, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
)
linears.append(Linear(out_emb_dim, 1))
else:
input_emb_dim = self.embeddings_dim[layer - 1]
linears.append(Linear(out_emb_dim, 1))
subnet = Sequential(
Linear(2 * input_emb_dim, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
Linear(out_emb_dim, out_emb_dim),
BatchNorm1d(out_emb_dim),
ReLU(),
)
ecnns.append(subnet)
ecs.append(EdgeConv(ecnns[-1], aggr="mean"))
linears = torch.nn.ModuleList(linears) # has got one more for initial input
ecnns = torch.nn.ModuleList(ecnns)
ecs = torch.nn.ModuleList(ecs)
if self.do_ls:
ls = learnable_sig(self.dim_features, type="sep")
return nn.ModuleDict(
{
"first": first_h,
"linears": linears,
"ecnns": ecnns,
"ecs": ecs,
"ls": ls,
}
)
return nn.ModuleDict(
{"first": first_h, "linears": linears, "ecnns": ecnns, "ecs": ecs}
)
def forward_sub(self, sub, data, edge_index=None, edge_weight=None):
if edge_index == None:
xfeat, edge_index, batch = data.x, data.edge_index, data.batch
else:
xfeat = data
batch = torch.zeros((xfeat.shape[0],), dtype=torch.long, device="cuda")
out = 0
pooling = self.pooling
Z = 0
last_ind = []
for i in torch.unique(batch):
last_ind.append(torch.sum(batch <= i) - 1)
core_x = []
for layer in range(self.no_layers):
if layer == 0:
# x, M = self.first_h(x, edge_index)
x = sub["first"](xfeat)
if self.do_ls:
core_x.append(x[last_ind, :])
z = sub["linears"][layer](x)
Z += z
# dout = F.dropout(pooling(z, batch), p=self.dropout, training=self.training)
# dout = F.dropout(torch.mean(pooling(z, batch, 1000),dim=1,keepdim=True), p=self.dropout, training=self.training)
# dout=pooling(z, M, batch)
# dout=global_mean_pool(dout[0],dout[3])
# out += dout
else:
# Layer l ("convolution" layer)
# import pdb;pdb.set_trace()
# x = self.convs[layer-1](x, edge_index)
x = sub["ecs"][layer - 1](x, edge_index)
if self.do_ls:
core_x.append(x[last_ind, :])
# x = self.ecs[layer-1](x,batch)
z = sub["linears"][layer](x)
Z += z
# dout=pooling(z, M, batch)
# dout = F.dropout(torch.mean(pooling(z, batch, 1000),dim=1,keepdim=True), p=self.dropout, training=self.training)
# dout = F.dropout(pooling(z, batch), p=self.dropout, training=self.training)#F.dropout(self.linears[layer](pooling(x, batch)), p=self.dropout, training=self.training)
# out += dout
if self.do_ls:
core_x = torch.cat(core_x, dim=1)
# ZZ=self.ls(Z,core_x.detach(),batch) #learn based on embed
ZZ = sub["ls"](
Z, xfeat[last_ind, :], batch
) # learn sig thresh based on base feats
out = pooling(ZZ, batch)
return out, ZZ
else:
out = pooling(Z, batch)
return out, Z
def forward(self, x, edge_index=None, edge_weight=None, batch=None):
core_outE, cell_outE = self.forward_sub(self.subE, x, edge_index, edge_weight)
core_outS, cell_outS = self.forward_sub(self.subS, x, edge_index, edge_weight)
core_out = torch.cat([core_outE, core_outS], dim=1)
cell_out = torch.cat([cell_outE, cell_outS], dim=1)
if edge_index == None:
return core_out, cell_out
else:
return cell_out