Search code examples
tensorflowkeras

Confusion about keras Model: __call__ vs. call vs. predict methods


I have realized that I do not quite understand the difference between calling either the __call__, call, or predict method of a Keras' model.

For example, we have a trained keras model. After calling the code:

# After training.
y_pred_1 = model(X_new)
y_pred_2 = model.call(X_new)
y_pred_3 = model.predict(X_new)

I expected that y_pred_1, y_pred_2, and y_pred_3 are all the same. But it turned out that they are not the same.

Could you please explain to me the difference?


Solution

  • Adding to @Dmitry Kabanov, they are similar yet they aren't exactly the same thing. If you care about performance, need to look in to critical differences between them.

    model.predict model(x)
    loops over the data in batches which means means that predict() calls can scale to very large arrays. happens in-memory and doesn't scale
    not differentiable differentiable
    use this if you just need the output value use this when you need to retrieve the gradients
    Output is NumPy value Output is a Tensor
    use this if you have batches of data to be predicted use this for small dataset
    relatively slower for small data relatively faster for small data

    Please check more detailed explanation in Keras FAQs