gcn.py 986 Bytes
Newer Older
Zhengwei's avatar
Zhengwei committed
1
2
3
4
5
"""
This code was copied from the GCN implementation in DGL examples.
"""
import torch
import torch.nn as nn
6

Zhengwei's avatar
Zhengwei committed
7
8
from dgl.nn.pytorch import GraphConv

9

Zhengwei's avatar
Zhengwei committed
10
class GCN(nn.Module):
11
12
13
    def __init__(
        self, g, in_feats, n_hidden, n_classes, n_layers, activation, dropout
    ):
Zhengwei's avatar
Zhengwei committed
14
15
16
17
18
19
20
        super(GCN, self).__init__()
        self.g = g
        self.layers = nn.ModuleList()
        # input layer
        self.layers.append(GraphConv(in_feats, n_hidden, activation=activation))
        # hidden layers
        for i in range(n_layers - 1):
21
22
23
            self.layers.append(
                GraphConv(n_hidden, n_hidden, activation=activation)
            )
Zhengwei's avatar
Zhengwei committed
24
25
26
27
28
29
30
31
32
        # output layer
        self.layers.append(GraphConv(n_hidden, n_classes))
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, features):
        h = features
        for i, layer in enumerate(self.layers):
            if i != 0:
                h = self.dropout(h)
33
            h = layer(self.g, h)
Zhengwei's avatar
Zhengwei committed
34
        return h