【问题标题】:Runtime Error after using index_add function in pytorch在 pytorch 中使用 index_add 函数后出现运行时错误
【发布时间】:2021-11-08 04:06:23
【问题描述】:

我有一个无法解决的问题...我阅读了 Pytorch 文档以摆脱这个问题,但我的代码看起来不错,但发生了错误。

        indices = ...
        X = ...
        Y = ...

        print(torch.min(indices)) # tensor(0, device='cuda:0')
        print(torch.max(indices)) # tensor(30, device='cuda:0')
        print(indices.dtype) # torch.int64
        print(indices.shape) # torch.Size([498])

        print(X.shape) # torch.Size([498, 2048])
        print(X.dtype) # torch.float32

        print(Y.shape) # torch.Size([31, 2048])
        print(Y.dtype) # torch.float32

        Y = Y.index_add(0, indices, X)

index_add 的调用导致:

RuntimeError: number of dims don't match in permute

我做错了什么?提前谢谢你。

编辑: 来自文档:

张量的第dim维度必须与长度相同 索引(必须是向量),并且所有其他维度必须匹配 self,否则将引发错误。

在这种情况下dim=0 tensor=X self=Y 并且遵循X.shape[0] 应该等于len(indices) 并且X.shape[1] 应该等于Y.shape[1],就是这样......它可能是一个pytorch错误吗?

编辑2: 该错误仅针对特定的张量值并使用:

torch.use_deterministic_algorithms(True)
torch.manual_seed(1234)

所以应该是pytorch的bug。

【问题讨论】:

  • 你能提供一个完全可重现的错误代码吗?使用种子和特定张量。

标签: python pytorch runtime-error


【解决方案1】:

你可能正在尝试做

X = X.index_add(0, indices, Y)

你交换了XY

【讨论】:

  • 谢谢你的回答,但是不,我想使用索引将X添加到Y,我参考pytorch.org/docs/stable/generated/torch.Tensor.index_add_.html中的示例
  • 据我了解,我需要为 X 的每一行创建一个索引,用于将该行添加到索引指定的 Y 行。 X 有 498 行,我有 0 到 30 之间的 498 个索引(实际上 Y 有 31 行)。我错了吗?在我提到的示例中,它似乎以这种方式工作
  • 奇怪,我只是用一个最小的例子模拟了你的东西,它可以工作。 pastebin.com/4Unt5mka(此代码可以正常工作)。看看我是否正确复制。
  • 是的,你做得对,并在我的代码中用随机张量替换我的张量它也可以工作......使用我的特定张量的值它会引发异常
猜你喜欢
  • 2021-06-04
  • 2022-01-25
  • 1970-01-01
  • 2022-09-26
  • 2020-10-05
  • 2018-02-02
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
相关资源
最近更新 更多