【问题标题】:How to load the last checkpoint in TensorFlow?如何在 TensorFlow 中加载最后一个检查点?
【发布时间】:2020-06-07 17:53:10
【问题描述】:

我正在this tutorial 上使用 TensorFlow 进行练习。 evaluate 函数依赖于训练加载最新的检查点:

checkpoint_path = "./checkpoints/train"
ckpt = tf.train.Checkpoint(encoder=encoder,
                           decoder=decoder,
                           optimizer = optimizer)
ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)

start_epoch = 0
if ckpt_manager.latest_checkpoint:
  start_epoch = int(ckpt_manager.latest_checkpoint.split('-')[-1])
  ckpt.restore(ckpt_manager.latest_checkpoint)

for epoch in range(start_epoch, EPOCHS):
    start = time.time()
    total_loss = 0

    for (batch, (img_tensor, target)) in enumerate(dataset):
        batch_loss, t_loss = train_step(img_tensor, target)
        total_loss += t_loss

        if batch % 100 == 0:
            print ('Epoch {} Batch {} Loss {:.4f}'.format(
              epoch + 1, batch, batch_loss.numpy() / int(target.shape[1])))
    loss_plot.append(total_loss / num_steps)

    ckpt_manager.save()

没有ckpt_manager.save()evaluation 功能将不起作用。

当我们已经训练了一个模型并且检查点在checkpoint_path 中可用时。我们应该如何在没有训练的情况下加载模型?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    您可以使用tf.train.latest_checkpoint获取最新的检查点文件,然后使用ckpt.restore手动加载:

    checkpoint_path = "./checkpoints/train"
    ckpt = tf.train.Checkpoint(encoder=encoder,
                               decoder=decoder,
    
    ckpt_path = tf.train.latest_checkpoint(checkpoint_path)
    ckpt.restore(ckpt_path)
    

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 2018-10-04
      • 2021-08-16
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      相关资源
      最近更新 更多