我正在使用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超出了大小为3的轴0的边界
答案 0 :(得分:0)
确定要写:
for j in range(w[l].shape[l]):
不是
for j in range(w[l].shape[1]):
希望我能帮上忙!
答案 1 :(得分:0)
替换
for j in range(w[l].shape[l])
与
for j in range(w[l].shape[0])
因为您要分配node_in = h
,所以h是h = np.zeros((w[l].shape[0],))
,所以如果您要进行for i in range(w[l].shape[l])
,则node_in
和w[l].shape[l]
的大小可能不匹配,并且会导致索引错误。