2016-07-28 209 views
6

我已經寫了一個張量流CNN,它已經被訓練了。我想恢復它在幾樣,但不幸的是它吐出運行:Tensorflow ValueError:沒有要保存的變量

ValueError: No variables to save

我的eval代碼可以在這裏找到:

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() 

回答

10

tf.train.Saver必須創建變量你想恢復(或保存)。此外,它必須在與這些變量相同的圖形中創建。

假設Process.forward_propagation(…)還創建模型中的變量,增加了保護創作這條線之後應該工作:

forward_propgation_results = Process.forward_propagation(images) 

此外,你必須通過你創建的tf.Session構造函數,因此你的新tf.Graph將需要在with塊內移動創建sess

產生的功能將是這樣的:

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)) 
+0

非常感謝你,雖然我沒有刪除EVAL數據= EVAL數據,它應該仍然工作。現在當我運行程序時,python不會輸出任何內容。 –

+0

我不確定'eval_data'來自哪裏,因爲該名稱在您的示例中未被綁定。 Process.eval_inputs()是否使用輸入管道?也許你需要在運行'saver.restore()'後添加'tf.train.start_queue_runners(sess = sess)。 – mrry

+0

謝謝。代碼是固定的,但添加該行後彈出新的錯誤。 E tensorflow/core/client/tensor_c_api.cc:485] targets [0]超出範圍 –

相關問題