model.compile() 是否初始化 Keras(tensorflow 后端)中的所有权重和偏差? [英] Does model.compile() initialize all the weights and biases in Keras (tensorflow backend)?

查看:71
本文介绍了model.compile() 是否初始化 Keras(tensorflow 后端)中的所有权重和偏差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I start training a model, there is no model saved previously. I can use model.compile() safely. I have now saved the model in a h5 file for further training using checkpoint.

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

Say, I want to train the model further. I am confused at this point: can I use model.compile() here? And should it be placed before or after the model = load_model() statement? If model.compile() reinitializes all the weights and biases, I should place it before model = load_model() statement.

发现一些讨论后,我觉得只有在我之前没有保存模型的情况下才需要model.compile().一旦我保存了模型,就不需要使用 model.compile().这是真的还是假的?当我想使用经过训练的模型进行预测时,我应该在预测之前使用 model.compile() 吗?

After discovering some discussions, it seems to me that model.compile() is only needed when I have no model saved previously. Once I have saved the model, there is no need to use model.compile(). Is it true or false? And when I want to predict using the trained model, should I use model.compile() before predicting?

推荐答案

何时使用?

如果你正在使用compile,它肯定在load_model()之后.毕竟,您需要一个模型来编译.(PS:load_model 使用随模型保存的优化器自动编译模型)

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)

compile 有什么作用?

What does compile do?

Compile 定义了损失函数优化器指标.就这样.

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.

是否需要多次使用编译?

仅当:

  • 您想更改其中一项:
    • 损失函数
    • 优化器/学习率
    • 指标
    • 某层的trainable属性

    再次编译的后果:

    如果您再次编译模型,您将失去优化器状态.

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

    这篇关于model.compile() 是否初始化 Keras(tensorflow 后端)中的所有权重和偏差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆