【问题标题】:Expected to see 1 array(s), but instead got the following list of 100 arrays预计会看到 1 个数组,但得到了以下 100 个数组的列表
【发布时间】:2019-01-06 13:52:17
【问题描述】:

我尝试使用神经网络创建字符级机器学习翻译。我对文本进行了预处理。 input_one_hot_encoded_list 包含输入单热编码句子,output_one_hot_encoded_list 包含我想要实现的另一种语言的单热编码句子。在这个例子中,我的字典有 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,...

【问题讨论】:

    标签: python tensorflow keras neural-network lstm


    【解决方案1】:

    不要将输入和输出作为一个 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)
    

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2020-05-20
      相关资源
      最近更新 更多