对于未初始化的变量,导入TensorFlow图失败

时间:2016-11-02 03:29:06

标签: python tensorflow

我正在尝试将多层感知器示例导出为.pb图。 为了做到这一点,我已经命名了输入变量和输出操作,并添加了以下行:

tf.train.write_graph(sess.graph_def, "./", "graph.pb", False)

要导入,我执行了以下操作:

with gfile.FastGFile("graph.pb",'rb') as f:

    print("load graph")
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')
    with tf.Session() as persisted_sess:

        persisted_result = persisted_sess.graph.get_tensor_by_name("output:0")
        avd = persisted_sess.run(persisted_result, feed_dict={"input_x:0": features_t})
        print ("Result:", str(avd))

导入正常,但会为“run”行抛出错误。

Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 972, in _do_call
    return fn(*args)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run_fn
    status, run_metadata)
  File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_3
     [[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "teste.py", line 56, in <module>
    avd = persisted_sess.run(persisted_result, feed_dict={"input_x:0": features_t})
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_3
     [[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]

Caused by op 'Variable_3/read', defined at:
  File "teste.py", line 37, in <module>
    _ = tf.import_graph_def(graph_def, name='')
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/importer.py", line 285, in import_graph_def
    op_def=op_def)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_3
     [[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]

我试图初始化所有变量,但它不起作用。

1 个答案:

答案 0 :(得分:2)

TensorFlow拆分将Graph定义和Variable值保存在不同的文件中(分别为图形和检查点)。

您想使用TF Saver。

有关详细信息,请参阅此答案: https://stackoverflow.com/a/33762168/4120005

或者这里的文档: https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html#saving-variables

如果你真的只需要从graphdef文件(* .pb)恢复,例如从C ++加载它,你将需要使用来自这里的freeze_graph.py脚本: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

此脚本将graphdef( .pb)和检查点( .ckpt)文件作为输入,并输出一个graphdef文件,其中包含常量形式的权重(您可以阅读文档有关详细信息的脚本。)