keras重载继续训练的问题

问题

colab的时间有限额,被中断后,要重新连接,加载模型继续训练。出现的问题是,每次重新加载模型后,训练开始的loss都会比中断前的loss大很大,训练几个batch后,loss会慢慢降下来。

原因

重新加载的代码有问题,模型优化器状态被重初始化了。

原来错误的步骤

  • 定义并编译模型
  • 加载权重(load_weights)
  • 训练
  • 保存模型及权重
  • 连接中断
  • 重新连接,回到第一步

参考stackoverflow,

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 i

你可能感兴趣的:(keras,tensorflow,深度学习)