我在ipython脚本下使用python3.7在我的anaconda中尝试使用神经网络。
我不熟悉,仍然在学习python的问题,不知道如何调试它。
import numpy as np
w1 = np.array([[11, 11, 9, 11, 7,13, 14, 6, 6, 12], [11, 11, 9, 11, 7,13, 14, 6, 6, 12], [11, 11, 9, 11, 7,13, 14, 6, 6, 12]])
w2 = np.zeros ((1,10))
b1 = np.array([0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8])
b2 = np.array([0.2])
def f(x):
return 1 / (1 + np.exp(-x))
def simple_looped_nn_calc(n_layers, x,w,b):
for l in range(n_layers-1):
if l == 0:
node_in = x
else:
node_in = h
h = np.zeros((w[l].shape[0],))
for i in range(w[l].shape[0]):
f_sum = 0
for j in range(w[l].shape[l]):
f_sum += w[l][i][j]* node_in[j]
f_sum += b[l][i]
h[i] = f(f_sum)
return h
w = [w1, w2]
b = [b1, b2]
x = [280, 0, 280, 280, 0, 0, 0, 0, 280, 0, 0, 0, 0, 0, 0, 0, 0, 280, 0]
当我运行我的代码时,我得到错误,simple_looped_nn_calc(3,x,w,b),如下所示:
IndexError:索引3超出轴0的大小为3的范围
你确定要写:
for j in range(w[l].shape[l]):
并不是
for j in range(w[l].shape[1]):
希望我帮忙!
更换
for j in range(w[l].shape[l])
同
for j in range(w[l].shape[0])
因为你正在分配node_in = h
,这里是h = np.zeros((w[l].shape[0],))
,所以如果你要做for i in range(w[l].shape[l])
那么node_in
和w[l].shape[l]
的大小可能不匹配并且会导致索引错误。