• 图卷积神经网络层的pytorch复现



    基本概念:

    图结构非常常见,属于非欧式空间,例如社交网络图、知识图谱、用户点击购买产品产生的关系图、分子结构图、人体关节点连接图。图卷积神经网络算法是一种根据图卷积和神经网络的理论,应用于广泛存在的图结构的实体的算法。图卷积来源于二维卷积,神经网络算法相当于在传统机器学习算法上加上可以学习的权重,使用梯度下降算法更新权重。总的来说,图卷积神经网络是一种结合信号处理和神经网络应用于图结构的一种新算法。具体应用可以对节点、边和整个图进行分类、分割、检测等应用。本文主要记录学习图卷积神经网络的一些理论和想法。

    图卷积层的数学描述:

    图卷积层经过很多的优化和迭代,目前比较主流的一种方法是每一层的复杂度更低,而通过堆叠多层进行更深层次的学习的方法进行学习。具体的推导过程在文献1中,这里省略大篇幅的推导过程。

    多层的图卷积网络按照下面的逐层递推规则:
    A ~ = A + I N D ~ i i = ∑ j A ~ i j \widetilde{A} = A+I_N \\ \widetilde{D}_{ii} = \sum_j{\widetilde{A}_{ij}} A =A+IND ii=jA ij

    H ( l + 1 ) = σ ( D ~ − 1 2 A ~ D ~ − 1 2 H ( l ) W ( l ) ) H^{(l+1)} = \sigma(\widetilde{D}^{-\frac{1}{2}}\widetilde{A}\widetilde{D}^{-\frac{1}{2}}H^{(l)}W^{(l)}) H(l+1)=σ(D 21A D 21H(l)W(l))

    式子中的 A A A指的是图的邻接矩阵形式, W ( l ) W^{(l)} W(l)指的是可学习的权重, H H H是图节点的最初的特征矩阵 H 0 H^{0} H0经过每一层变换后的矩阵, σ ( ) \sigma() σ()指的是激活函数。邻接矩阵和拉普拉斯矩阵可以参考2

    图的总体架构:

    图的总体架构如下所示,本篇文章需要实现的就是里面的hidden layers,GraphConvolutionLayer不改变图的结构,所以图结构进过图卷积神经网络层后仍然保持原来的结构。但是后面层的节点能够聚合前面层的节点信息。类似于卷积神经网络的“视野”的概念。深层得到更多的语义信息,浅层则保留更多的原始特征信息。

    在这里插入图片描述

    图卷积层pytorch代码实现3和注释:
    # -*- coding: utf-8 -*-
    # # @Use     : Paper reproduction
    # # @Time    : 2022/8/11 21:30
    # # @FileName: GraphConvolutionLayer.py
    # # @Software: PyCharm
    # # @Paper   : Spectral Networks and Locally Connected Networks on Graphs
    
    
    import torch
    import torch.nn as nn
    
    
    class GraphConvolutionLayer(nn.Module):
        """
        图卷积神经网络
        """
    
        def __init__(self, input_dim, output_dim, adjacency_matrix=None, use_bias=True):
            super(GraphConvolutionLayer, self).__init__()
    
            self.input_dim = input_dim
            self.output_dim = output_dim
            self.use_bias = use_bias
            data = torch.tensor(input_dim, output_dim)
            self.weight = nn.Parameter(data=data)
            if self.use_bias:
                self.bias = nn.Parameter(torch.tensor(input_dim, output_dim))
            else:
                self.register_parameter('bias', None)
            self.reset_parameters()
            self.L_matrix = self.calculate_L_matrix(adjacency_matrix)
    
        def reset_parameters(self):
            """
            重置权重
            """
            nn.init.kaiming_normal_(self.weight)
            if self.use_bias:
                nn.init.zeros_(self.bias)
    
        def forward(self, input_feature):
            """
            邻接矩阵是稀疏矩阵,使用稀疏矩阵的乘法
            @param input_feature:输入特征
            """
            # 计算图卷积的输出
            # (\widetilde{D}^{-\frac{1}{2}}\widetilde{A}\widetilde{D}^{-\frac{1}{2}}H^{(l)}W^{(l)})
            suport = torch.mm(input_feature, self.weight)
            output = torch.sparse.mm(self.L_matrix, suport)  # 注意因为邻接矩阵是稀疏矩阵,所以使用稀疏矩阵乘法提高效率
            if self.use_bias:
                output += self.bias
            return output
    
        @staticmethod
        def calculate_L_matrix(adjcency: torch.Tensor) -> torch.Tensor:
            """
            根据图的邻接矩阵计算矩阵L_matrix
            L_matrix = \widetilde{D}^{-\frac{1}{2}}\widetilde{A}\widetilde{D}^{-\frac{1}{2}}
            """
            dim = adjcency.shape[0]
            A_ware = adjcency + torch.eye(dim)  # 生成单位矩阵
            D_ii = torch.flatten(torch.sum(A_ware, dim=0))  # 按照列进行求和,并且展平成一维向量
            D_ware = torch.diag_embed(D_ii)  # 转换成对角矩阵
            D_ware_temp = torch.pow(D_ware, -0.5)  # 求对角阵的-1/2指数
            L_matrix = torch.mm(torch.mm(D_ware_temp, A_ware), D_ware_temp)  # 使用广播机制进行矩阵乘法
            return L_matrix
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    参考:

    其他参考文献和代码:

    Spectral Networks and Locally Connected Networks on Graphs

    Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering

    mdeff/cnn_graph: Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering (github.com)

    lutional Neural Networks on Graphs with Fast Localized Spectral Filtering (github.com)](https://github.com/mdeff/cnn_graph)


    1. SEMI-SUPERVISED CLASSIFICATION WITH GRAPH CONVOLUTIONAL NETWORKS ↩︎

    2. 图的拉普拉斯矩阵_KPer_Yang的博客-CSDN博客 ↩︎

    3. tkipf/gcn: Implementation of Graph Convolutional Networks in TensorFlow (github.com) ↩︎

  • 相关阅读:
    java毕业设计艾灸减肥管理网站Mybatis+系统+数据库+调试部署
    Vue 打包优化之 externals 抽离公共的第三方库
    java中锁的面试题
    IOS面试题object-c 51-60
    概念解析 | 揭秘视觉与语言交叉模型:CLIP和BLIP的介绍
    CUDA说明和安装[window]
    多任务爬虫(多线程和多进程)
    Inter FPGA配置管理SDM(Secure Device Manager)与配置理解
    在报表开发工具Stimulsoft Report数据透视表的新功能介绍
    多维度分片需求,如何解决查询问题?
  • 原文地址:https://blog.csdn.net/KPer_Yang/article/details/126326770