如何按名称获取张量流操作? [英] How to get a tensorflow op by name?
问题描述
您可以使用 tf.get_default_graph().get_tensor_by_name("tensor_name:0")
但是你能得到一个操作,比如Optimizer.minimize
,或者一个队列上的enqueue
操作?
But can you get an operation, such as Optimizer.minimize
, or an enqueue
operation on a queue?
在我的第一个模型中,我从 build_model
函数返回了我需要的所有张量和操作.但是张量列表变得丑陋.在后来的模型中,我将所有张量和操作都放入字典中以便于访问.这一次,我想我会在需要时按名称查找张量,但我不知道如何使用 ops 来做到这一点.
In my first model I returned all tensors and ops I would need from a build_model
function. But the list of tensors got ugly. In later models I tossed all tensors and ops in a dictionary for easier access. This time around I thought I'd just look up tensors by name as I needed them, but I don't know how to do that with ops.
或者有更好的方法来做到这一点?我发现到处都需要各种张量和操作.训练、推理代码、测试用例,因此需要一种很好的标准方式来访问图表的各个部分,而无需到处传递变量.
Or is there a better way to do this? I find various tensors and ops are needed all over the place. Training, inference code, test cases, hence the desire for a nice standard way of accessing the various parts of the graph without passing variables all over the place.
推荐答案
您可以使用 tf.Graph.get_operation_by_name()
方法通过名称获取tf.Operation
.例如,要从默认图中获取一个名为 "enqueue"
的操作:
You can use the tf.Graph.get_operation_by_name()
method to get a tf.Operation
by name. For example, to get an operation called "enqueue"
from the default graph:
op = tf.get_default_graph().get_operation_by_name("enqueue")
这篇关于如何按名称获取张量流操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!