-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcn.py
42 lines (38 loc) · 1.25 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
"""
This code was copied from the GCN implementation in DGL examples.
"""
import torch
import torch.nn as nn
from dgl.nn.pytorch import SGConv
from torch_geometric.nn import GCNConv
class GCN(nn.Module):
def __init__(self,
in_feats,
n_hidden,
n_classes,
n_layers,
activation,
dropout,
bias = True,
weight=True):
super(GCN, self).__init__()
self.layers = nn.ModuleList()
# input layer
self.bns = torch.nn.ModuleList()
self.layers.append(GCNConv(in_feats, n_hidden))
self.bns.append(torch.nn.BatchNorm1d(n_hidden, momentum = 0.01))
# hidden layers
for i in range(n_layers - 1):
self.layers.append(GCNConv(n_hidden, n_hidden))
# if i != (n_layers - 2):
self.bns.append(torch.nn.BatchNorm1d(n_hidden, momentum = 0.01))
# output layer
self.dropout = nn.Dropout(p=dropout)
def forward(self, features,edge_index):
h = features
for i, layer in enumerate(self.layers):
if i != 0:
h = self.dropout(h)
h = layer(h,edge_index)
# h = self.bns[i](h)
return h