RuntimeError:叶变量已移至图形内部

问题描述 投票:1回答:1

我尝试使用pytorch进行自动渐变。当我正在测试时,我遇到了错误。我的代码如下:

w11 = torch.rand((100,2), requires_grad=True)
w12 = torch.rand((100,2), requires_grad=True)
w12[:,1] = w12[:,1] + 1
w13 = torch.rand((100,2), requires_grad=True)
w13[:,1] = w13[:,1] + 2
out1=(w11-w12)**2
out2=out1.mean()
out2.backward(retain_graph=True)
pytorch
1个回答
0
投票

当您想用require_grad = True替换张量中的某些内容时,使用with torch.no_grad(),>

w11 = torch.rand((100,2), requires_grad=True)
w12 = torch.rand((100,2), requires_grad=True)
w13 = torch.rand((100,2), requires_grad=True)
with torch.no_grad():
  w12[:,1] = w12[:,1] + 1
  w13[:,1] = w13[:,1] + 2
out1=(w11-w12)**2
out2=out1.mean()
out2.backward(retain_graph=True)

一切都会顺利。

© www.soinside.com 2019 - 2024. All rights reserved.