Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I load a Keras model from .json and .hdf5 files. When I call model.evaluate(), it returns an error:

You must compile a model before training/testing. Use `model.compile(optimizer, loss)

Why do I need to compile to run evaluate()?

To add, the model can be passed predict() with no problem.

like image 284
bhomass Avatar asked Sep 09 '17 05:09

bhomass


People also ask

Is model compile necessary?

It is the necessary step that is necessary to perform before training the model. It is the final and important step in creating a model. After completing the compilation model, the training process begins.

Why do we need to compile the model?

During compilation, COMPILE checks for format errors, so you can use COMPILE to help debug your code before running a model. When you do not use COMPILE before you run the model, then the model is compiled automatically before it is solved.

What does it mean to compile a Keras model?

Compile defines the loss function, the optimizer and the metrics. That's all. It has nothing to do with the weights and you can compile a model as many times as you want without causing any problem to pretrained weights. You need a compiled model to train (because training uses the loss function and the optimizer).


1 Answers

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).

like image 109
Daniel Möller Avatar answered Oct 17 '22 18:10

Daniel Möller