反向传播是在分析计算梯度之后执行的,然后在训练时使用这些公式。神经网络本质上是一个多元函数,需要找到或训练函数的系数或参数。
相对于特定变量的梯度的定义是函数值的变化率。因此,正如您所提到的,从定义来看一阶导数 https://en.wikipedia.org/wiki/Derivative我们可以近似函数的梯度,包括神经网络。
要检查神经网络的分析梯度是否正确,最好使用数值方法进行检查。
For each weight layer w_l from all layers W = [w_0, w_1, ..., w_l, ..., w_k]
For i in 0 to number of rows in w_l
For j in 0 to number of columns in w_l
w_l_minus = w_l; # Copy all the weights
w_l_minus[i,j] = w_l_minus[i,j] - eps; # Change only this parameter
w_l_plus = w_l; # Copy all the weights
w_l_plus[i,j] = w_l_plus[i,j] + eps; # Change only this parameter
cost_minus = cost of neural net by replacing w_l by w_l_minus
cost_plus = cost of neural net by replacing w_l by w_l_plus
w_l_grad[i,j] = (cost_plus - cost_minus)/(2*eps)
此过程一次仅更改一个参数并计算数值梯度。在这种情况下我使用了(f(x+h) - f(x-h))/2h
,这似乎对我来说效果更好。
请注意,您提到:“因为数值梯度计算的结果不依赖于目标数据”,这是不正确的。就像当你找到cost_minus
and cost_plus
上面,成本是根据以下计算的
- 权重
- 目标班级
因此,反向传播的过程应该独立于梯度检查。在反向传播更新之前计算数值梯度。在一个时期内使用反向传播计算梯度(使用与上面类似的方法)。然后比较向量/矩阵的每个梯度分量并检查它们是否足够接近。