[英]ValueError: all input arrays must have the same shape
I am trying to execute a code but got this Value error. 我正在尝试执行代码,但出现此Value错误。 Source.pkl file contains 100 elements as shown in array.png attached with this. Source.pkl文件包含100个元素,如随附的array.png中所示。
As shown in figure at index 0 there are 16 numpy arrays all with shape(1,59) so, first element has shape (16,1,59). 如图中索引0所示,共有16个numpy数组,它们的形状均为(1,59),因此第一个元素的形状为(16,1,59)。 At index 1 there are 11 numpy arrays all with shape(1,59) so, now shape of second element is (11,1,59) and so on for other elements also. 在索引1处,有11个numpy数组,它们的形状均为(1,59),因此,第二个元素的形状为(11,1,59),对于其他元素,依此类推。
How can I have all the arrays with the same shape? 如何使所有阵列具有相同的形状?
import numpy as np
from keras.layers import Input, LSTM, Dense
from keras.models import Model
pickle_in = open("source.pkl","rb")
predictions = np.load(pickle_in)[0:100]
in_predictions = np.stack(predictions)
pickle_in.close()
Here's two choices: 这有两个选择:
Append zero arrays to every list, until all of the lists have same size. 将零数组追加到每个列表,直到所有列表的大小都相同。 In your case, assuming 16 is the largest size among all of the lists, you have to append (1, 59) shaped arrays to every list until the size of it is 16. 在您的情况下,假设16个是所有列表中最大的大小,则必须向每个列表追加(1,59)形状的数组,直到其大小为16。
But, it's a waste of space. 但是,这是浪费空间。 Another choice is: 另一个选择是:
Concatenate your lists to a large one, and then convert the large one into numpy array. 将您的列表连接成一个大列表 ,然后将大列表转换为numpy数组。 Use another list to record which array was originally in which list. 使用另一个列表来记录最初在哪个列表中的数组。 Here is a sample code, but I have not tested it: 这是一个示例代码,但我尚未对其进行测试:
pickle_in = open("source.pkl","rb")
predictions = np.load(pickle_in)[0:100]
wrap_list = []
index_map = []
for i in range(len(predictions)):
pred = predictions[i]
wrap_list += pred
index_map += [i for _ in range(len(pred))]
in_predictions = np.array(wrap_list)
pickle_in.close()
I hope this could help you. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.