【问题标题】:Pytorch geometric - Is the function of agg='add' equal to matmul(adjacency_matrix, feature_matrix)?Pytorch几何 - agg ='add'的函数是否等于matmul(adjacency_matrix,feature_matrix)?
【发布时间】:2022-07-29 14:23:29
【问题描述】:

我对以下代码有疑问。特别是在 (1) 中指定:

__init__(aggr='add') 

(2) 中,我没有 (1) 中的 aggr='add',而是邻接矩阵 (edge_index) 和节点之间的乘法(x_j):

matmul(edge_index, x_j)

说它们相同并产生相同的结果是否正确?

代码(1)

import torch
from torch.nn import Linear, Parameter
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import add_self_loops, degree

class GCNConv(MessagePassing):
    def __init__(self, in_channels, out_channels):
        super().__init__(aggr='add') 
        self.reset_parameters()

    def reset_parameters(self):
        self.lin.reset_parameters()
        

    def forward(self, x, edge_index):
        # x has shape [N, in_channels]
        # edge_index has shape [2, E]

        # Step 1: Add self-loops to the adjacency matrix.
        edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))

        # Step 4-5: Start propagating messages.
        out = self.propagate(edge_index, x=x)

        return out

    def message(self, x_j):
        return x_j

代码(2)

import torch
from torch.nn import Linear, Parameter
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import add_self_loops, degree

class GCNConv(MessagePassing):
    def __init__(self, in_channels, out_channels):
        super().__init__() 
        self.reset_parameters()

    def reset_parameters(self):
        self.lin.reset_parameters()


    def forward(self, x, edge_index):
        # x has shape [N, in_channels]
        # edge_index has shape [2, E]

        edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))

        out = self.propagate(edge_index, x=x)

        return out

    def message_and_aggregate(self, edge_index, x_j):
        return matmul(edge_index, x_j)

【问题讨论】:

    标签: python pytorch pytorch-geometric graph-neural-network


    【解决方案1】:

    如你所描述的(其实你的代码有一些错误,代码可能无法成功运行),代码1只是添加了邻居节点和自身的特征(有自循环)来得到更新后的特征,代码2是将特征(包括自己的特征)乘以邻接矩阵,也就是将邻居节点的特征相加(矩阵对应的位置为1)。因此,两者本质上没有区别。

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多