pytorch - can't find the inplace operation: one of the variables needed for gradient computation has been modified by an inplace operation
问题描述
I am trying to compute a loss on the jacobian of the network (i.e. to perform double backprop), and I get the following error: RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
I can't find the inplace operation in my code, so I don't know which line to fix.
*The error occurs in the last line: loss3.backward()
inputs_reg = Variable(data, requires_grad=True)
output_reg = self.model.forward(inputs_reg)
num_classes = output.size()[1]
jacobian_list = []
grad_output = torch.zeros(*output_reg.size())
if inputs_reg.is_cuda:
grad_output = grad_output.cuda()
jacobian_list = jacobian.cuda()
for i in range(10):
zero_gradients(inputs_reg)
grad_output.zero_()
grad_output[:, i] = 1
jacobian_list.append(torch.autograd.grad(outputs=output_reg,
inputs=inputs_reg,
grad_outputs=grad_output,
only_inputs=True,
retain_graph=True,
create_graph=True)[0])
jacobian = torch.stack(jacobian_list, dim=0)
loss3 = jacobian.norm()
loss3.backward()
解决方案
推荐阅读
- c# - 将条件映射为真或忽略应用
- java - 如何在空手道 dsl 中使用肥皂响应 (xml) 验证 xml 架构 (XSD)
- android - 远程配置值获取不一致
- scala - Spark SparkOutOfMemoryError 尽管每个任务的内存都大于输入
- angular - Angular 6 - 从下拉列表中默认选择的选项
- xamarin.forms - 网格 xamarin 形式中的重叠框架
- reactjs - 如何使用 laravel echo 和 react js?
- unity3d - Unity 网络在哪里?
- shell - Makefile forloop break 语句中的问题
- python - 即使使用 inplace=True,pandas replace 也不会替换值