-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcn.py
164 lines (135 loc) · 5.68 KB
/
gcn.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import tqdm
import dgl
import dgl.function as fn
from dgl.utils import expand_as_pair
from utils.functions import create_activation
class GCN(nn.Module):
def __init__(self,
in_dim,
num_hidden,
out_dim,
num_layers,
dropout,
activation,
residual,
norm,
encoding=False
):
super(GCN, self).__init__()
self.num_hidden = num_hidden
self.out_dim = out_dim
self.num_layers = num_layers
self.gcn_layers = nn.ModuleList()
self.activation = activation
self.dropout = dropout
last_activation = create_activation(activation) if encoding else None
last_residual = encoding and residual
last_norm = norm if encoding else None
if num_layers == 1:
self.gcn_layers.append(GraphConv(
in_dim, out_dim, residual=last_residual, norm=last_norm, activation=last_activation))
else:
self.gcn_layers.append(GraphConv(
in_dim, num_hidden, residual=residual, norm=norm, activation=create_activation(activation)))
for l in range(1, num_layers - 1):
self.gcn_layers.append(GraphConv(
num_hidden, num_hidden, residual=residual, norm=norm, activation=create_activation(activation)))
self.gcn_layers.append(GraphConv(
num_hidden, out_dim, residual=last_residual, activation=last_activation, norm=last_norm))
self.norms = None
self.head = nn.Identity()
def forward(self, g, inputs, return_hidden=False):
h = inputs
hidden_list = []
for l in range(self.num_layers):
h = F.dropout(h, p=self.dropout, training=self.training)
h = self.gcn_layers[l](g, h)
if self.norms is not None and l != self.num_layers - 1:
h = self.norms[l](h)
hidden_list.append(h)
if self.norms is not None and len(self.norms) == self.num_layers:
h = self.norms[-1](h)
if return_hidden:
return self.head(h), hidden_list
else:
return self.head(h)
def inference(self, g, device="cuda", batch_size=128):
feat = g.ndata['feat']
sampler = dgl.dataloading.MultiLayerFullNeighborSampler(1, prefetch_node_feats=['feat'])
all_nid = torch.arange(g.num_nodes()).to(g.device)
dataloader = dgl.dataloading.DataLoader(g, all_nid, sampler, batch_size=batch_size, shuffle=False, drop_last=False, num_workers=0)
buffer_device = torch.device('cpu')
pin_memory = (buffer_device != device)
for l, layer in enumerate(self.gcn_layers):
y = torch.empty(
g.num_nodes(), self.num_hidden if l != self.num_layers - 1 else self.out_dim,
device=buffer_device, pin_memory=pin_memory)
feat = feat.to(device)
for input_nodes, output_nodes, blocks in tqdm.tqdm(dataloader):
x = feat[input_nodes]
graph = blocks[0].to(device)
h = layer(graph, x)
if self.norms is not None and l != self.num_layers - 1:
h = self.norms[l](h)
y[output_nodes[0]:output_nodes[-1]+1] = h.to(buffer_device)
feat = y
return y
def reset_classifier(self, num_classes):
self.head = nn.Linear(self.out_dim, num_classes)
class GraphConv(nn.Module):
def __init__(self,
in_dim,
out_dim,
norm=None,
activation=None,
residual=True,
):
super().__init__()
self._in_feats = in_dim
self._out_feats = out_dim
self.fc = nn.Linear(in_dim, out_dim)
if residual:
if self._in_feats != self._out_feats:
self.res_fc = nn.Linear(
self._in_feats, self._out_feats, bias=False)
print("! Linear Residual !")
else:
print("Identity Residual ")
self.res_fc = nn.Identity()
else:
self.register_buffer('res_fc', None)
self.norm = norm
if norm is not None:
self.norm = norm(out_dim)
self._activation = activation
self.reset_parameters()
def reset_parameters(self):
self.fc.reset_parameters()
def forward(self, graph, feat):
with graph.local_scope():
aggregate_fn = fn.copy_src('h', 'm')
feat_src, feat_dst = expand_as_pair(feat, graph)
degs = graph.out_degrees().float().clamp(min=1)
norm = torch.pow(degs, -0.5)
shp = norm.shape + (1,) * (feat_src.dim() - 1)
norm = torch.reshape(norm, shp)
feat_src = feat_src * norm
graph.srcdata['h'] = feat_src
graph.update_all(aggregate_fn, fn.sum(msg='m', out='h'))
rst = graph.dstdata['h']
rst = self.fc(rst)
degs = graph.in_degrees().float().clamp(min=1)
norm = torch.pow(degs, -0.5)
shp = norm.shape + (1,) * (feat_dst.dim() - 1)
norm = torch.reshape(norm, shp)
rst = rst * norm
if self.res_fc is not None:
rst = rst + self.res_fc(feat_dst)
if self.norm is not None:
rst = self.norm(rst)
if self._activation is not None:
rst = self._activation(rst)
return rst