Fid*_*dem 5 python backpropagation neural-network
我正在学习神经网络,特别是通过反向传播实现来查看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
Run Code Online (Sandbox Code Playgroud)
计算错误的代码行在Bishops书中是不同的.在页145,方程4.41他将输出单位错误定义为:
d_k = y_k - t_k
Run Code Online (Sandbox Code Playgroud)
其中y_k是输出,t_k是目标.(我使用_来表示下标)所以我的问题是这行代码:
error = targets[k]-self.ao[k]
Run Code Online (Sandbox Code Playgroud)
事实上:
error = self.ao[k] - targets[k]
Run Code Online (Sandbox Code Playgroud)
我很可能完全错了,但有人可以帮我解决我的困惑.谢谢
这完全取决于您使用的误差测量。举几个错误度量的例子(为了简洁起见,我将使用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
Run Code Online (Sandbox Code Playgroud)
您使用哪一种完全取决于上下文。当目标输出可以取任何值时,可以使用 MSE 和 MAE,而当目标输出为 或0
以及1
处于y
开放范围时,MLE 会给出非常好的结果(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)
Run Code Online (Sandbox Code Playgroud)
编辑: Per alfa在书中的评论y - t
实际上是MSE的导数。在这种情况下,t - y
是不正确的。但请注意,MSE 的实际导数是2 * (y - t) / n
,而不是简单的y - t
。
如果不除以n
(因此实际上有平方和误差 (SSE),而不是均方误差),则导数将为2 * (y - t)
。此外,如果您使用SSE / 2
作为误差度量,则导数中的1 / 2
和2
会抵消,剩下y - t
。