-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
260 lines (229 loc) · 10.1 KB
/
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
import torch
import torch.nn as nn
from layers import *
from torch_geometric.nn import avg_pool, graclus
from torch_geometric.data import Batch
from layers import SAGEConv
import pickle
from QGraph.v0_1.graph_coarsen import graph_coarsen
# Neural network for the embedding module
class ModelSpectral(torch.nn.Module):
def __init__(self,se_params,device):
super(ModelSpectral,self).__init__()
self.l = se_params.get('l')
self.pre = se_params.get('pre')
self.post = se_params.get('post')
self.coarsening_threshold = se_params.get('coarsening_threshold')
self.activation = getattr(torch, se_params.get('activation'))
self.lins = se_params.get('lins')
self.conv_post = nn.ModuleList(
[SAGEConv(self.l, self.l) for i in range(self.post)]
)
self.conv_coarse = SAGEConv(2,self.l)
self.lins1=nn.Linear(self.l,self.lins[0])
self.lins2=nn.Linear(self.lins[0],self.lins[1])
self.lins3=nn.Linear(self.lins[1],self.lins[2])
self.final=nn.Linear(self.lins[2],2)
self.device = device
def forward(self, graph):
x, edge_index, batch = graph.x, graph.edge_index, graph.batch
unpool_info = []
x_info=[]
cluster_info=[]
edge_info=[]
while x.size()[0] > self.coarsening_threshold:
cluster = graclus(edge_index,num_nodes=x.shape[0])
cluster_info.append(cluster)
edge_info.append(edge_index)
gc = avg_pool(cluster, Batch(batch=batch, x=x, edge_index=edge_index,shuffle=False))
x, edge_index, batch = gc.x, gc.edge_index, gc.batch
# coarse iterations
x=torch.eye(2).to(self.device)
x=self.conv_coarse(x,edge_index)
x=self.activation(x)
while edge_info:
# un-pooling / interpolation / prolongation / refinement
edge_index = edge_info.pop()
cluster = cluster_info.pop()
output, inverse = torch.unique(cluster, return_inverse=True)
x = x[inverse]
# post-smoothing
for i in range(self.post):
x = self.activation(self.conv_post[i](x, edge_index))
x=self.lins1(x)
x=self.activation(x)
x=self.lins2(x)
x=self.activation(x)
x=self.lins3(x)
x=self.activation(x)
x=self.final(x)
x,_=torch.linalg.qr(x,mode='reduced')
return x
# Neural network for the partitioning module
class ModelPartitioning(torch.nn.Module):
def __init__(self,pe_params):
super(ModelPartitioning,self).__init__()
self.l = pe_params.get('l')
self.pre = pe_params.get('pre')
self.post = pe_params.get('post')
self.coarsening_threshold = pe_params.get('coarsening_threshold')
self.activation = getattr(torch, pe_params.get('activation'))
self.lins = pe_params.get('lins')
self.conv_first = SAGEConv(1, self.l)
self.conv_pre = nn.ModuleList(
[SAGEConv(self.l, self.l) for i in range(self.pre)]
)
self.conv_post = nn.ModuleList(
[SAGEConv(self.l, self.l) for i in range(self.post)]
)
self.conv_coarse = SAGEConv(self.l,self.l)
self.lins1=nn.Linear(self.l,self.lins[0])
self.lins2=nn.Linear(self.lins[0],self.lins[1])
self.lins3=nn.Linear(self.lins[1],self.lins[2])
self.final=nn.Linear(self.lins[4],2)
def forward(self, graph):
x, edge_index, batch = graph.x, graph.edge_index, graph.batch
x = self.activation(self.conv_first(x, edge_index))
unpool_info = []
x_info=[]
cluster_info=[]
edge_info=[]
batches=[]
while x.size()[0] > self.coarsening_threshold:
# pre-smoothing
for i in range(self.pre):
x = self.activation(self.conv_pre[i](x, edge_index))
# pooling / coarsening / restriction
x_info.append(x)
batches.append(batch)
cluster = graph_coarsen(edge_index[0],edge_index[1],weight=None,num_nodes=x.shape[0])
cluster_info.append(cluster)
edge_info.append(edge_index)
gc = avg_pool(cluster, Batch(batch=batch, x=x, edge_index=edge_index))
x, edge_index, batch = gc.x, gc.edge_index, gc.batch
# coarse iterations
x = self.activation(self.conv_coarse(x,edge_index))
while edge_info:
# un-pooling / interpolation / prolongation / refinement
edge_index = edge_info.pop()
output, inverse = torch.unique(cluster_info.pop(), return_inverse=True)
x = (x[inverse] + x_info.pop())/2
# post-smoothing
for i in range(self.post):
x = self.activation(self.conv_post[i](x, edge_index))
x=self.lins1(x)
x=self.activation(x)
x=self.lins2(x)
x=self.activation(x)
x=self.lins3(x)
x=self.activation(x)
x=self.final(x)
x=torch.softmax(x,dim=1)
return x
# 2022_10_26
class ModelSpectral_1026(torch.nn.Module):
def __init__(self,se_params,device):
super(ModelSpectral_1026,self).__init__()
self.l = se_params.get('l')
self.pre = se_params.get('pre')
self.post = se_params.get('post')
self.coarsening_threshold = se_params.get('coarsening_threshold')
self.activation = getattr(torch, se_params.get('activation'))
self.lins = se_params.get('lins')
self.conv_post = nn.ModuleList(
[SAGEConv(self.l, self.l) for i in range(self.post)]
# [ChebConv(self.l, self.l,3) for i in range(self.post)]
)
self.conv_coarse = SAGEConv(2,self.l)
# self.conv_coarse = ChebConv(2,self.l,3)
self.lins1=nn.Linear(self.l,self.lins[0])
# self.bn_1=nn.BatchNorm1d(self.lins[0])
self.lins2=nn.Linear(self.lins[0],self.lins[1])
# self.bn_2=nn.BatchNorm1d(self.lins[1])
self.lins3=nn.Linear(self.lins[1],self.lins[2])
# self.bn_3=nn.BatchNorm1d(self.lins[2])
self.final=nn.Linear(self.lins[2],2)
# self.bn_qr=nn.BatchNorm1d(2)
self.device = device
self.arr_edge = None
def forward(self,graph):
if self.arr_edge is None:
x,edge_index,batch=graph.x,graph.edge_index,graph.batch
cluster_info=[]
edge_info=[]
while x.size()[0]>self.coarsening_threshold:
cluster = graph_coarsen(edge_index[0],edge_index[1],weight=None,num_nodes=x.shape[0])
cluster_info.append(cluster)
edge_info.append(edge_index)
gc = avg_pool(cluster, Batch(batch=batch, x=x, edge_index=edge_index,shuffle=False))
x, edge_index, batch = gc.x, gc.edge_index, gc.batch
self.arr_cluster = cluster_info
self.arr_edge = edge_info
self.last_gc = gc
nMap = len(self.arr_cluster)
edge_index = self.last_gc.edge_index
x = torch.eye(2).to(self.device)
x = self.conv_coarse(x,edge_index)
x = self.activation(x)
for map_no in reversed(range(nMap)):
edge_index = self.arr_edge[map_no]
cluster = self.arr_cluster[map_no]
output, inverse = torch.unique(cluster, return_inverse=True)
x = x[inverse]
for i in range(self.post):
x = self.activation(self.conv_post[i](x, edge_index))
x=self.lins1(x)
x=self.activation(x)
x=self.lins2(x)
x=self.activation(x)
x=self.lins3(x)
x=self.activation(x)
x=self.final(x)
x,_=torch.linalg.qr(x,mode='reduced')
return x
def get_info(self):
return self.arr_cluster,self.arr_edge,self.last_gc
class ModelPartitioning_1026(torch.nn.Module):
def __init__(self,pe_params):
super(ModelPartitioning_1026,self).__init__()
self.l = pe_params.get('l')
self.pre = pe_params.get('pre')
self.post = pe_params.get('post')
self.coarsening_threshold = pe_params.get('coarsening_threshold')
self.activation = getattr(torch, pe_params.get('activation'))
self.lins = pe_params.get('lins')
self.conv_first = SAGEConv(1, self.l)
self.conv_pre = nn.ModuleList(
[SAGEConv(self.l, self.l) for i in range(self.pre)]
)
self.conv_post = nn.ModuleList(
[SAGEConv(self.l, self.l) for i in range(self.post)]
)
self.conv_coarse = SAGEConv(self.l,self.l)
self.lins1=nn.Linear(self.l,self.lins[0])
self.lins2=nn.Linear(self.lins[0],self.lins[1])
self.lins3=nn.Linear(self.lins[1],self.lins[2])
self.final=nn.Linear(self.lins[4],2)
self.arr_edge = None
def forward(self,graph,cluster_info,edge_info):
x, edge_index, batch = graph.x, graph.edge_index, graph.batch
x = self.activation(self.conv_first(x, edge_index))
if self.arr_edge is None:
x_info = []
cluster_info = []
edge_info = []
while x.size()[0] > self.coarsening_threshold:
# pre-smoothing
for i in range(self.pre):
x = self.activation(self.conv_pre[i](x, edge_index))
# def down_sampling(graph,coarsening_threshold):
# x,edge_index,batch = graph.x,graph.edge_index,graph.batch
# cluster_info = []
# edge_info = []
# while x.size()[0]>coarsening_threshold:
# cluster = graph_coarsen(edge_index[0], edge_index[1],weight=None,num_nodes=x.shape[0])
# cluster_info.append(cluster)
# edge_info.append(edge_index)
# gc = avg_pool(cluster,Batch(batch=batch, x=x, edge_index=edge_index,shuffle=False))
# x,edge_index,batch = gc.x,gc.edge_index,gc.batch
# return cluster_info,edge_info,gc