Mike Vella
Mike Vella

Reputation: 10575

Output node for tensorflow graph created with tf.layers

I have built a tensorflow neural net and now want to run the graph_util.convert_variables_to_constants function on it. However this requires an output_node_names parameter. The last layer in the net has the name logit and is built as follows:

logits = tf.layers.dense(inputs=dropout, units=5, name='logit')

however there are many nodes in that scope:

gd = sess.graph_def
for n in gd.node:
    if 'logit' in n.name:print(n.name)

prints:

logit/kernel/Initializer/random_uniform/shape
logit/kernel/Initializer/random_uniform/min
logit/kernel/Initializer/random_uniform/max
logit/kernel/Initializer/random_uniform/RandomUniform
logit/kernel/Initializer/random_uniform/sub
logit/kernel/Initializer/random_uniform/mul
logit/kernel/Initializer/random_uniform
logit/kernel
logit/kernel/Assign
logit/kernel/read
logit/bias/Initializer/zeros
logit/bias
logit/bias/Assign
logit/bias/read
logit/Tensordot/Shape
logit/Tensordot/Rank
logit/Tensordot/axes
...
logit/Tensordot/Reshape_1
logit/Tensordot/MatMul
logit/Tensordot/Const_2
logit/Tensordot/concat_2/axis
logit/Tensordot/concat_2
logit/Tensordot
logit/BiasAdd
...

How do I work out which of these nodes is the output node?

Upvotes: 0

Views: 1153

Answers (1)

Max
Max

Reputation: 1039

If the graph is complex, a common way is to add an identity node at the end:

output = tf.identity(logits, 'output')
# you can use the name "output"

For example, the following code should work:

logits = tf.layers.dense(inputs=dropout, units=5, name='logit')
output = tf.identity(logits, 'output')
output_graph_def = tf.graph_util.convert_variables_to_constants(
    ss, tf.get_default_graph().as_graph_def(), ['output']) 

Upvotes: 1

Related Questions