-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
255 lines (212 loc) · 8.16 KB
/
models.py
File metadata and controls
255 lines (212 loc) · 8.16 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
import torch
import random
import math
import torch.nn.functional as F
import os.path as osp
import numpy as np
import torch_geometric.transforms as T
from torch_geometric.nn.conv.gcn_conv import gcn_norm
from torch.autograd import Variable
from torch.nn import Parameter
from torch.nn import Linear
#from torch_geometric.nn import GATConv, GCNConv, ChebConv
from torch_geometric.nn import GATConv, ChebConv
from gcn_conv import GCNConv
from torch_geometric.nn import MessagePassing, APPNP
from torch_geometric.utils import to_scipy_sparse_matrix
import scipy.sparse as sp
from scipy.special import comb
from Bernpro import Bern_prop
class GPR_prop(MessagePassing):
'''
propagation class for GPR_GNN
'''
def __init__(self, K, alpha, Init, Gamma=None, bias=True, **kwargs):
super(GPR_prop, self).__init__(aggr='add', **kwargs)
self.K = K
self.Init = Init
self.alpha = alpha
assert Init in ['SGC', 'PPR', 'NPPR', 'Random', 'WS']
if Init == 'SGC':
# SGC-like
TEMP = 0.0*np.ones(K+1)
TEMP[-1] = 1.0
elif Init == 'PPR':
# PPR-like
TEMP = alpha*(1-alpha)**np.arange(K+1)
TEMP[-1] = (1-alpha)**K
elif Init == 'NPPR':
# Negative PPR
TEMP = (alpha)**np.arange(K+1)
TEMP = TEMP/np.sum(np.abs(TEMP))
elif Init == 'Random':
# Random
bound = np.sqrt(3/(K+1))
TEMP = np.random.uniform(-bound, bound, K+1)
TEMP = TEMP/np.sum(np.abs(TEMP))
elif Init == 'WS':
# Specify Gamma
TEMP = Gamma
self.temp = Parameter(torch.tensor(TEMP))
def reset_parameters(self):
torch.nn.init.zeros_(self.temp)
for k in range(self.K+1):
self.temp.data[k] = self.alpha*(1-self.alpha)**k
self.temp.data[-1] = (1-self.alpha)**self.K
def forward(self, x, edge_index, edge_weight=None):
edge_index, norm = gcn_norm(
edge_index, edge_weight, num_nodes=x.size(0), dtype=x.dtype)
hidden = x*(self.temp[0])
for k in range(self.K):
x = self.propagate(edge_index, x=x, norm=norm)
gamma = self.temp[k+1]
hidden = hidden + gamma*x
return hidden
def message(self, x_j, norm):
return norm.view(-1, 1) * x_j
def __repr__(self):
return '{}(K={}, temp={})'.format(self.__class__.__name__, self.K,
self.temp)
def message(self, x_j, norm):
return norm.view(-1, 1) * x_j
def __repr__(self):
return '{}(K={}, temp={})'.format(self.__class__.__name__, self.K,
self.temp)
class GPRGNN(torch.nn.Module):
def __init__(self, dataset, args):
super(GPRGNN, self).__init__()
self.lin1 = Linear(dataset.num_features, args.hidden)
self.lin2 = Linear(args.hidden, dataset.num_classes)
self.prop1 = GPR_prop(args.K, args.alpha, args.Init)
self.Init = args.Init
self.dprate = args.dprate
self.dropout = args.dropout
def reset_parameters(self):
self.prop1.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.dropout(x, p=self.dropout, training=self.training)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin2(x)
if self.dprate == 0.0:
x = self.prop1(x, edge_index)
return x
else:
x = F.dropout(x, p=self.dprate, training=self.training)
x = self.prop1(x, edge_index)
return x
class BernNet(torch.nn.Module):
def __init__(self,dataset, args):
super(BernNet, self).__init__()
self.lin1 = Linear(dataset.num_features, args.hidden)
self.lin2 = Linear(args.hidden, dataset.num_classes)
self.m = torch.nn.BatchNorm1d(dataset.num_classes)
self.prop1 = Bern_prop(args.K)
self.dprate = args.dprate
self.dropout = args.dropout
def reset_parameters(self):
self.prop1.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.dropout(x, p=self.dropout, training=self.training)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin2(x)
#x= self.m(x)
if self.dprate == 0.0:
x = self.prop1(x, edge_index)
return x
else:
x = F.dropout(x, p=self.dprate, training=self.training)
x = self.prop1(x, edge_index)
return x
class GCN_Net(torch.nn.Module):
def __init__(self, dataset, args):
super(GCN_Net, self).__init__()
self.conv1 = GCNConv(dataset.num_features, args.hidden)
self.conv2 = GCNConv(args.hidden, dataset.num_classes)
self.dropout = args.dropout
def reset_parameters(self):
self.conv1.reset_parameters()
self.conv2.reset_parameters()
def forward(self, data, return_rep=False):
x, edge_index = data.x, data.edge_index
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.conv2(x, edge_index, return_rep=return_rep)
return x
class ChebNet(torch.nn.Module):
def __init__(self, dataset, args):
super(ChebNet, self).__init__()
self.conv1 = ChebConv(dataset.num_features, 32, K=2)
self.conv2 = ChebConv(32, dataset.num_classes, K=2)
self.dropout = args.dropout
def reset_parameters(self):
self.conv1.reset_parameters()
self.conv2.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.conv2(x, edge_index)
return x
class GAT_Net(torch.nn.Module):
def __init__(self, dataset, args):
super(GAT_Net, self).__init__()
self.conv1 = GATConv(
dataset.num_features,
args.hidden,
heads=args.heads,
dropout=args.dropout)
self.conv2 = GATConv(
args.hidden * args.heads,
dataset.num_classes,
heads=args.output_heads,
concat=False,
dropout=args.dropout)
self.dropout = args.dropout
def reset_parameters(self):
self.conv1.reset_parameters()
self.conv2.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.dropout(x, p=self.dropout, training=self.training)
x = F.elu(self.conv1(x, edge_index))
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.conv2(x, edge_index)
return x
class APPNP_Net(torch.nn.Module):
def __init__(self, dataset, args):
super(APPNP_Net, self).__init__()
self.lin1 = Linear(dataset.num_features, args.hidden)
self.lin2 = Linear(args.hidden, dataset.num_classes)
self.prop1 = APPNP(args.K, args.alpha)
self.dropout = args.dropout
def reset_parameters(self):
self.lin1.reset_parameters()
self.lin2.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.dropout(x, p=self.dropout, training=self.training)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin2(x)
x = self.prop1(x, edge_index)
return x
class MLP(torch.nn.Module):
def __init__(self, dataset,args):
super(MLP, self).__init__()
self.lin1 = Linear(dataset.num_features, args.hidden)
self.lin2 = Linear(args.hidden, dataset.num_classes)
self.dropout =args.dropout
def reset_parameters(self):
self.lin1.reset_parameters()
self.lin2.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.dropout(x, p=self.dropout, training=self.training)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.lin2(x)
return x