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