"basicsr/vscode:/vscode.git/clone" did not exist on "76b9024b282206200ee82b441dbe08c2d9299f2f"
gcn.py 1.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
"""GCN using DGL nn package

References:
- Semi-Supervised Classification with Graph Convolutional Networks
- Paper: https://arxiv.org/abs/1609.02907
- Code: https://github.com/tkipf/gcn
"""
import tensorflow as tf
from tensorflow.keras import layers
10

11
12
from dgl.nn.tensorflow import GraphConv

13

14
class GCN(tf.keras.Model):
15
16
17
    def __init__(
        self, g, in_feats, n_hidden, n_classes, n_layers, activation, dropout
    ):
18
19
20
21
        super(GCN, self).__init__()
        self.g = g
        self.layer_list = []
        # input layer
22
23
24
        self.layer_list.append(
            GraphConv(in_feats, n_hidden, activation=activation)
        )
25
26
        # hidden layers
        for i in range(n_layers - 1):
27
28
29
            self.layer_list.append(
                GraphConv(n_hidden, n_hidden, activation=activation)
            )
30
31
32
33
34
35
36
37
38
39
40
        # output layer
        self.layer_list.append(GraphConv(n_hidden, n_classes))
        self.dropout = layers.Dropout(dropout)

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