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

查看:619
本文介绍了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 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属性
    • You want to change one of these:
      • Loss function
      • Optimizer / Learning rate
      • Metrics
      • The trainable property of some layer

      再次编译的后果:

      如果再次编译模型,则会丢失优化器状态.

      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天全站免登陆