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

The above code returns true.I use with tf.device here to illustrate that the ":0" doesn't mean the variable lie on the specific device.So what's the meaning of the ":0" in the variable's name(foo and bar in this example)?

EncodeTS
  • 328
  • 3
  • 9
  • Does the assert return true? Have you read the documentation for `tensorflow.Variable` class? – OneCricketeer Dec 02 '16 at 06:01
  • @cricket_007 Yes,it returns true,and In the [tensorflow doc](https://www.tensorflow.org/versions/master/how_tos/variable_scope/index.html),you can find some similar code,but the doc never explain what's the meaning of the ":0". – EncodeTS Dec 02 '16 at 06:04
  • Gotcha. I was just reading over the source code. Can't easily spot it, though. – OneCricketeer Dec 02 '16 at 06:07
  • As far as I know it means "variable bar after 0th iteration" but I am not experienced TF user – mbednarski Dec 02 '16 at 09:53

1 Answers1

28

It has to do with representation of tensors in underlying API. A tensor is a value associated with output of some op. In case of variables, there's a Variable op with one output. An op can have more than one output, so those tensors get referenced to as <op>:0, <op>:1 etc. For instance if you use tf.nn.top_k, there are two values created by this op, so you may see TopKV2:0 and TopKV2: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?

Community
  • 1
  • 1
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
  • 1
    When you create a variable, e.g. using `tf.Variable()`, you don't and you can't append a `:0` to the name but when you get a variable name with e.g. `some_var.name`, the name you get has a `:0`. I think this is the source of much confusion. In some contexts, a 'variable' is not really a variable but a tensor op that gets the variable, so that there's a `:0` at the end, but this is not really explicitly explained in the documentation. – Joshua Chia May 10 '19 at 03:07