首页 > 其他分享 >Encounter the RuntimeError: one of the variables needed for gradient computation has been modified b

Encounter the RuntimeError: one of the variables needed for gradient computation has been modified b

时间:2022-10-14 15:59:11浏览次数:43  
标签:rand gradient RuntimeError torch modified computation operation copy variables

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  

标签:rand,gradient,RuntimeError,torch,modified,computation,operation,copy,variables
From: https://www.cnblogs.com/dongdongjia/p/16791800.html

相关文章