答案 0 :(得分:1)
何时使用?
如果您使用的是编译,则肯定必须在load_model()之后。毕竟,您需要一个模型进行编译。 (PS:load_model使用与模型一起保存的优化器自动编译模型)
编译有什么作用?
Compile定义损失函数,优化器和指标。就是这样。
它与权重无关,您可以根据需要多次编译模型,而不会对预训练权重造成任何问题。
您需要编译的模型进行训练(因为训练使用损失函数和优化器)。但是不必编译用于预测的模型。
您需要多次使用编译吗?
仅在以下情况下
:您要更改以下一项: 损失函数 优化器/学习率 指标 某一层的可训练属性 您加载(或创建)了尚未编译的模型。或者您的加载/保存方法没有考虑以前的编译。 再次编译的后果:
如果再次编译模型,则会丢失优化器状态。
这意味着您的训练在开始时会受到一点影响,直到调整学习速度,动量等为止。但是绝对不会对重量造成损害(除非您的初始学习速度如此之大以至于第一步训练会大大改变微调的权重。
答案 1 :(得分:0)
配置用于训练的模型。 documentation
就我个人而言,我不会称之为编译,因为它所做的与编译无关,在计算机科学术语中,同时考虑机器学习和编译是非常令人困惑/不知所措的。< /p>
它只是设置你传递给它的参数:优化器、损失函数、指标、急切执行。您可以多次运行它,它只会覆盖您之前设置的设置。
我对 TensorFlow 开发人员的建议是在短期内将其重命名为 configure
,并且可能在未来(不是那么重要)改用 1 个 setter(或使用工厂/构建器模式)对于每个配置参数。
base_layer.keras_api_gauge.get_cell('compile').set(True)
with self.distribute_strategy.scope():
if 'experimental_steps_per_execution' in kwargs:
logging.warn('The argument `steps_per_execution` is no longer '
'experimental. Pass `steps_per_execution` instead of '
'`experimental_steps_per_execution`.')
if not steps_per_execution:
steps_per_execution = kwargs.pop('experimental_steps_per_execution')
self._validate_compile(optimizer, metrics, **kwargs)
self._run_eagerly = run_eagerly
self.optimizer = self._get_optimizer(optimizer)
self.compiled_loss = compile_utils.LossesContainer(
loss, loss_weights, output_names=self.output_names)
self.compiled_metrics = compile_utils.MetricsContainer(
metrics, weighted_metrics, output_names=self.output_names)
self._configure_steps_per_execution(steps_per_execution or 1)
# Initializes attrs that are reset each time `compile` is called.
self._reset_compile_cache()
self._is_compiled = True
self.loss = loss or {} # Backwards compat.