Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
| 7 | from torch.nn.modules.module import Module |
| 8 | |
| 9 | class GraphConvolution(Module): |
| 10 | """ |
| 11 | Simple GCN layer, similar to https://arxiv.org/abs/1609.02907 |
| 12 | """ |
| 13 | |
| 14 | def __init__(self, in_features, out_features, bias=True): |
| 15 | super(GraphConvolution, self).__init__() |
| 16 | self.in_features = in_features |
| 17 | self.out_features = out_features |
| 18 | self.weight = Parameter(torch.FloatTensor(in_features, out_features)) |
| 19 | if bias: |
| 20 | self.bias = Parameter(torch.FloatTensor(out_features)) |
| 21 | else: |
| 22 | self.register_parameter('bias', None) |
| 23 | self.reset_parameters() |
| 24 | |
| 25 | def reset_parameters(self): |
| 26 | stdv = 1. / math.sqrt(self.weight.size(1)) |
| 27 | self.weight.data.uniform_(-stdv, stdv) |
| 28 | if self.bias is not None: |
| 29 | self.bias.data.uniform_(-stdv, stdv) |
| 30 | |
| 31 | def forward(self, input, adj): |
| 32 | support = torch.mm(input, self.weight) |
| 33 | output = torch.spmm(adj, support) |
| 34 | if self.bias is not None: |
| 35 | return output + self.bias |
| 36 | else: |
| 37 | return output |
| 38 | |
| 39 | def __repr__(self): |
| 40 | return self.__class__.__name__ + ' (' \ |
| 41 | + str(self.in_features) + ' -> ' \ |
| 42 | + str(self.out_features) + ')' |
| 43 | |
| 44 | |
| 45 | class GCN(nn.Module): |