我尝试为神经网络实现在线反向传播算法。
计算出每个节点的每个输出和净值(即不应用激活函数的值)之后,存储在变量" out"和"网",我也尝试计算delta值(每个节点的感应场)和每个节点的权重变化; delta值存储在名为" delta的变量中,是否存在变量存储在" delta_weights"变量,实际上是一个三维矩阵:这个矩阵的第一个索引是我工作的级别,第二个代表网络中的一个节点,第三个是进入该节点的连接之一(synapsis) 。
在第一时刻,这两个变量包含全零。
然后我写了这段代码:
for j in range(self.l):
# self.l is the number of levels in the network: I'm running through all the levels
if j == 0:
# last level: output units
for i in range(self.p[-1]):
# self.p[j] is the number of units in level j: I'm considering every unit in the specific level fixed by the previous for cicle
ei = np.subtract(target[i], outs[-1][i])
# error of unit i in the last level
deltai = ei * self.der_actfunct(nets[-1][i])
delta[-1][i] = deltai
for h in range(self.p[-2]):
delta_weights[-1][i][h] = eta * deltai * outs[-2][h]
else:
# other levels
# going back until first level
for i in range(self.p[-j-1]):
temp = 0
for k in range(self.p[-j]):
temp += delta[-j][k] * self.network[-j][k].weights[i]
# calculating induced field of hidden nodes
deltai = temp * self.der_actfunct(nets[-j-1][i])
delta[-j-1][i] = deltai
if j == self.l-1:
for h in range(self.n):
delta_weights[0][i][h] = eta * deltai * variable[h]
else:
for h in range(self.p[-j-2]):
delta_weights[-j-1][i][h] = eta * deltai * outs[-j-2][h]
# UPDATE WEIGHTS
for j in range(self.l):
for i in range(self.p[j]):
self.network[j][i].set_weights(np.add(self.network[j][i].weights, delta_weights[j][i]), 0)
return
我的问题是,训练错误实际上在超过10个时期之后会增加。我确定这是一个错误,所以我的代码怎么了?
(我使用的是eta,学习率为0.01)