gcn.py 1003 Bytes
Newer Older
1
2
3
4
5
6
7
8
"""
This code was copied from the GCN implementation in DGL examples.
"""
import tensorflow as tf
from tensorflow.keras import layers

from dgl.nn.tensorflow import GraphConv

9

10
class GCN(layers.Layer):
11
12
13
    def __init__(
        self, g, in_feats, n_hidden, n_classes, n_layers, activation, dropout
    ):
14
15
        super(GCN, self).__init__()
        self.g = g
16
        self.layers = []
17
18
19
20
        # 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)
            )
24
25
26
27
28
29
30
31
32
33
34
        # output layer
        self.layers.append(GraphConv(n_hidden, n_classes))
        self.dropout = layers.Dropout(dropout)

    def call(self, features):
        h = features
        for i, layer in enumerate(self.layers):
            if i != 0:
                h = self.dropout(h)
            h = layer(self.g, h)
        return h