Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the predict and predict_on_batch methods of a Keras model?

According to the keras documentation:

predict_on_batch(self, x) Returns predictions for a single batch of samples. 

However, there does not seem to be any difference with the standard predict method when called on a batch, whether it being with one or multiple elements.

model.predict_on_batch(np.zeros((n, d_in))) 

is the same as

model.predict(np.zeros((n, d_in))) 

(a numpy.ndarray of shape (n, d_out)

like image 807
Phylliade Avatar asked Jul 07 '17 13:07

Phylliade


People also ask

What is the difference between model predict and model evaluate?

predict() returns the final output of the model, i.e. answer. While model. evaluate() returns the loss. The loss is used to train the model (via backpropagation) and it is not the answer.

What is model predict in keras?

Keras model predicts is the method of function provided in Keras that helps in the predictions of output depending on the specified samples of input to the model.

What is predict on batch?

Batch prediction is useful when you want to generate predictions for a set of observations all at once, and then take action on a certain percentage or number of the observations. Typically, you do not have a low latency requirement for such an application.

How does model predict work?

model. predict() : given a trained model, predict the label of a new set of data. This method accepts one argument, the new data X_new (e.g. model. predict(X_new) ), and returns the learned label for each object in the array.


2 Answers

It seems predict_on_batch is a lot faster compared to predict if executed on a single batch.

  • batch & model information
    • batch shape: (1024, 333)
    • batch dtype: float32
    • model parameters: ~150k
  • timeit result:
    • predict: ~1.45 seconds
    • predict_on_batch: ~95.5 ms

In summary, predict method has extra operations to ensure a collection of batches are processed right, whereas, predict_on_batch is a lightweight alternative to predict that should be used on a single batch.

like image 28
Kutay YILDIZ Avatar answered Oct 12 '22 10:10

Kutay YILDIZ


The difference lies in when you pass as x data that is larger than one batch.

predict will go through all the data, batch by batch, predicting labels. It thus internally does the splitting in batches and feeding one batch at a time.

predict_on_batch, on the other hand, assumes that the data you pass in is exactly one batch and thus feeds it to the network. It won't try to split it (which, depending on your setup, might prove problematic for your GPU memory if the array is very big)

like image 144
GPhilo Avatar answered Oct 12 '22 08:10

GPhilo