Need To Compile Keras Model Before `model.evaluate()`

Because evaluate will calculate the loss function and the metrics.

You don't have any of them until you compile the model. They're parameters to the compile method:

model.compile(optimizer=..., loss=..., metrics=...) 

On the other hand, predict doesn't evaluate any metric or loss, it just passes the input data through the model and gets its output.

You need the "loss" for training too, so you can't train without compiling. And you can compile a model as many times as you want, and even change the parameters.


The outputs and the loss function:

The model's outputs depend on it being defined with weights. That is automatic and you can predict from any model, even without any training. Every model in Keras is already born with weights (either initialized by you or randomly initialized)

You input something, the model calculates the output. At the end of everything, this is all that matters. A good model has proper weights and outputs things correctly.

But before getting to that end, your model needs to be trained.

Now, the loss function takes the current output and compares it with the expected/true result. It's a function supposed to be minimized. The less the loss, the closer your results are to the expected. This is the function from which the derivatives will be taken so the backpropagation algorithm can update the weights.

The loss function is not useful for the final purpose of the model, but it's necessary for training. That's probably why you can have models without loss functions (and consequently, there is no way to evaluate them).

Tags:

Keras