Tensorflow1.4 之 saver()

saver()函数翻译

Tensorflow中saver是一个类,保存在 \tensorflow\python\training\saver.py文件中。

Saver 类添加一个在 checkpoints 文件中保存和重新加载的操作(op)。

Saver 可以使用提供的计数器 counter 自动的对 checkpoint 文件进行命名。 在训练中,这点让我们可以在不同的 steps 时,保存多个 checkpoint 文件。例如,可以使用训练 step 数来给 checkpoint 文件起名。为了避免撑爆磁盘, saver 自动管理 checkpoint 文件。例如:可以保存最近的N个文件,或者N个小时保存一个文件。

可以通过设置save 函数中 global_step 参数来给 checkpoint 文件名进行数字编号。

python
saver.save(sess, 'my-model', global_step=0) ==> filename: 'my-model-0'
...
saver.save(sess, 'my-model', global_step=1000) ==> filename: 'my-model-1000'

此外, 配置 Saver() 构造函数的参数能让你对 checkpoint 文件在磁盘上生成有更多的控制方式。

“max_to_keep”
配置最大的保存 checkpoint 文件的个数,生成新文件时会删除旧文件,如果设置为0 或者 None,则保存所有 checkpoint 文件,默认为保存5个checkpoint文件。

“keep_checkpoint_every_n_hours”
除保证checkpoint 文件最多有 max_to_keep 个之外,你希望每一个小时保存一个checkpoint文件,当训练时间非常长,而你又想每隔一段时间分析训练进度时,这点非常有用。例如,将 keep_checkpoint_every_n_hours 设置为2 ,则每过两个小时会保存一个checkpoint文件。默认将其设置为10000时disables the feature.

需要注意的是,需要调用‘save()’方法来保存模型。

训练工程中通常这样保存模型参数:

# Create a saver.
saver = tf.train.Saver(...
variables...)
# Launch the graph and train, saving the model every 1,000 steps.
sess = tf.Session()
for step in xrange(1000000):
    sess.run(..training_op..)
    if step % 1000 == 0:
        # Append the step number to the checkpoint name:
        saver.save(sess, 'my-model', global_step=step)

猜你喜欢

转载自blog.csdn.net/ilikede/article/details/79131695