TensorFlow:评估恢复图 [英] TensorFlow: eval restored graph

查看:25
本文介绍了TensorFlow:评估恢复图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从检查点恢复图形.检查点由 tf.Supervisor 创建.有 meta 文件和检查点.

I am trying to restore graph from a checkpoint. The checkpoint is created by tf.Supervisor. There are both meta file and checkpoint.

我试图实现的是从单独的应用程序加载此图以运行某些操作(即恢复现有模型).

What I try to achive is to load this graph from separate application to run some operation (i.e. resue existing model).

我按照以下方式执行此操作(如下所述:https://www.tensorflow.org/api_docs/python/tf/train/import_meta_graph):

I do this as the following (as explained here: https://www.tensorflow.org/api_docs/python/tf/train/import_meta_graph):

meta = 'path/to/file.meta'

my_graph = tf.Graph()
with my_graph.as_default():
        with tf.Session() as sess:
                saver = tf.train.import_meta_graph(meta)
                saver.restore(sess, tf.train.latest_checkpoint(os.path.dirname(meta)))
                op = my_graph.get_operation_by_name("op")
                print(sess.run(op))

我看到的是None.我希望看到的是一维张量.我使用 get_collection 检查了 my_graph 对象,发现我的所有变量都需要 op 正确运行并使用从检查点恢复的值进行初始化.我怎样才能弄清楚为什么没有正确评估操作?我真的被困在这里了.

What I see is None. What I expect to see is 1-D Tensor. I inspected my_graph object using get_collection and find that there are all my variables required for op to run correctly initialized with values restored from the checkpoint. How can I figure out why the operation is not evaluated correctly? I am really stuck here.

以下代码:

print(sess.run(my_graph.get_operation_by_name("Variable_2")))
print(sess.run(my_graph.get_tensor_by_name("Variable_2:0")))

印刷品

None
4818800

就好像一个操作和对应的变量之间没有联系.

as if there is no connection between an operation and corresponding variable.

推荐答案

tf.Graph.get_operation_by_name() 方法总是返回一个 tf.Operation 对象.当您将 tf.Operation 对象传递给 时tf.Session.run(),TensorFlow 将执行该操作(以及它所依赖的一切)并丢弃其输出(如果有).

The tf.Graph.get_operation_by_name() method always returns a tf.Operation object. When you pass a tf.Operation object to tf.Session.run(), TensorFlow will execute that operation (and everything on which it depends) and discard its outputs (if any).

如果您对特定输出的值感兴趣,您必须告诉 TensorFlow 哪个输出(a tf.Tensor) 你感兴趣.有两个主要选项:

If you are interested in the value of a particular output, you have to tell TensorFlow which output (a tf.Tensor) you are interested in. There are two main options:

  • 从图中获取一个 tf.Operation,然后选择其中一个 输出:

  • Get a tf.Operation from the graph and then select one of its outputs:

op = my_graph.get_operation_by_name("op")
output = op.outputs[0]
print(sess.run(output))

  • 通过调用 tf.Tensor"noreferrer">tf.Graph.get_tensor_by_name(),并将 ":<output index>" 附加到操作名称:

  • Get a tf.Tensor from the graph by calling tf.Graph.get_tensor_by_name(), and appending ":<output index>" to the operation's name:

    output = my_graph.get_tensor_by_name("op:0")
    print(sess.run(output))
    

  • 为什么 TensorFlow 会做出这种区分?一方面,一个操作可以有多个输出,因此有时需要具体说明您想要获取哪个输出.另一方面,操作可能会产生副作用并产生大量输出——参见 tf.assign() 为例——通常将 tf.Operation 传递给 sess.run() 更有效,以便该值不会复制回 Python 程序.

    Why does TensorFlow draw this distinction? For one thing, a operation can have multiple outputs, so it is sometimes necessary to be specific about which output you want to fetch. For another, an operation may have a side effect and produce a large output—see tf.assign() for an example—and it is often more efficient to pass the tf.Operation to sess.run() so that the value is not copied back into the Python program.

    这篇关于TensorFlow:评估恢复图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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