Ошибка Keras model.predict для категориальных меток - PullRequest
0 голосов
/ 28 июня 2018

Я пытаюсь увидеть результаты прогноза и распечатать их с помощью функции 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_sequence. Они находятся в массиве, test_text имеет форму 100, а test_posts имеет форму 1. label_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 голосов
/ 28 июня 2018

Модель ожидает два массива, и вы передаете один единственный массив 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)

...