PyG 自定义数据集报错 IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)

使用Pytorch-Geometric框架自定义数据集时,报错
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)

原因: 我这边的情况是N个节点,每个节点只有一个特征,Data(x, y, edge_index)中的 x 为 一维张量 时会报错。

解决方法: 改为二维张量

x = torch.from_numpy(features[i])
# x: tensor([ 0.7407, -0.1673,  0.1678,  0.1453, -0.2432, -0.7662,  0.1561])
# x.size(): torch.Size([7])
# 此时训练会报错  IndexError: 阿巴阿巴 but got -2

x = torch.from_numpy(features[i].reshape(-1, 1))
# x: tensor([[ 0.7407],
#            [-0.1673],
#            [ 0.1678],
#            [ 0.1453],
#            [-0.2432],
#            [-0.7662],
#            [ 0.1561]])
# x.size(): torch.Size([7,1])
# 正常训练


你可能感兴趣的:(深度学习,python,机器学习,pytorch)