简体   繁体   中英

tensorflow - how to build operations using tensor names?

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM