首页 > 解决方案 > 错误:只有整数标量数组可以转换为标量索引(初学者)

问题描述

我是使用python的初学者,我很困惑为什么我不断收到错误“只有标量数组可以转换为标量网络”

Hidden_​​layer_network 类:

def __init__(self, W_list, b_list, phi, phi_derivative):
    
    #Store weights and nonlinearities
    W_list = np.array([2,3,4]) 
    b_list = np.array([5,6,7]) 
    
    self.W_list = W_list
    self.b_list = b_list
    self.phi = phi
    self.phi_derivative = phi_derivative
    
    #Initial node values
    self.h_list = np.zeros(W_list[0])
    self.a_list = np.zeros(W_list[0])
    self.z = np.zeros(W_list[0])

    
def forward_pass(self, x):
    for i in range(self.z):
        if i == 0:
            self.h_list[i] = self.W_list[i].dot(x)
        else:
            self.h_list[i] = self.W_list[i].dot(self.h_list[i-1])
        if i == (self.z-1):
            self.a_list[i] = softmax (self.h_list)
            self.out = self.a_list[i]
        else:
            self.a_list[i] = self.phi(self.h_list[i])
    
  

当我尝试进行前向传递时,这里是代码

n_hidden = 120
n_pixels = 784
n_out = 10
sigma_1 = 1 / np.sqrt(n_hidden + n_pixels)
sigma_2 = 1 / np.sqrt(n_out + n_hidden)
W_list = np.random.normal(0, sigma_1, (n_hidden, n_pixels))
b_list = np.zeros(n_hidden)
W_output = np.random.normal(0, sigma_2, (n_out, n_hidden))
b_output = np.zeros(n_out)

def tanh(z):

  return np.tanh(z)

def tanh_derivative(z):

  return 1 - np.tanh(z) ** 2

network = Hidden_layer_network(W_list=W_list, b_list=b_list,
                               phi=tanh, phi_derivative=tanh_derivative)

和:

network.forward_pass(x)

然后它返回错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-373-7af2bac02cb9> in <module>
----> 1 network.forward_pass(x)

<ipython-input-368-d753003446a1> in forward_pass(self, x)
 19 
 20     def forward_pass(self, x):
---> 21         for i in range(self.z):
 22             if i == 0:
 23                 self.h_list[i] = self.W_list[i].dot(x)

TypeError: only integer scalar arrays can be converted to a scalar index

标签: pythonneural-networkrecurrent-neural-network

解决方案


推荐阅读