[BPnet识别MNIST09]神经网络的调试

写在前面

今天我把这个程序跑了一遍,发现实在是找不出问题了,于是我就开始反思程序逻辑上的问题,之前我的神经网络只有2个神经元输入可能太少了,导致神经网络训练的效果本身就不理想,还有我今天有复查了一下公式,又发现了一个错误,等下放出来。

神经网络模型

我将神经网络改成了输入神经元是784个,也就是说图像上的每一个像素点都最为神经网络的输入,而且我把神经网络的2个隐层里的神经元都设置为了16个,图示如下图所示:

[BPnet识别MNIST09]神经网络的调试

公式更新:

[BPnet识别MNIST09]神经网络的调试
python程序实现:

n_num = 16		#第二个隐层神经元数目
m_num = 16		#第一个隐层神经元数目
total_x = 784	#输入层神经元个数

for i in range(0, n_num):
    gamma[i] = random.uniform(0, start_rand_max)
    theta_2[0][i] = random.uniform(0, start_rand_max)

# init para
for i in range(0, m_num):  # [1,4]
    theta_1[0][i] = random.uniform(0,start_rand_max)
    for k in range(0, total_x):
        w[k][i] = random.uniform(0, start_rand_max)

for i in range(0, m_num):
    for j in range(0, n_num):
        v[i][j] = random.uniform(0, start_rand_max)
        
# update dw,dv,dgama,dtheta_1,dtheta_2,dtheta_3
dtheta_3 = -1 * (y_out - y) * (y_out * (1 - y_out))
for i in range(0, n_num):
    dtheta_2[0][i] = -1 * (y_out - y) * (y_out * (1 - y_out)) * gamma[i] * \
                     (n[0][i] * (1 - n[0][i]))
    dgamma[i] = (y_out - y) * (y_out * (1 - y_out)) * n[0][i]
for i in range(0, m_num):
    for j in range(0, n_num):
        dv[i][j] =  (y_out - y) * (y_out * (1 - y_out)) * (n[0][j] * (1 - n[0][j])) * \
                  gamma[j] * m[0][i]
        dtheta_1[0][i] = dtheta_1[0][i] + (-1) * (y_out - y) * (y_out * (1 - y_out)) * gamma[j] * \
                        (n[0][j] * (1 - n[0][j])) * v[i][j] * (m[0][i] * (1 - m[0][i]))

for i in range(0, total_x):
    for j in range(0, m_num):
        for k in range(0, n_num):
            dw[i][j] = dw[i][j] + (y_out - y) * (y_out * (1 - y_out)) * gamma[k] * \
                      (n[0][k] * (1 - n[0][k])) * \
                       v[j][k] * (m[0][j] * (1 - m[0][j])) * x[i]

# update w,v,gama,theta_1,theta_2,theta_3
for i in range(0, m_num):
    theta_1[0][i] = theta_1[0][i] - study_step * dtheta_1[0][i]
    for j in range(0, total_x):
        w[j][i] = w[j][i] - study_step * dw[j][i]
for i in range(0, n_num):
    theta_2[0][i] = theta_2[0][i] - study_step * dtheta_2[0][i]
    gamma[i] = gamma[i] - study_step * dgamma[i]
# print(w)
for i in range(0, m_num):
    for j in range(0, n_num):
        v[i][j] = v[i][j] - study_step * dv[i][j]
theta_3 = theta_3 - study_step * dtheta_3
上一篇:如何实现一个私有变量,用 getName 方法可以访问,不能直接访问


下一篇:我的第一个开源项目 Kiwis2 Mockserver