最近,尝试使用Pytorch和Pytorch Geometric(PyG),构建图神经网络架构,根据论文《The Emerging Field of Signal Processing on Graphs》中的算法,用PyG重现实验,出现错误及程序源代码如下,请求指导,谢谢!(已解决)
RuntimeError Traceback (most recent call last)
<ipython-input-14-14adeb5271eb> in <module>
2 best_val_acc = test_acc = 0
3 for epoch in range(1, 201):
----> 4 train()
5 train_acc, val_acc, tmp_test_acc = test()
6 if val_acc > best_val_acc:
<ipython-input-12-f9f6ce4bd81a> in train()
5 optimizer.zero_grad()
6 # 将误差反向传播
----> 7 F.nll_loss(model()[data.train_mask], data.y[data.train_mask]).backward()
8 # 更新参数
9 optimizer.step()
D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
--> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)
<ipython-input-10-a5f12f83ff5c> in forward(self)
10 x, edge_index = data.x, data.edge_index
11
---> 12 x = self.conv1(x, edge_index)
13 x = F.relu(x)
14 x = F.dropout(x, training=self.training)
D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
--> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)
<ipython-input-9-e745cda6afb6> in forward(self, x, edge_index)
33
34 # Step 3-5: Start propagating messages.
---> 35 return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x)
36
37 def message(self, x_j, edge_index, size):
D:\Anaconda3\lib\site-packages\torch_geom

本文分享了使用Pytorch和PytorchGeometric构建图神经网络的实战经验,针对实验过程中遇到的错误进行了详细分析,并提供了修改后的代码实现,强调了自定义tensor移至GPU的重要性。
最低0.47元/天 解锁文章





