In Tensorflow, get the names of all the Tensors in a graph
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 List of tensor names in graph in Tensorflow
The paper is not accurately reflecting the model. If you download the source from arxiv it has an accurate model description as model.txt, and the names in there correlate strongly with the names in the released model.
To answer your first question, sess.graph.get_operations()
gives you a list of operations. For an op, op.name
gives you the name and op.values()
gives you a list of tensors it produces (in the inception-v3 model, all tensor names are the op name with a ":0" appended to it, so pool_3:0
is the tensor produced by the final pooling op.)
In Tensorflow, get the names of all the Tensors in a graph using c++
I got the output by following the steps,
int node_count = graph_def.node_size();
for (int i = 0; i < node_count; i++)
{
auto n = graph_def.node(i);
cout<<"Names : "<< n.name() <<endl;
}
Tensorflow: Get all weight tensors by name
You can call the tf.trainable_variables()
if you are only concerned about weights you can optimize. It returns a list of all variables with trainable
parameter set to True
.
tf.reset_default_graph()
# These can be optimized
for i in range(5):
tf.Variable(tf.random_normal(dtype=tf.float32, shape=[32,32]), name="h{}".format(i))
# These cannot be optimized
for i in range(5):
tf.Variable(tf.random_normal(dtype=tf.float32, shape=[32,32]), name="n{}".format(i), trainable=False)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
graph = tf.get_default_graph()
for t_var in tf.trainable_variables():
print(t_var)
Prints:<tf.Variable 'h0:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h1:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h2:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h3:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h4:0' shape=(32, 32) dtype=float32_ref>
On the other hand tf.global_variables()
returns a list of all variables:for g_var in tf.global_variables():
print(g_var)
<tf.Variable 'h0:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h1:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h2:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h3:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'h4:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'n0:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'n1:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'n2:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'n3:0' shape=(32, 32) dtype=float32_ref>
<tf.Variable 'n4:0' shape=(32, 32) dtype=float32_ref>
UPDATETo have more control over the Variables that you'd like to receive there are several way how to filten them. One way is what openmark suggested. In this case you can filter them based on the variable scope prefix.
However, if this is not enough, for example if you wish to access several groups simultaneously there are other ways. You could simply filter them by name, that is:
for g_var in tf.global_variables():
if g_var.name.beginswith('h'):
print(g_var)
However, you have to be aware of the naming convention of tensorflow Variables. That is :0
postfix for example, variable scope prefix and more.Second way, less involved, is to create your own collections. For example if I am interested in variables that ends with a number divisible by 2 and somewhere else in the code I am interested in variables whose name ends with a number divisible by 4 I could do something like this:
# These can be optimized
for i in range(5):
h_var = tf.Variable(tf.random_normal(dtype=tf.float32, shape=[32,32]), name="h{}".format(i))
if i % 2 == 0:
tf.add_to_collection('vars_divisible_by_2', h_var)
if i % 4 == 0:
tf.add_to_collection('vars_divisible_by_4', h_var)
and then I can simply call tf.get_collection()
function:tf.get_collection('vars_divisible_by_2)
[<tf.Variable 'h0:0' shape=(32, 32) dtype=float32_ref>,
<tf.Variable 'h2:0' shape=(32, 32) dtype=float32_ref>,
<tf.Variable 'h4:0' shape=(32, 32) dtype=float32_ref>]
or tf.get_collection('vars_divisible_by_4'):
[<tf.Variable 'h0:0' shape=(32, 32) dtype=float32_ref>,
<tf.Variable 'h4:0' shape=(32, 32) dtype=float32_ref>]
Tensorflow: How to get a tensor by name?
There is a function tf.Graph.get_tensor_by_name(). For instance:
import tensorflow as tf
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d, name='example')
with tf.Session() as sess:
test = sess.run(e)
print e.name #example:0
test = tf.get_default_graph().get_tensor_by_name("example:0")
print test #Tensor("example:0", shape=(2, 2), dtype=float32)
Related Topics
Spark Dataframe: Computing Row-Wise Mean (Or Any Aggregate Operation)
Python Pandas, Write Dataframe to Fixed-Width File (To_Fwf)
How to Change Any Data Type into a String
Asyncio: How to Cancel a Future Been Run by an Executor
How Does the Max() Function Work on List of Strings in Python
Setting Variables with Exec Inside a Function
Saving Plots (Axessubplot) Generated from Python Pandas with Matplotlib's Savefig
Safely Create a File If and Only If It Does Not Exist with Python
What Is More 'Pythonic' for 'Not'
Error Importing Scikit-Learn Modules
How to Check If Two Strings Are Anagrams of Each Other
Python Decompression Relative Performance
Continuing in Python's Unittest When an Assertion Fails
Bad Idea to Catch All Exceptions in Python
Does 'Anaconda' Create a Separate Pythonpath Variable for Each New Environment
How to Get Two Random Records with Django