【问题标题】:Pytorch RuntimeError: Invalid index in gatherPytorch RuntimeError:聚集中的索引无效
【发布时间】:2019-06-20 04:57:21
【问题描述】:

我是 Pytorch 的新手,遇到这个错误:

x.gather(1, c)

RuntimeError:聚集时的索引无效 /pytorch/aten/src/TH/generic/THTensorEvenMoreMath.cpp:457

以下是有关张量的一些信息:

print(x.size())
print(c.size())
print(type(x))
print(type(c))

torch.Size([128, 2])
torch.Size([128, 1])
<class 'torch.Tensor'>
<class 'torch.Tensor'>

x 用浮点值填充,c 用整数填充,这可能是问题吗?

【问题讨论】:

    标签: python deep-learning pytorch


    【解决方案1】:

    这仅仅意味着您的索引张量 c 具有无效索引。 例如,以下索引张量是有效的:

            x = torch.tensor([
            [5, 9, 1],
            [3, 2, 8],
            [7, 4, 0]
        ])
        c = torch.tensor([
            [0, 0, 0],
            [1, 2, 0],
            [2, 2, 1]
        ])
        x.gather(1, c)
    >>>tensor([[5, 5, 5],
            [2, 8, 3],
            [0, 0, 4]])
    

    但是,以下索引张量无效:

    c = torch.tensor([
        [0, 0, 0],
        [1, 2, 0],
        [2, 2, 3]
    ])
    

    它给出了你提到的例外

    RuntimeError:聚集中的索引无效

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2019-03-16
      • 2013-08-07
      • 2011-03-24
      • 1970-01-01
      • 2018-05-08
      • 2020-08-04
      • 2021-01-14
      相关资源
      最近更新 更多