火炬7的反向传播如何?

时间:2015-09-15 02:40:51

标签: backpropagation torch

我试图通过火炬教程了解监督学习。

http://code.madbits.com/wiki/doku.php?id=tutorial_supervised

反向传播:

http://home.agh.edu.pl/~vlsi/AI/backp_t_en/backprop.html

据我所知,此火炬教程中的参数更新位于步骤4培训程序

output = model:forward(inputs[i])
df_do = criterion:backward(output, targets[i])
model:backward(inputs[i], df_do)

例如,我得到了这个

output = -2.2799
         -2.3638
         -2.3183
         -2.1955
         -2.3377
         -2.3434
         -2.3740
         -2.2641
         -2.3449
         -2.2214
         [torch.DoubleTensor of size 10]

targets[i] = 9

df_do是这个吗?

0
0
0
0
0
0
0
0
-1
0
[torch.DoubleTensor of size 10]

我知道目标是9,在这个例子中输出是4,所以结果是错误的,给出了df_do的第9个元素" -1"。

但为什么?

根据http://home.agh.edu.pl/~vlsi/AI/backp_t_en/backprop.html

df_do是[目标(期望输出) - 输出]。

1 个答案:

答案 0 :(得分:4)

在Torch中,backprop与数学中的完全一样。 df_do损失的衍生物w.r.t.预测,因此完全由您的loss function定义,即nn.Criterion。 最着名的是均方误差(nn.MSECriterion): enter image description here

请注意,MSE标准要求目标具有与预测相同的大小(用于分类的单热矢量)。如果选择MSE,则衍生向量df_do将计算为:

enter image description here

然而,MSE标准通常不太适合分类。更合适的一个是似然准则,它将probability vector作为预测,将真实类的标量索引作为目标。目的是简单地最大化真实类别的概率,等于其负面的最小化:

enter image description here

如果我们给它对数概率向量预测(它是单调变换,因此不会影响优化结果但计算稳定性更高),我们将得到负对数似然丢失函数({ {1}}):

enter image description here

在这种情况下,nn.ClassNLLCriterion如下:

enter image description here

在火炬教程中NLL criterion is used by default