【问题标题】:Naming TensorFlow/Keras checkpoints命名 TensorFlow/Keras 检查点
【发布时间】:2021-03-17 03:05:33
【问题描述】:

我正在关注 TensorFlow (link) 上的“使用 RNN 生成文本”教程。我已经对该模型进行了 10 个 epoch 的训练,并希望对其进行更多训练。我已经编写了允许模型恢复训练的代码。 (这将从最近的检查点开始恢复训练——在本例中为检查点 10)。它训练得很好。但是,保存的检查点会覆盖以前的检查点。这是因为当我重新运行代码时,纪元数再次从 1 开始。因此,当我完成 11 - 20 个 epoch 时,我仍然只有 10 个检查点(1 - 10),但它们已经覆盖了之前的 10 个检查点。我想将新的检查点重命名为检查点 11 - 20,但没有这样做。这是代码的相关部分:

# Directory where the checkpoints will be saved
checkpoint_dir = './training_checkpoints'
# Name of the checkpoint files
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch+10}")

checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_prefix,
    save_weights_only=True)

EPOCHS = 10

和TensorFlow官网的原代码唯一不同的是我修改了原行

checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch}")

checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch+10}")

但是,它不起作用。这是错误:

KeyError: 'epoch+10'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "project/RNN_text_generator_finetune.py", line 102, in <module>
    history = model.fit(dataset, epochs=EPOCHS, callbacks=[checkpoint_callback])
  File "/opt/miniconda3/envs/newest11142020/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/opt/miniconda3/envs/newest11142020/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1137, in fit
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "/opt/miniconda3/envs/newest11142020/lib/python3.8/site-packages/tensorflow/python/keras/callbacks.py", line 412, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "/opt/miniconda3/envs/newest11142020/lib/python3.8/site-packages/tensorflow/python/keras/callbacks.py", line 1249, in on_epoch_end
    self._save_model(epoch=epoch, logs=logs)
  File "/opt/miniconda3/envs/newest11142020/lib/python3.8/site-packages/tensorflow/python/keras/callbacks.py", line 1282, in _save_model
    filepath = self._get_file_path(epoch, logs)
  File "/opt/miniconda3/envs/newest11142020/lib/python3.8/site-packages/tensorflow/python/keras/callbacks.py", line 1332, in _get_file_path
    raise KeyError('Failed to format this callback filepath: "{}". '
KeyError: 'Failed to format this callback filepath: "./training_checkpoints/ckpt_{epoch+10}". Reason: \'epoch+10\''

有没有办法重命名代码中的检查点?

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    恢复训练时可以进行如下设置

    model.fit(..., 
         initial_epoch=epoch,
         ..)
    

    这里,initial_epoch 是一个整数。开始训练的时期,这对于恢复以前的训练运行很有用)。假设您已经在 epoch 10 训练了一个模型并停止训练。因此,当恢复训练时,将initial_epoch 设置为10Src 和有洞察力的discussin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-21
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多