Tensorflow ValueError:没有要保存的变量 [英] Tensorflow ValueError: No variables to save from

查看:187
本文介绍了Tensorflow ValueError:没有要保存的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个tensorflow CNN,它已经受过训练.我希望将其还原以在一些示例上运行它,但不幸的是它吐出来了:

I have written a tensorflow CNN and it is already trained. I wish to restore it to run it on a few samples but unfortunately its spitting out:

ValueError:没有要保存的变量

ValueError: No variables to save

我的评估代码可以在这里找到:

My eval code can be found here:

import tensorflow as tf

import main
import Process
import Input

eval_dir = "/Users/Zanhuang/Desktop/NNP/model.ckpt-30"
checkpoint_dir = "/Users/Zanhuang/Desktop/NNP/checkpoint"

init_op = tf.initialize_all_variables()
saver = tf.train.Saver()

def evaluate():
  with tf.Graph().as_default() as g:
    sess.run(init_op)

    ckpt = tf.train.get_checkpoint_state(checkpoint_dir)

    saver.restore(sess, eval_dir)

    images, labels = Process.eval_inputs(eval_data = eval_data)

    forward_propgation_results = Process.forward_propagation(images)

    top_k_op = tf.nn.in_top_k(forward_propgation_results, labels, 1)

    print(top_k_op)

def main(argv=None):
    evaluate()

if __name__ == '__main__':
  tf.app.run()

推荐答案

必须在要还原(或保存)的变量后 创建tf.train.Saver.此外,必须在与这些变量相同的图形中创建它.

The tf.train.Saver must be created after the variables that you want to restore (or save). Additionally it must be created in the same graph as those variables.

假设Process.forward_propagation(…)还在模型中创建变量,则在此行之后添加保护程序创建应该起作用:

Assuming that Process.forward_propagation(…) also creates the variables in your model, adding the saver creation after this line should work:

forward_propgation_results = Process.forward_propagation(images)

此外,您还必须将创建的新tf.Graph传递给tf.Session构造函数,因此您还需要将sess的创建也移至该with块内.

In addition, you must pass the new tf.Graph that you created to the tf.Session constructor so you'll need to move the creation of sess inside that with block as well.

生成的函数将类似于:

def evaluate():
  with tf.Graph().as_default() as g:
    images, labels = Process.eval_inputs(eval_data = eval_data)
    forward_propgation_results = Process.forward_propagation(images)
    init_op = tf.initialize_all_variables()
    saver = tf.train.Saver()
    top_k_op = tf.nn.in_top_k(forward_propgation_results, labels, 1)

  with tf.Session(graph=g) as sess:
    sess.run(init_op)
    saver.restore(sess, eval_dir)
    print(sess.run(top_k_op))

这篇关于Tensorflow ValueError:没有要保存的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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