简体   繁体   中英

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:

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. They are in array, test_text has shape of 100 and test_posts has shape of 1. labels_name is names of the labels. 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.

 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.

Update:
There is no need to use 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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