2016-12-02 888 views
8
import tensorflow as tf 
with tf.device('/gpu:0'): 
    foo = tf.Variable(1, name='foo') 
    assert foo.name == "foo:0" 
with tf.device('/gpu:1'): 
    bar = tf.Variable(1, name='bar') 
    assert bar.name == "bar:0" 

上面的代码返回true。我在这里使用with tf.device来说明“:0”不是指变量lie在特定设备上。那么变量名称中的“:0”是什么意思foo和bar在这个例子中)?在TensorFlow中,变量名称中“:0”的含义是什么?

+0

assert是否返回true?你读过'tensorflow.Variable'类的文档吗? –

+0

@ cricket_007是的,它返回true,并且在[tensorflow doc](https://www.tensorflow.org/versions/master/how_tos/variable_scope/index.html)中,可以找到一些类似的代码,但是doc永远不要解释“:0”的含义。 – EncodeTS

+0

Gotcha。我只是在阅读源代码。虽然不能轻易发现它。 –

回答

11

它与底层API中张量的表示有关。张量是与某些操作的输出相关的值。在变量的情况下,有一个输出为Variable op。运算可以有一个以上的输出,所以这些张量获得参考为<op>:0<op>:1等。例如,如果你使用tf.nn.top_k,还有由此op创建两个值,所以你可能会看到TopKV2:0TopKV2:1

a,b=tf.nn.top_k([1], 1) 
print a.name # => 'TopKV2:0' 
print b.name # => 'TopKV2:1' 

How to understand the term `tensor` in TensorFlow?

相关问题