简单记录一下这个问题的解决。我需要使用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的博客-优快云博客
发现主要的问题就是:
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失格的博客-优快云博客
个人感觉确实比torch.gather好用多了! 例如这里:
torch.gather() 和torch.sactter_()的用法简析_Teeyohuang的博客-优快云博客
我实在觉得看起来有点晕。 总的来说还是感觉对有一些tensor操作不是很熟悉。具体的操作列表可以参考这里:Pytorch中Tensor的各种操作 - 知乎
就简单总结这么多。