From 1becba5f4d06b6e92170c6092e6c69ed2d90056c Mon Sep 17 00:00:00 2001 From: WangGodder <32242917+WangGodder@users.noreply.github.com> Date: Mon, 9 Dec 2019 12:03:27 +0800 Subject: [PATCH] update GCN --- torchcmh/models/.gitignore | 1 - torchcmh/models/GCN.py | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 torchcmh/models/GCN.py diff --git a/torchcmh/models/.gitignore b/torchcmh/models/.gitignore index 44a978d..e658dee 100644 --- a/torchcmh/models/.gitignore +++ b/torchcmh/models/.gitignore @@ -2,7 +2,6 @@ capsnet.py resnet_capsnet.py CompactBilinearPooling.py -GCN.py GCNText.py GCMH QDCMH diff --git a/torchcmh/models/GCN.py b/torchcmh/models/GCN.py new file mode 100644 index 0000000..58b8844 --- /dev/null +++ b/torchcmh/models/GCN.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# @Time : 2019/8/28 +# @Author : Godder +# @Github : https://github.com/WangGodder +import math +import torch +from torch.nn.parameter import Parameter +from torch import nn + + +class GraphConvolution(nn.Module): + """ + Simple GCN layer, similar to https://arxiv.org/abs/1609.02907 + """ + + def __init__(self, in_features, out_features, bias=True): + super(GraphConvolution, self).__init__() + self.in_features = in_features + self.out_features = out_features + self.weight = Parameter(torch.FloatTensor(in_features, out_features)) + if bias: + self.bias = Parameter(torch.FloatTensor(out_features)) + else: + self.register_parameter('bias', None) + self.reset_parameters() + + def reset_parameters(self): + stdv = 1. / math.sqrt(self.weight.size(1)) + self.weight.data.uniform_(-stdv, stdv) + if self.bias is not None: + self.bias.data.uniform_(-stdv, stdv) + + def forward(self, input, adj): + support = torch.mm(input, self.weight) + output = torch.spmm(adj, support) + if self.bias is not None: + return output + self.bias + else: + return output + + def __repr__(self): + return self.__class__.__name__ + ' (' \ + + str(self.in_features) + ' -> ' \ + + str(self.out_features) + ')' + +