如何在 torch 中编写 updateGradInput 和 accGradParameters?

How to write the updateGradInput and accGradParameters in torch?

我知道这两个函数是做torch的后向传播的,界面如下 updateGradInput(input, gradOutput) accGradParameters(input, gradOutput, scale) 我对 gradInputgradOutput 在图层中的真正含义感到困惑。 假设网络的成本是 C 和一层 LL 层的 gradInputgradOutput 是指 d_C/d_input_Ld_C/d_output_L 吗?

如果是,如何根据gradOutput计算gradInput

另外,accGradParameters是不是要累积d_C/d_Weight_Ld_C/d_bias_L?如果是,如何计算这些值?

Do gradInput and gradOutput of layer L mean d_C/d_input_L and d_C/d_output_L

是:

  • gradInput =成本w.r.t层输入的导数,
  • gradOutput = 成本 w.r.t 层输出的导数。

how to compute gradInput according to gradOutput

调整来自 The building blocks of Deep Learning 的架构(警告:在此架构中,成本表示为 L = Loss,层 f) 我们有:

有关在 LogSoftMax 层上进行此类计算的具体分步示例,您可以参考

does accGradParameters mean to accumulate d_C/d_Weight_L and d_C/d_bias_L

是的。在 torch/nn.

中命名为 gradWeightgradBias

how to compute these values?

同上。还是用上面博客的一个公式post:

除了 jacobian 的维数不同(有关详细信息,请参阅博客 post)。例如,对于线性层,这将转换为:

这是层的输入与 gradOutput 之间的外积。在手电筒中 we have:

self.gradWeight:addr(scale, gradOutput, input)

并且:

gradOutput。在手电筒 we have:

self.gradBias:add(scale, gradOutput)

在这两种情况下,scale 是在实践中用作学习率的比例因子。