AddSelfLoops()(data) data = T.NormalizeFeatures()(data) ToUndirected。ToUndirected()操作通过为所有边增加一个反向边来实现无向化,这样一来,所有消息传播将会在两个方向都进行,但是在增加反向边的过程中,如果有必要,会生成新的边类型(应该指的是节点调换的过程,真正的边类型应该不会变); AddSelfLoops。
from torch_geometric.utilsimportadd_self_loops,degree # 定义GCN空域图卷积神经网络classGCNConv(MessagePassing,ABC):# 网络初始化 def__init__(self,in_channels,out_channels):""":param in_channels:节点属性向量的维度:param out_channels:经过图卷积之后,节点的特征表示维度""" # 定义伽马函数为求和函数,...
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, edg...
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)) # St...
5. add_self_loops:如果为False不再强制添加自环,默认为True。 6. normalize:默认为True,表示对邻接矩阵进行归一化。 7. bias:默认添加偏置。 于是模型搭建如下: 代码语言:javascript 复制 classGCN(torch.nn.Module):def__init__(self,num_node_features,num_classes):super(GCN,self).__init__()self.con...
super(GCN, self).__init__(aggr='add') self.lin = torch.nn.Linear(in_channels, out_channels) def forward(self, x, edge_index): edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) x = self.lin(x) row, col = edge_index ...
在这里,我们首先使用torch_geometric.utils.add_self_loops()函数(步骤 1)将自循环添加到边缘索引中,并通过调用torch.nn.Linear实例(步骤 2)线性转换节点特征。 归一化系数由每个节点的节点度数导出,每个节点的节点度数被转换为每个边。结果保存在形状的张量中(步骤 3) ...
(self, x, edge_index): # x: 节点特征矩阵,形状为 [num_nodes, in_channels] # edge_index: 边索引,形状为 [2, num_edges] edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) x = self.lin(x) row, col = edge_index deg = degree(col, x.size(0), dtype=x.dtype...
比如可以直接在MP的参数里传aggr='add',也可以直接自定义,一般情况下等价自定义如下: defaggregate(self,inputs:Tensor,index:Tensor,dim_size:Optional[int] = None)->Tensor:returnscatter(inputs, index, dim=self.node_dim, dim_size=dim_size, reduce=self.aggr) ...
[25, 10], num_hops=2, batch_size=1000, ---> 5 shuffle=True, add_self_loops=True 6 ) 7 /usr/local/lib/python3.6/dist-packages/torch_geometric/data/sampler.py in __init__(self, edge_index, sizes, node_idx, num_nodes, flow, **kwargs) 77 flow: str = "source_to_target", ...