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

Yuk*_*ang 2 python machine-learning neural-network deep-learning 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 个或更多输出的模型

Sha*_*hai 5

pytorch(和其他深度学习框架)的整个前提是标量损失函数梯度的反向传播。
在你的例子中,你有一个向量(dim=2)损失函数:

[cross_entropy_loss(output_1, target_1), cross_entropy_loss(output_2, target_2)]
Run Code Online (Sandbox Code Playgroud)

您需要决定如何将这两个损失合并为一个量损失。
例如:

weight = 0.5  # relative weight
loss = weight * cross_entropy_loss(output_1, target_1) + (1. - weight) * cross_entropy_loss(output_2, target_2)
# now loss is a scalar
loss.backward()
optimizer.step()
Run Code Online (Sandbox Code Playgroud)

  • 旁注:不要使用“lambda”作为变量名,因为它是 Python 保留关键字(它不起作用)。 (2认同)