问题描述

---------------------------------------------------------------------------RuntimeErrorTraceback (most recent call last)<ipython-input-111-5fc6204e7ba4> in <module> 16 for epoch in range(epochs): 17 optimizer.zero_grad()---> 18 pred = model(data) 1920 loss = loss_function(pred[data.train_mask], data.y[data.train_mask]) # 损失D:\Anaconda\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs) 1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1109 or _global_forward_hooks or _global_forward_pre_hooks):-> 1110 return forward_call(*input, **kwargs) 1111 # Do not call functions when jit is used 1112 full_backward_hooks, non_full_backward_hooks = [], []<ipython-input-110-6b56b2d44b83> in forward(self, data)9 x, edge_index = data.x, data.edge_index 10 ---> 11 x = self.conv1(x, edge_index) 12 x = F.relu(x) 13 x = F.dropout(x, training=self.training)D:\Anaconda\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs) 1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1109 or _global_forward_hooks or _global_forward_pre_hooks):-> 1110 return forward_call(*input, **kwargs) 1111 # Do not call functions when jit is used 1112 full_backward_hooks, non_full_backward_hooks = [], []<ipython-input-109-82363192a2f1> in forward(self, x, edge_index) 5657 def forward(self, x, edge_index):---> 58 return self.propagate(edge_index, x=x)D:\Anaconda\lib\site-packages\torch_geometric\nn\conv\message_passing.py in propagate(self, edge_index, size, **kwargs)389 aggr_kwargs = res[0] if isinstance(res, tuple) else res390 --> 391 out = self.aggregate(out, **aggr_kwargs)392 393 for hook in self._aggregate_forward_hooks.values():<ipython-input-109-82363192a2f1> in aggregate(self, x_j, edge_index) 4647 # 2.聚合邻居特征---> 48 aggr_out = scatter(x_j, row, dim=0, reduce='sum') # [num_nodes, feature_size] 4950 return aggr_outD:\Anaconda\lib\site-packages\torch_scatter\scatter.py in scatter(src, index, dim, out, dim_size, reduce)150 """151 if reduce == 'sum' or reduce == 'add':--> 152 return scatter_sum(src, index, dim, out, dim_size)153 if reduce == 'mul':154 return scatter_mul(src, index, dim, out, dim_size)D:\Anaconda\lib\site-packages\torch_scatter\scatter.py in scatter_sum(src, index, dim, out, dim_size)9 out: Optional[torch.Tensor] = None, 10 dim_size: Optional[int] = None) -> torch.Tensor:---> 11 index = broadcast(index, src, dim) 12 if out is None: 13 size = list(src.size())D:\Anaconda\lib\site-packages\torch_scatter\utils.py in broadcast(src, other, dim) 10 for _ in range(src.dim(), other.dim()): 11 src = src.unsqueeze(-1)---> 12 src = src.expand(other.size()) 13 return srcRuntimeError: The expanded size of the tensor (13264) must match the existing size (10556) at non-singleton dimension 0.Target sizes: [13264, 16].Tensor sizes: [10556, 1]

原因分析:

使用PyG搭建GCN网络时使用 scatter(x_j, row, dim=0, reduce='sum') 进行聚合邻居时出现问题,报错的原因也是也很简单,就是维度不匹配,因为输入的维度我们采用了自环维度变成了【13264】,而聚合的索引维度为【10556】。

解决方案:

解决的方法也很容易,只需要保证两个维度一致即可,这里我是搭建PyG任务出现的维度不一致,所以可能和小伙伴们遇到的情况不太相同,所以解决方法可能不一样,你要保证这个函数的输入维度一致即可。

def forward(self, x, edge_index):# 2.添加自环信息,考虑自身信息if self.add_self_loops:edge_index, _ = add_self_loops(edge_index, num_nodes=x.shape[0]) # [2, E]return self.propagate(edge_index, x=x)

对于其它方式出现这种报错的,你们要检查函数的输入维度进行进一步排错