Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between model(x) and model.predict(x) in Keras?

What is the difference between using model(x) and model.predict(x) for predicting the outcome of a model in Keras?

like image 810
Leonardo Barazza Avatar asked Mar 22 '19 22:03

Leonardo Barazza


1 Answers

Keras with tensorflow backend was using underlying tensorflow objects, but mostly was providing high level outputs which could be understood outside the tensorflow environment (as an example it could output numpy arrays or python lists).
Today given a model in tensorflow 2.0 (built using the keras library),

out_np = model.predict(x)

provides a numpy array which can, as an example, be printed with print(out_np).
On the other hand,

out_tf = model(x)

results into a tensorflow object, wich can be converted to a numpy array with .numpy()
The two results are equivalent, as an example, we have that the following is True,

out_np.max() == out_tf.numpy().max()

The format may be different, but the meaning of model(x) and model.predict(x) is the same:
given an input x, it is the value of the output nodes of a network characterized by its structure, weights and biases.

like image 143
Antonio TortoriciMontaperto Avatar answered Oct 07 '22 01:10

Antonio TortoriciMontaperto