Tensorflow 保护程序似乎覆盖了现有的已保存变量文件 [英] Tensorflow saver seems to overwrite existing saved variable files

查看:19
本文介绍了Tensorflow 保护程序似乎覆盖了现有的已保存变量文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 tensorflow 编写神经网络代码.我让它在每 1000 个 epoch 中保存变量.因此,我希望为不同的文件保存第 1001 个纪元、第 2001 个纪元、第 3001 个纪元……的变量.下面的代码是我做的保存功能.

I am writing neural network code in tensorflow. I made it to save variables in every 1000 epoch. So, I expect to save variables of 1001th epoch, 2001th epoch, 3001th epoch ... for different files. The code below is the save function I made.

def save(self, epoch):
    model_name = "MODEL_save"
    checkpoint_dir = os.path.join(model_name)

    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
    self.saver.save(self.sess, checkpoint_dir + '/model', global_step=epoch)
    self.saver.save(self.sess, checkpoint_dir + '/model')
    print("path for saved %s" % checkpoint_dir)

一旦调用该函数,我将这段代码保存两次.因为我想通过使用 'global_step=epoch' 为每 1000 个时期保存变量的历史记录.并且想在没有指定纪元的情况下将最新的变量保存在文件中.每当满足时代条件时,我都会调用此函数,如下所示.

I made this code to save two times once the function is called. Because I wanted to save history of variables for every 1000 epoch by using 'global_step=epoch'. And wanted to save latest variables in the file without epoch specified. I call this function whenever the epoch condition is met like below.

for epoch in xrange(self.m_total_epoch):

    .... CODE FOR NEURAL NETWORK ....

    if epoch%1000 == 1 and epoch != 1:
        self.save(epoch)

假设当前纪元是 29326,我希望目录中所有保存的文件来自 1001, 2001, 3001 ... 29001.但是,只有部分文件来自 26001, 27001, 28001, 29001.我检查过它发生了在其他电脑中.这与我的预期不同.为什么会发生?

Assuming current epoch is 29326, I expect all the saved files in the directory from 1001, 2001, 3001 ... 29001. However, there are only partial of files from 26001, 27001, 28001, 29001. I checked it happened in other computers. It is different from what I expected. Why does it happen?

推荐答案

tf.train.Saver 在其构造函数中有一个 max_to_keep 参数,它只保存最新的模型.而这个 max_to_keep 参数的默认值是 5,有点出人意料.所以默认情况下,你只会有最新的 5 个模型.

tf.train.Saver has a max_to_keep argument in its constructor that keeps only the latest models saved. And this max_to_keep argument, somewhat suprisingly, has a default value of 5. So by default, you will only have the latest 5 models.

要保留所有模型,将此变量设置为None:

To keep all models, set this variable to None:

saver = tf.train.Saver(max_to_keep=None)

这篇关于Tensorflow 保护程序似乎覆盖了现有的已保存变量文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆