Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用import\u meta\u graph还原图形时未创建变量?_Python_Tensorflow - Fatal编程技术网

Python 使用import\u meta\u graph还原图形时未创建变量?

Python 使用import\u meta\u graph还原图形时未创建变量?,python,tensorflow,Python,Tensorflow,我尝试从我训练的模型中恢复图形,然后尝试恢复模型: import tensorflow as tf import reader from ptb_word_lm import PTBInput, PTBModel, get_config, run_epoch def main(_): checkpoint_path = "/Users/roger/data/ptb_out" checkpoint_path = tf.train.latest_checkpoint(checkpo

我尝试从我训练的模型中恢复图形,然后尝试恢复模型:

import tensorflow as tf
import reader
from ptb_word_lm import PTBInput, PTBModel, get_config, run_epoch

def main(_):
    checkpoint_path = "/Users/roger/data/ptb_out"
    checkpoint_path = tf.train.latest_checkpoint(checkpoint_path)

    raw_data = reader.ptb_raw_data("/Users/roger/data/simple-examples/small_data")
    train_data, valid_data, test_data, _ = raw_data

    config = get_config()
    eval_config = get_config()
    eval_config.batch_size = 1
    eval_config.num_steps = 1


    with tf.Session() as session:
        initializer = tf.random_uniform_initializer(-config.init_scale,
                                                config.init_scale)

        saver = tf.train.import_meta_graph(checkpoint_path + ".meta")
        saver.restore(session, checkpoint_path)

        with tf.name_scope("Test"):
            test_input = PTBInput(config=eval_config, data=test_data, name="TestInput")
            with tf.variable_scope("Model", reuse=True, initializer=initializer):
                mtest = PTBModel(is_training=False, config=eval_config,
                                input_=test_input)
            test_perplexity = run_epoch(session, mtest)
            print("Test Perplexity: %.3f" % test_perplexity)

if __name__ == "__main__":
  tf.app.run()
然而,我发现创建的变量
模型/嵌入
并没有从图中恢复。所以我得到如下错误:

 ValueError: Variable Model/embedding does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

那么如何才能正确地恢复模型

我认为,由于您在变量范围中设置了reuse=True,因此在调用PTBModel()时,它会尝试查找该变量,而不是创建它。如果在作用域中使用retuse=True的get_variable(),它将永远不会创建变量。

我认为,由于您的
模型
作用域嵌套在
测试
中,因此嵌入会变得
模型/测试/嵌入
,这让人感到困惑TF@gokul_uf但是根据训练规则,没关系