# How can I get prediction for only one instance in Keras?

When I request Keras to apply prediction with a fitted model to a new dataset without label like this:

model1.predict_classes(X_test)


it works fine. But when I try to make prediction for only one row, it fails:

model1.predict_classes(X_test[10])

Exception: Error when checking : expected dense_input_6 to have shape (None, 784) but got array with shape (784, 1)


I wonder, why?

predict_classes is expecting a 2D array of shape (num_instances, features), like X_test is. But indexing a single instance as in X_test[10] returns a 1D array of shape (features,).

To add back the extra axis, you can use np.expand_dims(X_test[10], axis=0), or X_test[10][np.newaxis,:], or don't get rid of it in the first place (e.g., by using X_test[10:11]).

• It doesn't seem to work: no error message but no output either. Weird. – Hendrik Aug 17 '16 at 7:40

Currently (Keras v2.0.8) it takes a bit more effort to get predictions on single rows after training in batch.

Basically, the batch_size is fixed at training time, and has to be the same at prediction time.

The workaround right now is to take the weights from the trained model, and use those as the weights in a new model you've just created, which has a batch_size of 1.

The quick code for that is

model = create_model(batch_size=64)
mode.fit(X, y)
weights = model.get_weights()
single_item_model = create_model(batch_size=1)
single_item_model.set_weights(weights)
single_item_model.compile(compile_params)


Here's a blog post that goes into more depth: https://machinelearningmastery.com/use-different-batch-sizes-training-predicting-python-keras/

I've used this approach in the past to have multiple models at prediction time- one that makes predictions on big batches, one that makes predictions on small batches, and one that makes predictions on single items. Since batch predictions are much more efficient, this gives us the flexibility to take in any number of prediction rows (not just a number that is evenly divisible by batch_size), while still getting predictions pretty rapidly.

You can do:

q = model.predict( np.array( [single_x_test,] )  )


You should pass a list with just 1 example, I can't test right now but this should work:

model1.predict_classes([X_test[10]])

• It doesn't work unfortunately. – Hendrik Aug 17 '16 at 7:39
self.result = self.model.predict(X)


where X is numpy array. That is all I did and it worked.

This would be how to predict for one element, this time number 17.

model.predict_classes(X_test[17:18])

• what's wrong with the answer? – Supamee Sep 6 at 17:42

I have fixed this by using the following approach:

single_test = X_test[10]
single_test = single_test.reshape(1,784)


Please note that amount of features (784) in the reshape function is based on your example above, if you have fewer features then you need to adjust it.

Hope it will work for you too.