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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.