-
Notifications
You must be signed in to change notification settings - Fork 30
/
BWGNN.py
213 lines (190 loc) · 6.94 KB
/
BWGNN.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import dgl.function as fn
import math
import dgl
import sympy
import scipy
import numpy as np
from torch import nn
from torch.nn import init
from dgl.nn.pytorch import GraphConv, EdgeWeightNorm, ChebConv, GATConv, HeteroGraphConv
class PolyConv(nn.Module):
def __init__(self,
in_feats,
out_feats,
theta,
activation=F.leaky_relu,
lin=False,
bias=False):
super(PolyConv, self).__init__()
self._theta = theta
self._k = len(self._theta)
self._in_feats = in_feats
self._out_feats = out_feats
self.activation = activation
self.linear = nn.Linear(in_feats, out_feats, bias)
self.lin = lin
# self.reset_parameters()
# self.linear2 = nn.Linear(out_feats, out_feats, bias)
def reset_parameters(self):
if self.linear.weight is not None:
init.xavier_uniform_(self.linear.weight)
if self.linear.bias is not None:
init.zeros_(self.linear.bias)
def forward(self, graph, feat):
def unnLaplacian(feat, D_invsqrt, graph):
""" Operation Feat * D^-1/2 A D^-1/2 """
graph.ndata['h'] = feat * D_invsqrt
graph.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
return feat - graph.ndata.pop('h') * D_invsqrt
with graph.local_scope():
D_invsqrt = torch.pow(graph.in_degrees().float().clamp(
min=1), -0.5).unsqueeze(-1).to(feat.device)
h = self._theta[0]*feat
for k in range(1, self._k):
feat = unnLaplacian(feat, D_invsqrt, graph)
h += self._theta[k]*feat
if self.lin:
h = self.linear(h)
h = self.activation(h)
return h
class PolyConvBatch(nn.Module):
def __init__(self,
in_feats,
out_feats,
theta,
activation=F.leaky_relu,
lin=False,
bias=False):
super(PolyConvBatch, self).__init__()
self._theta = theta
self._k = len(self._theta)
self._in_feats = in_feats
self._out_feats = out_feats
self.activation = activation
def reset_parameters(self):
if self.linear.weight is not None:
init.xavier_uniform_(self.linear.weight)
if self.linear.bias is not None:
init.zeros_(self.linear.bias)
def forward(self, block, feat):
def unnLaplacian(feat, D_invsqrt, block):
""" Operation Feat * D^-1/2 A D^-1/2 """
block.srcdata['h'] = feat * D_invsqrt
block.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
return feat - block.srcdata.pop('h') * D_invsqrt
with block.local_scope():
D_invsqrt = torch.pow(block.out_degrees().float().clamp(
min=1), -0.5).unsqueeze(-1).to(feat.device)
h = self._theta[0]*feat
for k in range(1, self._k):
feat = unnLaplacian(feat, D_invsqrt, block)
h += self._theta[k]*feat
return h
def calculate_theta2(d):
thetas = []
x = sympy.symbols('x')
for i in range(d+1):
f = sympy.poly((x/2) ** i * (1 - x/2) ** (d-i) / (scipy.special.beta(i+1, d+1-i)))
coeff = f.all_coeffs()
inv_coeff = []
for i in range(d+1):
inv_coeff.append(float(coeff[d-i]))
thetas.append(inv_coeff)
return thetas
class BWGNN(nn.Module):
def __init__(self, in_feats, h_feats, num_classes, graph, d=2, batch=False):
super(BWGNN, self).__init__()
self.g = graph
self.thetas = calculate_theta2(d=d)
self.conv = []
for i in range(len(self.thetas)):
if not batch:
self.conv.append(PolyConv(h_feats, h_feats, self.thetas[i], lin=False))
else:
self.conv.append(PolyConvBatch(h_feats, h_feats, self.thetas[i], lin=False))
self.linear = nn.Linear(in_feats, h_feats)
self.linear2 = nn.Linear(h_feats, h_feats)
self.linear3 = nn.Linear(h_feats*len(self.conv), h_feats)
self.linear4 = nn.Linear(h_feats, num_classes)
self.act = nn.ReLU()
self.d = d
def forward(self, in_feat):
h = self.linear(in_feat)
h = self.act(h)
h = self.linear2(h)
h = self.act(h)
h_final = torch.zeros([len(in_feat), 0])
for conv in self.conv:
h0 = conv(self.g, h)
h_final = torch.cat([h_final, h0], -1)
# print(h_final.shape)
h = self.linear3(h_final)
h = self.act(h)
h = self.linear4(h)
return h
def testlarge(self, g, in_feat):
h = self.linear(in_feat)
h = self.act(h)
h = self.linear2(h)
h = self.act(h)
h_final = torch.zeros([len(in_feat), 0])
for conv in self.conv:
h0 = conv(g, h)
h_final = torch.cat([h_final, h0], -1)
# print(h_final.shape)
h = self.linear3(h_final)
h = self.act(h)
h = self.linear4(h)
return h
def batch(self, blocks, in_feat):
h = self.linear(in_feat)
h = self.act(h)
h = self.linear2(h)
h = self.act(h)
h_final = torch.zeros([len(in_feat),0])
for conv in self.conv:
h0 = conv(blocks[0], h)
h_final = torch.cat([h_final, h0], -1)
# print(h_final.shape)
h = self.linear3(h_final)
h = self.act(h)
h = self.linear4(h)
return h
# heterogeneous graph
class BWGNN_Hetero(nn.Module):
def __init__(self, in_feats, h_feats, num_classes, graph, d=2):
super(BWGNN_Hetero, self).__init__()
self.g = graph
self.thetas = calculate_theta2(d=d)
self.h_feats = h_feats
self.conv = [PolyConv(h_feats, h_feats, theta, lin=False) for theta in self.thetas]
self.linear = nn.Linear(in_feats, h_feats)
self.linear2 = nn.Linear(h_feats, h_feats)
self.linear3 = nn.Linear(h_feats*len(self.conv), h_feats)
self.linear4 = nn.Linear(h_feats, num_classes)
self.act = nn.LeakyReLU()
# print(self.thetas)
for param in self.parameters():
print(type(param), param.size())
def forward(self, in_feat):
h = self.linear(in_feat)
h = self.act(h)
h = self.linear2(h)
h = self.act(h)
h_all = []
for relation in self.g.canonical_etypes:
# print(relation)
h_final = torch.zeros([len(in_feat), 0])
for conv in self.conv:
h0 = conv(self.g[relation], h)
h_final = torch.cat([h_final, h0], -1)
# print(h_final.shape)
h = self.linear3(h_final)
h_all.append(h)
h_all = torch.stack(h_all).sum(0)
h_all = self.act(h_all)
h_all = self.linear4(h_all)
return h_all