我正在学习神经网络,特别是研究具有反向传播实现的 MLP。我正在尝试用 python 实现我自己的网络,我想在开始之前我会看看其他一些库。经过一番搜索后,我找到了 Neil Schemenauer 的 python 实现 bpnn.py。 ( http://arctrix.com/nas/python/bpnn.py )
在完成代码并阅读了 Christopher M. Bishops 书的第一部分“模式识别的神经网络”后,我在 backPropagate 函数中发现了一个问题:
# calculate error terms for output
output_deltas = [0.0] * self.no
for k in range(self.no):
error = targets[k]-self.ao[k]
output_deltas[k] = dsigmoid(self.ao[k]) * error
Bishops 书中计算误差的代码行不同。在第 145 页的等式 4.41 中,他将输出单位误差定义为:
d_k = y_k - t_k
其中 y_k 是输出,t_k 是目标。 (我用_代表下标) 所以我的问题是这行代码应该:
error = targets[k]-self.ao[k]
事实上:
error = self.ao[k] - targets[k]
我很可能完全错了,但请有人帮我解惑。谢谢
最佳答案
这完全取决于您使用的误差度量。举几个错误测量的例子(为简洁起见,我将使用 ys
表示 n
输出的向量,使用 ts
表示n
目标的向量):
mean squared error (MSE):
sum((y - t) ** 2 for (y, t) in zip(ys, ts)) / n
mean absolute error (MAE):
sum(abs(y - t) for (y, t) in zip(ys, ts)) / n
mean logistic error (MLE):
sum(-log(y) * t - log(1 - y) * (1 - t) for (y, t) in zip(ys, ts)) / n
您使用哪个完全取决于上下文。 MSE 和 MAE 可用于目标输出可以取任何值的情况,当目标输出为 0
或 1
以及 时,MLE 会给出非常好的结果y
在 open 范围内 (0, 1)
。
话虽如此,我还没有看到以前使用过的错误 y - t
或 t - y
(我自己在机器学习方面不是很有经验)。据我所知,您提供的源代码没有平方差或使用绝对值,您确定书上也没有吗?我对 y - t
或 t - y
的看法不是很好的错误度量,原因如下:
n = 2 # We only have two output neurons
ts = [ 0, 1 ] # Our target outputs
ys = [ 0.999, 0.001 ] # Our sigmoid outputs
# Notice that your outputs are the exact opposite of what you want them to be.
# Yet, if you use (y - t) or (t - y) to measure your error for each neuron and
# then sum up to get the total error of the network, you get 0.
t_minus_y = (0 - 0.999) + (1 - 0.001)
y_minus_t = (0.999 - 0) + (0.001 - 1)
编辑:根据 alfa's comment ,在书中,y - t
其实就是MSE的导数。在这种情况下,t - y
是不正确的。但是请注意,MSE 的实际导数是 2 * (y - t)/n
,而不仅仅是 y - t
。
如果您不除以 n
(因此您实际上得到的是平方和误差 (SSE),而不是均方误差),那么导数将为 2 * (y - t)
。此外,如果您使用 SSE/2
作为误差度量,则导数中的 1/2
和 2
抵消,您是留下 y - t
。
关于Python 神经网络反向传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16342591/