为什么在尝试强制转换np.array(some_list)ValueError时出现错误:无法广播输入数组 [英] Why do I get error trying to cast np.array(some_list) ValueError: could not broadcast input array

查看:170
本文介绍了为什么在尝试强制转换np.array(some_list)ValueError时出现错误:无法广播输入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有不同形状的np.arrays的np.array.我需要它,因为在下一部分中,我将用一个具有相同形状的delta_array总结这个大的np.array. 像矩阵求和 self.weights += delta_weights 我也将分别处理其中的每个数组.或者例如,我需要将所有元素的明智数组乘以某个数字.

I'm trying to create np.array of np.arrays of different shape. I need it because in the next part I will sum up this large np.array with some delta_array that has the same shape. Like matrices sum self.weights += delta_weights And I will be working with each array inside it separately as well. Or for example I will need to multiply element wise all arrays by some number.

无法弄清楚出了什么问题:(请帮帮我. 代码创建不同形状的随机np.array的列表. 所以这是我的代码:

Can't figure out what's wrong :( Please, help me. Code creates list of random np.arrays of different shape. So here is my code:

weights = []
for i in range(1, len(self.layers)):
    weights.append(np.random.rand(self.layers[i-1] + 1, self.layers[i]))
print(type(weights))
print([(type(w), w.shape) for w in weights])
#error here with layers = [2,2,1] or [3,3,1] etc
self.weights = np.array(weights)

输出: 对于self.layers = [2,2,1]

Output: For self.layers=[2, 2, 1]

<class 'list'>
[(<class 'numpy.ndarray'>, (3, 2)), (<class 'numpy.ndarray'>, (3, 1))]
Traceback (most recent call last):
line 20, in <module>
 run()
line 8, in run
 net.init_weights()
line 71, in init_weights
 self.weights = np.array(weights)
ValueError: could not broadcast input array from shape (3,2) into shape (3)

对于[2、3、1],一切都很好:

For [2, 3, 1] everything is okay:

<class 'list'>
[(<class 'numpy.ndarray'>, (3, 3)), (<class 'numpy.ndarray'>, (4, 1))]

对于[3,3,1]与[2,2,1]相同的故事-错误

For [3, 3, 1] same story as for [2, 2, 1] - error

对于[3,7,1]或[3,2,1],一切都很好.

For [3, 7, 1] or [3, 2, 1] everything is okay.

//这是用于机器学习的梯度下降权重矩阵.

//It's matrices of weights for gradient descent for machine learning.

推荐答案

好,您正在尝试创建矩阵数组,以便以后可以使用数组操作(如+=).

Ok, you're trying to create an array of matrices, so that you can use array operations (like +=) later on.

您可以声明一个ndarray来容纳另一个ndarray,然后用矩阵(它们只是二维ndarray)填充它:

You can declare an ndarray to hold another ndarray, and then fill it with the matrices (which are simply two dimensional ndarrays):

weights = np.empty(len(self.layers), dtype=np.ndarray)
for i in range(1, len(self.layers)):
    weights[i] = np.random.rand(self.layers[i-1] + 1, self.layers[i])

请记住,这绝对不是三维数组:它纯粹是二维数组的数组.

Keep in mind that this is definitely not a three dimensional array: it's purely an array of two dimensional arrays.

您的代码适用于[2, 3, 1]的原因是,一维数为1的数组被广播;据我了解,这纯粹是偶然的,不是您想要的.

The reason your code works for [2, 3, 1] is that the array with one dimension of 1 gets broadcasted; that's purely accidental, and not what you want, as far as I understand.

这篇关于为什么在尝试强制转换np.array(some_list)ValueError时出现错误:无法广播输入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆