PyTorch使用index_add_报:one of the variables needed for gradient computation has been modified

简单记录一下这个问题的解决。我需要使用tensor A的一些行来替换tensor B的一些行(通过一个表示索引的tensor来指定位置),发现可以使用:torch.index_select和torch.index_add来实现。具体等会来介绍一下,先说说怎么解决这个报错。发现PyTorch的文档主要介绍的是:torch.Tensor.index_add_:

torch.Tensor.index_add_ — PyTorch 1.11.0 documentation

torch.index_add — PyTorch 1.11.0 documentation

所以我就用了第一个,类似于这样:

updated_des = torch.index_select(out_h, 0, des)
h=h.index_add_(0, des, updated_des)

结果就报了这样的错:

untimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [12779, 64]], which is output 0 of IndexAddBackward, is at version 40; expected version 39 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

参考了一下其他网友是怎么解决的:报错解决:one of the variables needed for gradient computation has been modified by an inplace operation_y4ung的博客-CSDN博客

发现主要的问题就是:

inplace operation 就是直接对tensor的内容进行修改,而没有使用复制的副本 (An in-place operation is an operation that changes directly the content of a given Tensor without making a copy)。

所以我把第二行代码改了一下:

h=torch.index_add(h, 0, des, updated_des)

就可以解决上面的报错了。关于这两个function,可以参考这里的介绍:

【Pytorch】tensor可以按行、列筛选的大利器——torch.index_select()_Programmar失格的博客-CSDN博客

个人感觉确实比torch.gather好用多了! 例如这里:

torch.gather() 和torch.sactter_()的用法简析_Teeyohuang的博客-CSDN博客

我实在觉得看起来有点晕。 总的来说还是感觉对有一些tensor操作不是很熟悉。具体的操作列表可以参考这里:Pytorch中Tensor的各种操作 - 知乎

就简单总结这么多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值