Keras模型。预测类别标签的错误

时间:2018-06-28 14:49:06

标签: python numpy keras label prediction

我试图查看预测结果并使用model.predict函数打印它们,但出现错误:

ValueError: Error when checking model : the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[array([   0,....

我有多个输入,都是嵌入式的。当我将一个输入嵌入时,此代码以前有效。

for i in range(100):
    prediction_result = model.predict(np.array([test_text[i], test_posts[i]]))
    predicted_label = labels_name[np.argmax(prediction_result)]
    print(text_data.iloc[i][:100], "")
    print('Actual label:' + tags_test.iloc[i])
    print("Predicted label: " + predicted_label + "\n")

test_text和test_posts是pad_sequences的结果。它们在数组中,test_text的形状为100,test_posts的形状为1。labels_name是标签的名称。我在第二行中有错误;

prediction_result = model.predict(np.array([test_text[i], test_posts[i]]))

错误:

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
   1815         x = _standardize_input_data(x, self._feed_input_names,
   1816                                     self._feed_input_shapes,
-> 1817                                     check_batch_axis=False)
   1818         if self.stateful:
   1819             if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
     84                 'Expected to see ' + str(len(names)) + ' array(s), '
     85                 'but instead got the following list of ' +
---> 86                 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
     87         elif len(names) > 1:
     88             raise ValueError(

ValueError: Error when checking model : the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[array([   0,    0,  ...

这似乎是一个简单的解决方案,但我找不到。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

该模型需要两个数组,并且您要传递一个numpy数组。

 prediction_result = model.predict([test_text.values[i].reshape(-1,100), test_posts.values[i].reshape(-1,1)])

删除调用numpy.array方法,您的错误将消失。

更新:
无需使用for loop

prediction_result = model.predict([test_text.values.reshape(-1,100), test_posts.values.reshape(-1,1)])

这可以做您想要的。预测结果现在的形状为(number rows in test_text,number of outputs)