Python 期望看到1个数组,但得到了以下100个数组的列表

Python 期望看到1个数组,但得到了以下100个数组的列表,python,tensorflow,keras,neural-network,lstm,Python,Tensorflow,Keras,Neural Network,Lstm,我尝试使用神经网络创建字符级机器学习翻译。我对文本进行了预处理input\u one\u hot\u encoded\u list包含input one hot编码的句子,output\u one\u hot\u encoded\u list包含我想要实现的另一种语言的一个hot编码的句子。在这个例子中,我的字典有55个字符,我有100个句子,所以两个数组都由100个列表组成,其中包含50个列表(最长的句子有50个字符),其中包含55个整数(一个热编码,每个列表包含54个0和1)。你知道为什么它

我尝试使用神经网络创建字符级机器学习翻译。我对文本进行了预处理
input\u one\u hot\u encoded\u list
包含input one hot编码的句子,
output\u one\u hot\u encoded\u list
包含我想要实现的另一种语言的一个hot编码的句子。在这个例子中,我的字典有55个字符,我有100个句子,所以两个数组都由100个列表组成,其中包含50个列表(最长的句子有50个字符),其中包含55个整数(一个热编码,每个列表包含54个0和1)。你知道为什么它不起作用吗?错误显示在底部

print('shape of input_one_hot_encoded_list: ' + str(array(input_one_hot_encoded_list).shape))

print('shape of output_one_hot_encoded_list: ' + str(array(output_one_hot_encoded_list).shape))

shape = array(input_one_hot_encoded_list).shape

model = Sequential()
model.add(LSTM(len(dict), return_sequences=True, stateful=True,
               batch_input_shape=shape))
model.add(LSTM(len(dict), return_sequences=True, stateful=True))
model.add(LSTM(len(dict), return_sequences=True, stateful=True))
model.add(Dense(len(dict), activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])


print(model.summary())

model.fit(input_one_hot_encoded_list, output_one_hot_encoded_list, epochs=20)
运行上述代码的输出:

shape of input_one_hot_encoded_list: (100, 50, 55)
shape of output_one_hot_encoded_list: (100, 50, 55)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (100, 50, 55)             24420     
_________________________________________________________________
lstm_2 (LSTM)                (100, 50, 55)             24420     
_________________________________________________________________
lstm_3 (LSTM)                (100, 50, 55)             24420     
_________________________________________________________________
dense_1 (Dense)              (100, 50, 55)             3080      
=================================================================
Total params: 76,340
Trainable params: 76,340
Non-trainable params: 0
_________________________________________________________________
None
Traceback (most recent call last):
File "data_preparation.py", line 175, in <module>
model.fit(input_one_hot_encoded_list, output_one_hot_encoded_list, epochs=20)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 952, in fit
batch_size=batch_size)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 751, in _standardize_user_data
exception_prefix='input')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training_utils.py", line 102, in standardize_input_data
str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 100 arrays: [array([[1, 0, 0, ..., 0, 0, 0],
   [0, 1, 0, ..., 0, 0, 0],
   [0, 0, 1, ..., 0, 0, 0],
   ...,
   [1, 0, 0, ..., 0, 0, 0],
   [1, 0, 0, ..., 0, 0, 0],
   [1, 0, 0, ..., 0, 0,...
input\u one\u hot\u encoded\u列表的形状:(100,50,55)
输出\一个\热\编码\列表的形状:(100、50、55)
_________________________________________________________________
层(类型)输出形状参数
=================================================================
lstm_1(lstm)(100、50、55)24420
_________________________________________________________________
lstm_2(lstm)(100、50、55)24420
_________________________________________________________________
lstm_3(lstm)(100、50、55)24420
_________________________________________________________________
密集型_1(密集型)(100,50,55)3080
=================================================================
总参数:76340
可培训参数:76340
不可训练参数:0
_________________________________________________________________
没有一个
回溯(最近一次呼叫最后一次):
文件“data_preparation.py”,第175行,在
model.fit(输入\u one\u hot\u编码的\u列表,输出\u one\u hot\u编码的\u列表,epochs=20)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py”,第952行,适合
批次大小=批次大小)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/keras/engine/training.py”,第751行,在用户数据中
异常(前缀为“输入”)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/keras/engine/training\u utils.py”,第102行,标准化输入数据
str(len(data))+'数组:'+str(data)[:200]+'…')
ValueError:检查模型输入时出错:传递给模型的Numpy数组列表的大小不是模型预期的大小。期望看到1个数组,但得到了以下100个数组的列表:[array([[1,0,0,…,0,0],
[0, 1, 0, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
...,
[1, 0, 0, ..., 0, 0, 0],
[1, 0, 0, ..., 0, 0, 0],
[1, 0, 0, ..., 0, 0,...

不要将输入和输出作为numpy数组列表传递,这会让Keras认为您有多个LPLE输入和输出层,而是将它们作为一个numpy数组传递:

import numpy as np

input_one_hot_encoded = np.array(input_one_hot_encoded_list)
output_one_hot_encoded = np.array(output_one_hot_encoded_list)

model.fit(input_one_hot_encoded, output_one_hot_encoded, epochs=20)