简体   繁体   English

Keras model.predict错误的分类标签

[英]Keras model.predict error for categorical labels

I am trying to see prediction results and print them with model.predict function but I am having an error: 我试图查看预测结果并使用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,....

I have multiple input, both are embedded. 我有多个输入,都是嵌入式的。 This code was previously working when I have one input as embedded. 当我将一个输入嵌入时,此代码以前有效。

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 and test_posts are the result of pad_sequences. test_text和test_posts是pad_sequences的结果。 They are in array, test_text has shape of 100 and test_posts has shape of 1. labels_name is names of the labels. 它们在数组中,test_text的形状为100,test_posts的形状为1。labels_name是标签的名称。 I am having error in the second line; 我在第二行中有错误;

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

Error: 错误:

/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,  ...

It looks like a simple solution but I couldn't find it. 它看起来像一个简单的解决方案,但我找不到。 Thanks for the help. 谢谢您的帮助。

The model expects two arrays and you are passing one single numpy array. 该模型需要两个数组,并且您要传递一个单独的numpy数组。

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

remove calling the numpy.array method and you the Error will go away. 删除调用numpy.array方法,您错误就会消失。

Update: 更新:
There is no need to use for loop . 无需使用for loop

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

This can do what you want. 这可以做您想要的。 prediction_result is now have shape of (number rows in test_text,number of outputs) 现在的(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