繁体   English   中英

Keras model.predict错误的分类标签

[英]Keras model.predict error for categorical labels

我试图查看预测结果并使用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,  ...

它看起来像一个简单的解决方案,但我找不到。 谢谢您的帮助。

该模型需要两个数组,并且您要传递一个单独的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)形状为(number rows in test_text,number of outputs)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM