dongdongjia

Encounter the RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

Encounter the RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

这句话含义是:梯度计算所需的变量之一在后向传播时已被操作修改。

原因:对变量的赋值是就地操作,比如:

a = torch.rand(2,5,5,requires_grad=True) 
b = torch.rand(2,5,5)
a[:] = a/b  #这一步,使a值重新改变,后向传播时无法更新前向计算的梯度。

正确写法是先复制a_copy=a,再对a_copy进行操作。

a = torch.rand(2,5,5,requires_grad=True) 
b = torch.rand(2,5,5)
a_copy = a
a_copy[:] = a/b  

posted on 2022-10-14 15:45  咚咚咚咚嘉  阅读(61)  评论(0编辑  收藏  举报

导航