预计将看到1个数组,但获得了以下100个数组的列表

时间:2019-01-06 13:52:18

标签: python tensorflow keras neural-network lstm

我尝试使用神经网络创建字符级机器学习翻译。我对文本进行了预处理。 input_one_hot_encoded_list包含输入的一语编码的句子,output_one_hot_encoded_list包含我想要实现的另一种语言的一语编码的句子。在此示例中,我的字典有55个字符,我有100个句子,因此两个数组都由100个列表组成,其中包含50个列表(最长的句子有50个字符),其中包含55个整数(一种热编码,每个列表包含五十四个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,...

1 个答案:

答案 0 :(得分:0)

不是将输入和输出作为numpy数组的列表传递,这使Keras认为您具有多个输入和输出层,而是将它们作为一个单独的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)