我正在这里浏览PyTorch Geometric文档:https://pytorch-geometric.readthedocs.io/en/latest/notes/create_gnn.html
他们解释了一个“基本”代码:
import torch
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(GCNConv, self).__init__(aggr='add') # "Add" aggregation.
self.lin = torch.nn.Linear(in_channels, out_channels)
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 2: Linearly transform node feature matrix.
x = self.lin(x)
# Step 3: Compute normalization
row, col = edge_index
deg = degree(row, x.size(0), dtype=x.dtype)
deg_inv_sqrt = deg.pow(-0.5)
norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
# Step 4-6: Start propagating messages.
return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x,
norm=norm)
def message(self, x_j, norm):
# x_j has shape [E, out_channels]
# Step 4: Normalize node features.
return norm.view(-1, 1) * x_j
def update(self, aggr_out):
# aggr_out has shape [N, out_channels]
# Step 6: Return new node embeddings.
return aggr_out
说明包含以下段落:
“在message()函数中,我们需要归一化相邻节点特征x_j。在这里,x_j表示映射张量,其中包含每个边的相邻节点特征。可以通过附加_i或_j来自动映射节点特征实际上,任何张量都可以通过这种方式映射,只要它们的第一维中有?个条目即可。”
问题:我知道此映射张量x_j是一个张量,其中包含每个边缘的相邻节点特征。他们如何计算x_j?这是否意味着如果将x的名称更改为x_i,我将自动具有“映射张量”?