-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
217 lines (168 loc) · 6.68 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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import MessagePassing
from torch_geometric.nn import GCNConv, ChebConv, GATConv
from torch.autograd import Variable
from MethodGraphBert import MethodGraphBert
class MLP(torch.nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, feature_dim = None,
feature_pre=False, layer_num=2, dropout=False, **kwargs):
super(MLP, self).__init__()
self.feature_pre = feature_pre
self.layer_num = layer_num
self.dropout = dropout
if feature_pre:
self.linear_pre = nn.Linear(input_dim, feature_dim)
self.linear_first = nn.Linear(feature_dim, hidden_dim)
else:
self.linear_first = nn.Linear(input_dim, hidden_dim)
self.linear_hidden = nn.ModuleList([nn.Linear(hidden_dim, hidden_dim) for i in range(layer_num - 2)])
self.linear_out = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
if self.feature_pre:
x = self.linear_pre(x)
x = self.linear_first(x)
# x = F.relu(x)
if self.dropout:
x = F.dropout(x, training=self.training)
for i in range(self.layer_num - 2):
x = self.linear_hidden[i](x)
x = F.relu(x)
if self.dropout:
x = F.dropout(x, training=self.training)
x = self.linear_out(x)
return x
class KSNN(MessagePassing):
def __init__(self, input_dim, hidden_dim, output_dim, config = None, layer_num=2, conv_num = 5, alpha=0.9, data_o = None):
super(KSNN, self).__init__(aggr='max')
self.input_dim = input_dim
if config is not None: self.bert = MethodGraphBert(config)
self.encoder = MLP(self.input_dim, hidden_dim, output_dim, layer_num = layer_num)
self.decoder = MLP(output_dim, hidden_dim, self.input_dim, layer_num = layer_num)
self.alpha = alpha
self.conv_num = conv_num
self.output_dim = output_dim
self.hidden_dim = hidden_dim
self.gcn = GCNConv(-1, output_dim)
self.cheb = ChebConv(-1, output_dim, 1)
self.gat = GATConv(-1, output_dim)
self.pca_Z = None
self.pca_v = None
########### KS-PCA
def kspca_msg(self, x, edge_index):
de_X = x @ self.pca_v.t()
x_j = self.propagate(edge_index, x=de_X)*self.alpha
tmp_x = torch.max(de_X,x_j)
return tmp_x @ self.pca_v
def kspca(self, X, edge_index):
x = self.pca_Z if not self.pca_Z is None else self._set_pcaX(X)
for i in range(self.conv_num):
x = self.kspca_msg(x,edge_index)
return x
########### KS-GNN
def msg(self, x, edge_index):
de_X = self.decoder(x)
x_j = self.propagate(edge_index, x=de_X)*self.alpha
tmp_x = torch.max(de_X,x_j)
return self.encoder(tmp_x)
def forward(self, x, edge_index):
x = self.encoder(x)
for i in range(self.conv_num):
x = self.msg(x, edge_index)
return x
########### GraphSAGE
def sage_msg(self, x, edge_index):
de_X = x
x_j = self.propagate(edge_index, x=de_X)
return torch.max(de_X,x_j)
def sage_forward(self, x, edge_index):
x = self.encoder(x)
for i in range(self.conv_num):
x = self.sage_msg(x, edge_index)
return x
# ########### GraphSAGE + Decoder
# def sage_decoder_forward(self, x, edge_index):
# x = self.encoder(x)
# for i in range(self.conv_num):
# x = self.msg(x, edge_index)
# return x
########### GCN
def gcn_forward(self, x, edge_index):
x = self.gcn.forward(x, edge_index)
for i in range(self.conv_num):
x = self.sage_msg(x, edge_index)
return x
########### GCN + Decoder
def gcn_decoder_forward(self, x, edge_index):
x = self.gcn.forward(x, edge_index)
for i in range(self.conv_num):
x = self.msg(x, edge_index)
return x
########### ChebConv
def cheb_forward(self, x, edge_index):
x = self.cheb.forward(x, edge_index)
for i in range(self.conv_num):
x = self.sage_msg(x, edge_index)
return x
########### ChebConv + Decoder
def cheb_decoder_forward(self, x, edge_index):
x = self.cheb.forward(x, edge_index)
for i in range(self.conv_num):
x = self.msg(x, edge_index)
return x
########### GAT
def gat_forward(self, x, edge_index):
x = self.gat.forward(x, edge_index)
for i in range(self.conv_num):
x = self.sage_msg(x, edge_index)
return x
########### GAT + Decoder
def gat_decoder_forward(self, x, edge_index):
x = self.gat.forward(x, edge_index)
for i in range(self.conv_num):
x = self.msg(x, edge_index)
return x
########### Node2Vec
def n2v_forward(self, x, edge_index):
x = self.gcn.forward(x, edge_index)
for i in range(self.conv_num):
x = self.sage_msg(x, edge_index)
return x
########### Node2Vec + Decoder
def n2v_decoder_forward(self, x, edge_index):
x = self.gcn.forward(x, edge_index)
for i in range(self.conv_num):
x = self.msg(x, edge_index)
return x
########### Proposed Method
# def bert_msg(self, x, edge_index):
# de_X = self.decoder(x)
# x_j = self.propagate(edge_index, x=de_X)*self.alpha
# tmp_x = torch.max(de_X,x_j)
# return self.bert(tmp_x)
def bert_msg(self, x, edge_index):
de_X = x
x_j = self.propagate(edge_index, x=de_X)
return torch.max(de_X,x_j)
def bert_forward(self, x, edge_index, raw_embeddings, wl_embedding, int_embeddings, hop_embeddings):
x = self.bert(raw_embeddings, wl_embedding, int_embeddings, hop_embeddings)
x = x[1]
for i in range(self.conv_num):
x = self.bert_msg(x, edge_index)
return x
########### PCA
def _set_pcaX(self, X):
u,s,v = torch.svd(X)
self.pca_v = v[:,:self.output_dim]
self.pca_Z = X @ self.pca_v
return self.pca_Z
def pca_msg(self, x, edge_index):
x_j = self.propagate(edge_index, x=x)*self.alpha
return torch.max(x,x_j)
def pca(self, X, edge_index):
x = self.pca_Z if not self.pca_Z is None else self._set_pcaX(X)
for i in range(self.conv_num):
x = self.pca_msg(x,edge_index)
return x