1

I can get a tensor by graph.get_tensor_by_name, however I cannot find it in tf.global_variable. In my case, I defined some tf.Tensor as follows:

output_y = Dense(units=y.shape[1],activation='softmax',kernel_regularizer=regularizers.l2(),bias_regularizer=regularizers.l2(),activity_regularizer=regularizers.l2(),name='output_y_'+str(index))(pretrain_output)
y_tf = tf.placeholder(tf.float32, shape=(None, y.shape[1]),name='y_tf_'+str(index))
loss_tensor = tf.nn.softmax_cross_entropy_with_logits(logits=output_y, labels=y_tf, name='loss_tensor_' + str(index))

I can export the tensor shape and name as follows:

>>output_y
<tf.Tensor 'train_variable/output_y_0/Softmax:0' shape=(?, 4) dtype=float32>
>>y_tf
<tf.Tensor 'train_variable/y_tf_0:0' shape=(?, 4) dtype=float32>
>>loss_tensor
<tf.Tensor 'train_variable/loss_tensor_0/Reshape_2:0' shape=(?,) dtype=float32>

Also, I can use tf.get_default_graph.get_tensor_by_name to retrieve the tensor:

>>tf.get_default_graph().get_tensor_by_name('train_variable/output_y_0/Softmax:0')
<tf.Tensor 'train_variable/output_y_0/Softmax:0' shape=(?, 4) dtype=float32>
>>tf.get_default_graph().get_tensor_by_name('train_variable/y_tf_0:0')
<tf.Tensor 'train_variable/y_tf_0:0' shape=(?, 4) dtype=float32>
>>tf.get_default_graph().get_tensor_by_name('train_variable/loss_tensor_0/Reshape_2:0')
<tf.Tensor 'train_variable/loss_tensor_0/Reshape_2:0' shape=(?,) dtype=float32>

However, these variable names cannot be found in tf.global_variables(). It seems that tf.global_variables() only contains the parameter variables like kernel/bias. Now I have to remember the tensor name in order to retrieve the object output (output_y in my case). Can someone show me how to retrieve a tensor, for example search it in a list with all tensor?

Xiang Lv
  • 31
  • 3

1 Answers1

1

There is a difference between a tensor from the read-operation of a node and a tensor as a variable.

A variable consists of a value and several operations:

import tensorflow as tf
a = tf.get_variable('a', tf.float32)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

sess.run(a)  # gives 42.
sess.run(tf.get_default_graph().get_tensor_by_name('a/read:0'))  # gives 42. as well
print(a.op.outputs)  # <tf.Tensor 'a:0' shape=() dtype=float32_ref>]

It just behaves similarly:

>>> type(a)
<class 'tensorflow.python.ops.variables.Variable'>
>>> type(tf.get_default_graph().get_tensor_by_name('a/read:0'))
<class 'tensorflow.python.framework.ops.Tensor'>

but they are different though.

The easiest way is to return output_y in case you need it again. Otherwise just follow: https://stackoverflow.com/a/36893840/7443104

Patwie
  • 4,360
  • 1
  • 21
  • 41
  • Oh I see. So, tf.global_variable only contains tensor as variable, not include the tensor as operation. Many thanks! – Xiang Lv Jun 26 '18 at 06:48