<filename>initial_improve_1/net/layers.py
# coding:utf-8
"""
Time: 2021/9/8 22:32
Author: eightyninth
File: layers.py
"""
import math
import numpy as np
import torch
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module

class gcn_layer(Module):
    def __init__(self, input_feat, output_feat, bias=True):
        super(gcn_layer, self).__init__()
        self.input_feat = input_feat
        self.output_feat = output_feat
        self.weights = Parameter(torch.FloatTensor(input_feat, output_feat))
        if bias:
            self.bias = Parameter(torch.FloatTensor(output_feat))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weights.size(1))
        self.weights.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, x, adj):
        """

        :param x: input, feature of nodes
        :param adj: input, adj = \^{D}^{-1/2}\^{A}\^{D}^{-1/2}, \^{A}=A+I
        :return:
        """
        sup = torch.mm(x, self.weights)
        y = torch.mm(adj, sup)
        if self.bias is not None:
            return y + self.bias
        else:
            return y
