python - 错误:只有整数标量数组可以转换为标量索引(初学者)
问题描述
我是使用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
解决方案
推荐阅读
- discord.py - 如何防止我的 discord.py Bot 禁止我?
- linux - 在循环中处理 bash 参数时如何使用分配语法?
- android - 从 Espresso 中的 CalendarView 中设置日期
- reactjs - 将 Kendo UI - RadialGauge 指针类型更改为自定义
- java - 将纹理图集与单个图像发送到 GPU 之间是否存在速度差异?
- amazon-web-services - AWS:指定多个子网时,RDS 会附加多个 IP 地址和 ENI 吗?
- java - 我已经编写了一个用于从数组中查找最小数字的代码,但它没有给出正确的输出
- kotlin - 聚合时是否将 kotlin 类型检查视为代码异味
- python - 子元素中的 Python 搜索
- python - 根据范围动态地将基本整数值增加到最大值?