毕业检查,平均验证?

数据挖掘 机器学习 优化 梯度下降
2022-02-02 23:43:47

我正在运行Gradient Checking,以发现我的数学计算梯度和实际采样梯度之间的任何差异 - 以确保我的反向传播正确实施。

在计算这样的差异时,我可以总结差异的平方,然后取它们的平均值吗?然后我可以使用这个平均值作为我对网络计算梯度的正确程度的估计:

1mi=0i=m(gini)2

甚至:

i=0i=m(gini)2

其中g是来自反向传播的梯度,n是来自梯度检查的梯度。


然而,Andrew Ng 建议:

||(gn)||2||g||2+||n||2

其中$\vert \vert 。\vert \vert _2$是向量的长度。


另一篇帖子也推荐了一种略有不同的方法:https ://stats.stackexchange.com/a/188724/187816


为什么他们的方法会比我的更好?

1个回答

让我举一个例子,安德鲁的建议比你的更好:

假设真正的梯度是 $(0, 0, 0)$,而您计算的梯度是 $(10^{-4}, 10^{-4}, 10^{-4})$。那么你的平均值将返回 $10^{-8}$,而 Andrew 的推荐将返回 $1$。您的指标可能会欺骗您认为您的梯度是正确计算的,并且错误只是由于数字问题,而 Andrew 不能欺骗您,因为它认为梯度可能非常小。(0,0,0) and the gradient you have computed is (104,104,104). Then your average would return 108, and Andrew's recommendation would return 1. Your metric could fool you into thinking that your gradient is computed propperly and the error is just due to a numeric issue, while Andrew's cannot fool you into that, due to the fact that it considers the fact that the gradient can be very small.

总结一下,如果你的梯度没有接近于零的范数,那也没关系。然而,当梯度接近于零时,你可能会误以为你的梯度是正确的,而实际上却不是。