forked from yh-yao/InterpretableClustering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
403 lines (285 loc) · 12.4 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import torch.nn as nn
import torch.nn.functional as F
import torch
import math
from torch.nn.parameter import Parameter
from layers import GraphConvolution
from dgl.nn.pytorch.conv import GraphConv,GATConv,SAGEConv
import dgl
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
class RNNGCN(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(RNNGCN, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nclass)
self.dropout = dropout
self.Lambda = Parameter(torch.FloatTensor(1))
self.Lambda.data.uniform_(0.2, 0.2)
def forward(self, x, adj):
#out=[]
now_adj=adj[:,0,:].clone()
for i in range(1,adj.shape[1]): #time_steps
now_adj=(1-self.Lambda)*now_adj+self.Lambda*adj[:,i,:] #weight decay
one_out=self.gc1(x[:,-1,:],now_adj)
one_out=F.relu(one_out)
one_out = F.dropout(one_out, self.dropout, training=self.training)
one_out = self.gc2(one_out,now_adj)
return F.log_softmax(one_out, dim=1)
class TRNNGCN(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout,nnode,use_cuda=False):
super(TRNNGCN, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nclass)
self.dropout = dropout
self.Lambda = Parameter(torch.FloatTensor(nclass,nclass))
self.Lambda.data.uniform_(0.5, 0.5)
self.use_cuda=use_cuda
y=torch.randint(0,nclass,(nnode,1)).flatten()
if self.use_cuda:
self.H = torch.zeros(nnode, nclass).cuda()
else:
self.H = torch.zeros(nnode, nclass)
self.H[range(self.H.shape[0]), y]=1
def forward(self, x, adj):
w=self.Lambda.data
w=w.clamp(0,1)
self.Lambda.data=w
if self.use_cuda:
decay_adj=torch.mm(torch.mm(self.H,self.Lambda),self.H.T).cuda()
else:
decay_adj=torch.mm(torch.mm(self.H,self.Lambda),self.H.T)
now_adj=adj[:,0,:].clone()#torch.zeros(adj.shape[0], adj.shape[2])
for i in range(1,adj.shape[1]): #time_steps
now_adj=(1-decay_adj)*now_adj+decay_adj*adj[:,i,:]
del decay_adj
one_out=F.relu(self.gc1(x[:,-1,:],now_adj))
one_out = F.dropout(one_out, self.dropout, training=self.training)
one_out = self.gc2(one_out,now_adj)
output=F.log_softmax(one_out, dim=1)
y=torch.argmax(output,dim=1)
H_shape=self.H.shape
del self.H
del now_adj
if self.use_cuda:
self.H = torch.zeros(H_shape).cuda()
else:
self.H = torch.zeros(H_shape)
self.H[range(H_shape[0]), y]=1
return output
class LSTMGCN(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(LSTMGCN, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nclass)
self.dropout = dropout
self.LS_begin=nn.LSTM(input_size=nfeat, hidden_size=nhid, num_layers=1, dropout=0.5,batch_first=True)
self.nhid=nhid
def forward(self, x, adj):
adj=self.LS_begin(adj)
x = F.relu(self.gc1(x[:,-1,:], adj[0][:,-1,:]))
x = F.dropout(x, self.dropout, training=self.training)
x = self.gc2(x, adj[0][:,-1,:])
return F.log_softmax(x, dim=1)
class GCNLSTM(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(GCNLSTM, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nhid)
self.dropout = dropout
self.LS_end=nn.LSTM(input_size=nhid, hidden_size=nclass, num_layers=2, dropout=0.5,
batch_first=True)
self.nhid=nhid
self.nclass=nclass
self.linear=nn.Linear(nclass, nclass)
def forward(self, x, adj):
out=[]
for i in range(adj.shape[1]):
one_out=F.relu(self.gc1(x[:,i,:],adj[:,i,:]))
one_out = F.dropout(one_out, self.dropout, training=self.training)
one_out = self.gc2(one_out, adj[:,i,:])
out+=[one_out]
out = torch.stack(out, 1)
out=self.LS_end(out)[0][:,-1,:]
return F.log_softmax(out, dim=1)
class GCN(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(GCN, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nclass)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = self.gc2(x, adj)
return F.log_softmax(x, dim=1)
class GAT(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(GAT, self).__init__()
self.dropout = dropout
self.conv1 = GATConv(nfeat, nhid, num_heads=1)
self.conv2 = GATConv(nhid, nclass, num_heads=1)
def forward(self, x, adj):
# Use node degree as the initial node feature. For undirected graphs, the in-degree
# is the same as the out_degree.
# Perform graph convolution and activation function.
x = F.relu(self.conv1(adj, x)) #different from self-defined gcn
x=x.reshape(x.shape[0],x.shape[2])
x = F.dropout(x, self.dropout, training=self.training)
x = self.conv2(adj, x)
x=x.reshape(x.shape[0],x.shape[2])
return F.log_softmax(x, dim=1)
class GraphSage(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(GraphSage, self).__init__()
self.dropout = dropout
self.conv1 = SAGEConv(nfeat, nhid,aggregator_type='mean')
self.conv2 = SAGEConv(nhid, nclass,aggregator_type='mean')
def forward(self, x, adj):
# Use node degree as the initial node feature. For undirected graphs, the in-degree
# is the same as the out_degree.
# Perform graph convolution and activation function.
x = F.relu(self.conv1(adj, x)) #different from self-defined gcn
x = F.dropout(x, self.dropout, training=self.training)
x = self.conv2(adj, x)
return F.log_softmax(x, dim=1)
#egcn
class Namespace(object):
'''
helps referencing object in a dictionary as dict.key instead of dict['key']
'''
def __init__(self, adict):
self.__dict__.update(adict)
def pad_with_last_val(vect,k):
device = 'cuda' if vect.is_cuda else 'cpu'
pad = torch.ones(k - vect.size(0),
dtype=torch.long,
device = device) * vect[-1]
vect = torch.cat([vect,pad])
return vect
#only use EGCN
class EGCN(torch.nn.Module): #egcn_o
def __init__(self, nfeat, nhid, nclass, device='cpu', skipfeats=False):
super().__init__()
GRCU_args = Namespace({})
feats = [nfeat,
nhid,
nhid]
self.device = device
self.skipfeats = skipfeats
self.GRCU_layers = []
self._parameters = nn.ParameterList()
self.mlp = torch.nn.Sequential(torch.nn.Linear(in_features = nhid,out_features = nhid),
torch.nn.ReLU(),
torch.nn.Linear(in_features = nhid,out_features = nclass))
for i in range(1,len(feats)):
GRCU_args = Namespace({'in_feats' : feats[i-1],
'out_feats': feats[i],
'activation': torch.nn.RReLU()})
grcu_i = GRCU(GRCU_args)
#print (i,'grcu_i', grcu_i)
self.GRCU_layers.append(grcu_i.to(self.device))
self._parameters.extend(list(self.GRCU_layers[-1].parameters()))
def parameters(self):
return self._parameters
def forward(self,Nodes_list, A_list):#,nodes_mask_list):
node_feats= Nodes_list[-1]
for unit in self.GRCU_layers:
Nodes_list = unit(A_list,Nodes_list)#,nodes_mask_list)
out = Nodes_list[-1]
if self.skipfeats:
out = torch.cat((out,node_feats), dim=1) # use node_feats.to_dense() if 2hot encoded input
return F.log_softmax(self.mlp(out), dim=1)
class GRCU(torch.nn.Module):
def __init__(self,args):
super().__init__()
self.args = args
cell_args = Namespace({})
cell_args.rows = args.in_feats
cell_args.cols = args.out_feats
self.evolve_weights = mat_GRU_cell(cell_args)
self.activation = self.args.activation
self.GCN_init_weights = Parameter(torch.Tensor(self.args.in_feats,self.args.out_feats))
self.reset_param(self.GCN_init_weights)
def reset_param(self,t):
#Initialize based on the number of columns
stdv = 1. / math.sqrt(t.size(1))
t.data.uniform_(-stdv,stdv)
def forward(self,A_list,node_embs_list):#,mask_list):
GCN_weights = self.GCN_init_weights
out_seq = []
for t,Ahat in enumerate(A_list):
node_embs = node_embs_list[t]
#first evolve the weights from the initial and use the new weights with the node_embs
GCN_weights = self.evolve_weights(GCN_weights)#,node_embs,mask_list[t])
node_embs = self.activation(Ahat.matmul(node_embs.matmul(GCN_weights)))
out_seq.append(node_embs)
return out_seq
class mat_GRU_cell(torch.nn.Module):
def __init__(self,args):
super().__init__()
self.args = args
self.update = mat_GRU_gate(args.rows,
args.cols,
torch.nn.Sigmoid())
self.reset = mat_GRU_gate(args.rows,
args.cols,
torch.nn.Sigmoid())
self.htilda = mat_GRU_gate(args.rows,
args.cols,
torch.nn.Tanh())
self.choose_topk = TopK(feats = args.rows,
k = args.cols)
def forward(self,prev_Q):#,prev_Z,mask): ###Same as GCNH
# z_topk = self.choose_topk(prev_Z,mask)
z_topk = prev_Q
update = self.update(z_topk,prev_Q)
reset = self.reset(z_topk,prev_Q)
h_cap = reset * prev_Q
h_cap = self.htilda(z_topk, h_cap)
new_Q = (1 - update) * prev_Q + update * h_cap
return new_Q
class mat_GRU_gate(torch.nn.Module):
def __init__(self,rows,cols,activation):
super().__init__()
self.activation = activation
#the k here should be in_feats which is actually the rows
self.W = Parameter(torch.Tensor(rows,rows))
self.reset_param(self.W)
self.U = Parameter(torch.Tensor(rows,rows))
self.reset_param(self.U)
self.bias = Parameter(torch.zeros(rows,cols))
def reset_param(self,t):
#Initialize based on the number of columns
stdv = 1. / math.sqrt(t.size(1))
t.data.uniform_(-stdv,stdv)
def forward(self,x,hidden):
out = self.activation(self.W.matmul(x) + \
self.U.matmul(hidden) + \
self.bias)
return out
class TopK(torch.nn.Module):
def __init__(self,feats,k):
super().__init__()
self.scorer = Parameter(torch.Tensor(feats,1))
self.reset_param(self.scorer)
self.k = k
def reset_param(self,t):
#Initialize based on the number of rows
stdv = 1. / math.sqrt(t.size(0))
t.data.uniform_(-stdv,stdv)
def forward(self,node_embs,mask):
scores = node_embs.matmul(self.scorer) / self.scorer.norm()
scores = scores + mask
vals, topk_indices = scores.view(-1).topk(self.k)
topk_indices = topk_indices[vals > -float("Inf")]
if topk_indices.size(0) < self.k:
topk_indices = u.pad_with_last_val(topk_indices,self.k)
tanh = torch.nn.Tanh()
if isinstance(node_embs, torch.sparse.FloatTensor) or \
isinstance(node_embs, torch.cuda.sparse.FloatTensor):
node_embs = node_embs.to_dense()
out = node_embs[topk_indices] * tanh(scores[topk_indices].view(-1,1))
#we need to transpose the output
return out.t()