小编Yuk*_*ang的帖子

在pytorch中,如何训练具有两个或多个输出的模型?

output_1, output_2 = model(x)
loss = cross_entropy_loss(output_1, target_1)
loss.backward()
optimizer.step()

loss = cross_entropy_loss(output_2, target_2)
loss.backward()
optimizer.step()
Run Code Online (Sandbox Code Playgroud)

但是,当我运行这段代码时,出现以下错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 4]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
Run Code Online (Sandbox Code Playgroud)

然后,我真的想知道我应该做什么来训练具有 2 个或更多输出的模型

python machine-learning neural-network deep-learning pytorch

2
推荐指数
1
解决办法
761
查看次数