是否在 Keras 初始化所有的权重和偏差(tensorflow 后端) ?

当我开始训练一个模型时,以前没有保存任何模型。我可以安全地使用 model.compile()。我现在已经将模型保存在 h5文件中,以便进一步使用 checkpoint进行培训。

我想进一步训练这个模型。我在这一点上感到困惑: 我可以在这里使用 model.compile()吗?它应该放在 model = load_model()语句之前还是之后?如果 model.compile()重新初始化所有的权重和偏差,我应该把它放在 model = load_model()语句之前。

在发现了一些讨论之后,在我看来,model.compile()只有在我以前没有保存任何模型时才需要。一旦我保存了模型,就不需要使用 model.compile()了。这是真的还是假的?当我想使用训练过的模型进行预测时,在进行预测之前是否应该使用 model.compile()

58886 次浏览

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

Don't forget that you also need to compile the model after changing the trainable flag of a layer, e.g. when you want to fine-tune a model like this:

  1. load VGG model without top classifier

  2. freeze all the layers (i.e. trainable = False)

  3. add some layers to the top

  4. compile and train the model on some data

  5. un-freeze some of the layers of VGG by setting trainable = True

  6. compile the model again (DON'T FORGET THIS STEP!)

  7. train the model on some data