zoukankan      html  css  js  c++  java
  • 【图卷积】【Graph Convolution】

    Graph Convolution

    基础版本

    github

    dataset

    wget https://data.deepai.org/Cora.zip

    import math
    
    import torch
    
    from torch.nn.parameter import Parameter
    from torch.nn.modules.module import Module
    
    
    class GraphConvolution(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):
            # input [in_features]
            # adj [num]
            support = torch.mm(input, self.weight)
            # support [out_feature]
            output = torch.spmm(adj, support)
            # output [out_feature]
            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) + ')'
    

    torch-geometric

    依赖

    pip install torch-scatter torch-sparse torch-cluster torch-spline-conv -f https://pytorch-geometric.com/whl/torch-${TORCH}.html
    
    pip install torch-scatter torch-sparse torch-cluster torch-spline-conv -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
    

    cuda

    版本选择

    conda install cudatoolkit=11.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64/
    
    conda install cudnn=7.4.1 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/
    
    
  • 相关阅读:
    【转】浅谈MVC与三层架构
    【转】小结登录的几种交互方式
    【转】 App架构设计经验谈:接口的设计
    【转】JS编码解码、C#编码解码
    jQuery 判断是否包含某个属性
    jQuery on()方法
    常用正则表达式大全
    Fiddler 抓取手机APP数据包
    [Asp.net]通过uploadify将文件上传到B服务器的共享文件夹中
    ★电车难题的n个坑爹变种
  • 原文地址:https://www.cnblogs.com/linzhenyu/p/14896937.html
Copyright © 2011-2022 走看看