训练tensorflow模型会自动保存参数吗?

Does training tensorflow model automatically save parameters?

我 运行 demo tensorflow MNIST model(在 models/image/mnist 中)

python -m tensorflow.models.image.mnist.convolutional

是不是说模型训练完成后,parameters/weights会自动存储到二级存储?或者我们是否必须编辑代码以包含 "saver" 用于存储参数的函数?

不,它们不会自动保存。一切都在记忆中。您必须明确添加一个保护程序功能以将您的模型存储到辅助存储。

首先你创建一个保存操作

saver = tf.train.Saver(tf.all_variables())

然后您想在训练过程中保存您的模型,通常是在 N 步之后。此中间步骤通常命名为 "checkpoints".

  # Save the model checkpoint periodically.
  if step % 1000 == 0:
    checkpoint_path = os.path.join('.train_dir', 'model.ckpt')
    saver.save(sess, checkpoint_path)

然后您可以从检查点恢复模型:

 saver.restore(sess, model_checkpoint_path)

查看 tensorflow.models.image.cifar10 的具体示例