Python 在 Tensorflow 中,获取图中所有张量的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36883949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Tensorflow, get the names of all the Tensors in a graph
提问by P. Camilleri
I am creating neural nets with Tensorflow
and skflow
; for some reason I want to get the values of some inner tensors for a given input, so I am using myClassifier.get_layer_value(input, "tensorName")
, myClassifier
being a skflow.estimators.TensorFlowEstimator
.
我正在使用Tensorflow
和创建神经网络skflow
;由于某种原因,我想获得某种内在的张量的值给定的输入,所以我使用的myClassifier.get_layer_value(input, "tensorName")
,myClassifier
作为一个skflow.estimators.TensorFlowEstimator
。
However, I find it difficult to find the correct syntax of the tensor name, even knowing its name (and I'm getting confused between operation and tensors), so I'm using tensorboard to plot the graph and look for the name.
但是,我发现很难找到张量名称的正确语法,即使知道它的名称(而且我在操作和张量之间感到困惑),所以我使用 tensorboard 绘制图形并查找名称。
Is there a way to enumerate all the tensors in a graph without using tensorboard?
有没有办法在不使用张量板的情况下枚举图中的所有张量?
回答by Yaroslav Bulatov
You can do
你可以做
[n.name for n in tf.get_default_graph().as_graph_def().node]
Also, if you are prototyping in an IPython notebook, you can show the graph directly in notebook, see show_graph
function in Alexander's Deep Dream notebook
另外,如果您在 IPython notebook 中进行原型设计,则可以直接在 notebook 中显示图形,请参阅show_graph
Alexander's Deep Dream notebook 中的函数
回答by Salvador Dali
There is a way to do it slightly faster than in Yaroslav's answer by using get_operations. Here is a quick example:
有一种方法可以通过使用get_operations比 Yaroslav 的答案略快。这是一个快速示例:
import tensorflow as tf
a = tf.constant(1.3, name='const_a')
b = tf.Variable(3.1, name='variable_b')
c = tf.add(a, b, name='addition')
d = tf.multiply(c, a, name='multiply')
for op in tf.get_default_graph().get_operations():
print(str(op.name))
回答by Yuan Tang
tf.all_variables()
can get you the information you want.
tf.all_variables()
可以得到你想要的信息。
Also, this commitmade today in TensorFlow Learn that provides a function get_variable_names
in estimator that you can use to retrieve all variable names easily.
此外,今天在 TensorFlow Learn 中进行的此提交get_variable_names
在 estimator 中提供了一个函数,您可以使用该函数轻松检索所有变量名称。
回答by Szabolcs
I'll try to summarize the answers:
我将尝试总结答案:
To get all nodes:
获取所有节点:
all_nodes = [n for n in tf.get_default_graph().as_graph_def().node]
These have the type tensorflow.core.framework.node_def_pb2.NodeDef
这些有类型 tensorflow.core.framework.node_def_pb2.NodeDef
To get all ops:
获取所有操作:
all_ops = tf.get_default_graph().get_operations()
These have the type tensorflow.python.framework.ops.Operation
这些有类型 tensorflow.python.framework.ops.Operation
To get all variables:
获取所有变量:
all_vars = tf.global_variables()
These have the type tensorflow.python.ops.resource_variable_ops.ResourceVariable
这些有类型 tensorflow.python.ops.resource_variable_ops.ResourceVariable
And finally, to answer the question, to get all tensors:
最后,回答这个问题,得到所有张量:
all_tensors = [tensor for op in tf.get_default_graph().get_operations() for tensor in op.values()]
These have the type tensorflow.python.framework.ops.Tensor
这些有类型 tensorflow.python.framework.ops.Tensor
回答by Lu Howyou
I think this will do too:
我认为这也可以:
print(tf.contrib.graph_editor.get_tensors(tf.get_default_graph()))
But compared with Salvado and Yaroslav's answers, I don't know which one is better.
但是和 Salvado 和 Yaroslav 的回答相比,我不知道哪个更好。
回答by Picard
The accepted answer only gives you a list of strings with the names. I prefer a different approach, which gives you (almost) direct access to the tensors:
接受的答案只为您提供带有名称的字符串列表。我更喜欢一种不同的方法,它使您(几乎)可以直接访问张量:
graph = tf.get_default_graph()
list_of_tuples = [op.values() for op in graph.get_operations()]
list_of_tuples
now contains every tensor, each within a tuple. You could also adapt it to get the tensors directly:
list_of_tuples
现在包含每个张量,每个张量都在一个元组中。您还可以对其进行调整以直接获取张量:
graph = tf.get_default_graph()
list_of_tuples = [op.values()[0] for op in graph.get_operations()]
回答by gebbissimo
Since the OP asked for the list of the tensors instead of the list of operations/nodes, the code should be slightly different:
由于 OP 要求提供张量列表而不是操作/节点列表,因此代码应该略有不同:
graph = tf.get_default_graph()
tensors_per_node = [node.values() for node in graph.get_operations()]
tensor_names = [tensor.name for tensors in tensors_per_node for tensor in tensors]
回答by ted
Previous answers are good, I'd just like to share a utility function I wrote to select Tensors from a graph:
以前的答案很好,我只想分享我编写的用于从图中选择张量的实用程序函数:
def get_graph_op(graph, and_conds=None, op='and', or_conds=None):
"""Selects nodes' names in the graph if:
- The name contains all items in and_conds
- OR/AND depending on op
- The name contains any item in or_conds
Condition starting with a "!" are negated.
Returns all ops if no optional arguments is given.
Args:
graph (tf.Graph): The graph containing sought tensors
and_conds (list(str)), optional): Defaults to None.
"and" conditions
op (str, optional): Defaults to 'and'.
How to link the and_conds and or_conds:
with an 'and' or an 'or'
or_conds (list(str), optional): Defaults to None.
"or conditions"
Returns:
list(str): list of relevant tensor names
"""
assert op in {'and', 'or'}
if and_conds is None:
and_conds = ['']
if or_conds is None:
or_conds = ['']
node_names = [n.name for n in graph.as_graph_def().node]
ands = {
n for n in node_names
if all(
cond in n if '!' not in cond
else cond[1:] not in n
for cond in and_conds
)}
ors = {
n for n in node_names
if any(
cond in n if '!' not in cond
else cond[1:] not in n
for cond in or_conds
)}
if op == 'and':
return [
n for n in node_names
if n in ands.intersection(ors)
]
elif op == 'or':
return [
n for n in node_names
if n in ands.union(ors)
]
So if you have a graph with ops:
因此,如果您有一个带操作的图表:
['model/classifier/dense/kernel',
'model/classifier/dense/kernel/Assign',
'model/classifier/dense/kernel/read',
'model/classifier/dense/bias',
'model/classifier/dense/bias/Assign',
'model/classifier/dense/bias/read',
'model/classifier/dense/MatMul',
'model/classifier/dense/BiasAdd',
'model/classifier/ArgMax/dimension',
'model/classifier/ArgMax']
Then running
然后运行
get_graph_op(tf.get_default_graph(), ['dense', '!kernel'], 'or', ['Assign'])
returns:
返回:
['model/classifier/dense/kernel/Assign',
'model/classifier/dense/bias',
'model/classifier/dense/bias/Assign',
'model/classifier/dense/bias/read',
'model/classifier/dense/MatMul',
'model/classifier/dense/BiasAdd']
回答by Akshaya Natarajan
This worked for me:
这对我有用:
for n in tf.get_default_graph().as_graph_def().node:
print('\n',n)