Let’s answer with an extract from Deep Learning with Python, Second Edition:
Both
y = model.predict(x)
andy = model(x)
(wherex
is an array of input data) mean “run the model onx
and retrieve the outputy
.” Yet they aren’t exactly the same thing.
predict()
loops over the data in batches (in fact, you can specify the batch size viapredict(x, batch_size=64)
), and it extracts the NumPy value of the outputs. It’s schematically equivalent to this:
def predict(x):
y_batches = []
for x_batch in get_batches(x):
y_batch = model(x_batch).numpy()
y_batches.append(y_batch)
return np.concatenate(y_batches)
This means that
predict()
calls can scale to very large arrays. Meanwhile,model(x)
happens in-memory and doesn’t scale. On the other hand,predict()
is not differentiable: you cannot retrieve its gradient if you call it in aGradientTape
scope.You should use
model(x)
when you need to retrieve the gradients of the model call, and you should usepredict()
if you just need the output value. In other words, always usepredict()
unless you’re in the middle of writing a low-level gradient descent loop (as we are now).
Leave a Reply