Does model.compile() initialize all the weights and biases in Keras (tensorflow backend)?

When to use?

If you’re using compile, surely it must be after load_model(). After all, you need a model to compile. (PS: load_model automatically compiles the model with the optimizer that was saved along with the model)

What does compile do?

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). But it’s not necessary to compile a model for predicting.

Do you need to use compile more than once?

Only if:

  • You want to change one of these:
    • Loss function
    • Optimizer / Learning rate
    • Metrics
    • The trainable property of some layer
  • You loaded (or created) a model that is not compiled yet. Or your load/save method didn’t consider the previous compilation.

Consequences of compiling again:

If you compile a model again, you will lose the optimizer states.

This means that your training will suffer a little at the beginning until it adjusts the learning rate, the momentums, etc. But there is absolutely no damage to the weights (unless, of course, your initial learning rate is so big that the first training step wildly changes the fine tuned weights).

Leave a Comment