Python Can';t在不重新保存的情况下恢复tensorflow会话

Python Can';t在不重新保存的情况下恢复tensorflow会话,python,tensorflow,Python,Tensorflow,代码如下: import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf # print(os.getcwd()) # os.chdir(os.getcwd()) # os.chdir("/tmp") chk_file = "hello.chk" def save(checkpoint_file=chk_file): with tf.Session() as session:

代码如下:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf


# print(os.getcwd())
# os.chdir(os.getcwd())
# os.chdir("/tmp")


chk_file = "hello.chk"
def save(checkpoint_file=chk_file):
    with tf.Session() as session:
        x = tf.Variable(initial_value=[1, 2, 3], name="x")
        y = tf.Variable(initial_value=[[1.0, 2.0], [3.0, 4.0]], name="y")
        session.run(tf.global_variables_initializer())
        print(session.run(tf.global_variables()))
        saver = tf.train.Saver()
        save_path = saver.save(sess=session, save_path=checkpoint_file)
        print(session.run(tf.global_variables()))



def restore(checkpoint_file=chk_file):
    with tf.Session() as session:
        saver = tf.train.Saver()
        saver.restore(sess=session, save_path=checkpoint_file)
        print(session.run(tf.global_variables()[0]))
        print(tf.global_variables()[0])
        # print(session.run(tf.get_variable("x", shape=(3, ))))

def reset():
    tf.reset_default_graph()

path = save()
# print(path)
restore("/home/kaiyin/PycharmProjects/text-classify/hello.chk")
有几个问题:

  • restore(path)
    不起作用,与
    saver.restore的文档描述相反

  • 相对路径不适用于
    还原
    ,即使您已经在正确的目录中

  • 如果注释掉
    path=save()
    行,则会出现错误:

    /home/kaiyin/virtualenvs/tensorflow/bin/python/home/kaiyin/PycharmProjects/text-classify/restore.py 回溯(最近一次呼叫最后一次): 文件“/home/kaiyin/PycharmProjects/text-classify/restore.py”,第38行,在 还原(“/home/kayin/PycharmProjects/text-classify/hello.chk”) 文件“/home/kaiyin/PycharmProjects/text-classify/restore.py”,第27行,在restore中 saver=tf.train.saver() 文件“/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site packages/tensorflow/python/training/saver.py”,第1040行,在init self.build() 文件“/home/kaiyin/virtualenvs/tensorflow/lib/python3.5/site packages/tensorflow/python/training/saver.py”,第1061行,内部版本 raise VALUERROR(“无需保存的变量”) ValueError:没有要保存的变量

    进程已完成,退出代码为1

我可以接受前两个问题,但第三个问题是一个真正的拦路虎。究竟为什么每次我想恢复会话时都需要保存会话?由于没有全局会话对象,
save
函数如何产生如此大的影响也有点神秘

Tensorflow版本:1.0.1

Python 3.5.2


Ubuntu 16.04我整天头痛

刚刚解决:

正确的方法是在调用restore()之前需要重新初始化所有变量

例如,在cifar10项目中(-188行)

如果要恢复以前保存的变量

首先需要调用推断()来初始化所有变量 然后调用restore()


重新初始化

def restore(checkpoint_file=chk_file):
    with tf.Session() as session:
        x = tf.Variable(initial_value=[1, 2, 3], name="x")
        y = tf.Variable(initial_value=[[1.0, 2.0], [3.0, 4.0]], name="y")
        saver = tf.train.Saver()
        saver.restore(sess=session, save_path=checkpoint_file)
        print(session.run(tf.global_variables()[1]))
        print(tf.global_variables()[0])
        # print(session.run(tf.get_variable("x", shape=(3, ))))

def reset():
    tf.reset_default_graph()

restore("/home/kaiyin/PycharmProjects/text-classify/hello.chk")

你为什么要腌制储蓄罐?对不起。这是一个实验,现在删除谢谢你的输入,但在阅读了你的答案后,我仍然不知道应该做什么。你能写些代码澄清一下吗?谢谢