简体   繁体   English

tensorflow-如何使用张量名称构建操作?

[英]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. 假设在Python 3.6中,我在tensorflow中创建了两个占位符,并为每个占位符分配了一个名称。 However, I use the same variable in python to store each tensor. 但是,我在python中使用相同的变量来存储每个张量。

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. 显然第一个不起作用,因为python没有名为“ left”或“ right”的变量,但似乎在第二种情况下,我应该能够引用这些名称。 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 以防万一,我在AWS Sagemaker上执行此操作conda_tensorflow_p36

You should be able to use get_tensor_by_name . 您应该可以使用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 ) 所以关于通过名称获取tf.placeholder问题 (使用get_tensor_by_name

Similar SO question for getting result. 获得结果的类似SO问题

For getting tf.Operation by names, you can use tf.Graph.get_operation_by_name 要通过名称获取tf.Operation,可以使用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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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