2

Let's say in Python 3.6 I create two placeholders in tensorflow and for each I assign a name. However, I use the same variable in python to store each tensor.

import tensorflow as tf
tfs = tf.InteractiveSession()

p3 = tf.placeholder(tf.float32,name='left')
p3 = tf.placeholder(tf.float32,name='right')

How can I now create an operation using the names I assigned instead of the variable names?

op1 = left * right
op2 = tf.multiply(left,right)

Obviously the first one won't work because python doesn't have a variable called 'left' or 'right', but it seems like in the second case I should be able to reference the names. If not, why would I ever bother to set names on a tensor?

In case it matters, I'm doing this on AWS Sagemaker conda_tensorflow_p36

Mark
  • 4,387
  • 2
  • 28
  • 48

2 Answers2

2

You should be able to use get_tensor_by_name.

tensor_left = tf.get_default_graph().get_tensor_by_name("left")
tensor_right = tf.get_default_graph().get_tensor_by_name("right")
op = tf.multiply(tensor_left, tensor_right)

SO question on getting tf.placeholder by name (using get_tensor_by_name)

Similar SO question for getting result.

Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
2

For getting tf.Operation by names, you can use tf.Graph.get_operation_by_name

op = tf.get_default_graph().get_operation_by_name("left")
op = tf.get_default_graph().get_operation_by_name("right")
Balaji MJ
  • 21
  • 3