简体   繁体   English

具有多个输出的转发功能?

[英]Forward function with multiple outputs?

Typically the forward function in nn.module of pytorch computes and returns predictions for inputs happening in the forward pass.通常,pytorch 的 nn.module 中的 forward 函数计算并返回对前向传递中发生的输入的预测。 Sometimes though, intermediate computations might be useful to return.但有时,中间计算可能对返回有用。 For example, for an encoder, one might need to return both the encoding and reconstruction in the forward pass to be used later in the loss.例如,对于编码器,可能需要在前向传递中返回编码和重构,以便稍后在损失中使用。

Question: Can Pytorch's nn.Module's forward function, return multiple outputs?问: Pytorch 的 nn.Module 的forward 函数,能否返回多个输出? Eg a tuple of outputs consisting predictions and intermediate values?例如,包含预测和中间值的输出元组?
Does such a return value not mess up the backward propagation or autograd?这样的返回值不会弄乱反向传播或 autograd 吗? If it does, how would you handle cases where multiple functions of input are incorporated in the loss function?如果是这样,您将如何处理将多个输入函数合并到损失函数中的情况?

(The question should be valid in tensorflow too.) (这个问题在张量流中也应该是有效的。)

"The question should be valid in Tensorflow too", but PyTorch and Tensorflow are different frameworks. “这个问题在 Tensorflow 中也应该有效”,但是 PyTorch 和 Tensorflow 是不同的框架。 I can answer for PyTorch at least.我至少可以回答 PyTorch。

Yes you can return a tuple containing any final and or intermediate result.是的,您可以返回一个包含任何最终和/或中间结果的元组。 And this does not mess up back propagation since the graph is saved implicitly from the tensors outputs using callbacks and cached tensors.这不会弄乱反向传播,因为图形是使用回调和缓存的张量从张量输出中隐式保存的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM