TensorFlow范围名称中的枚举

时间:2016-12-03 15:39:37

标签: tensorflow

我很难理解自动枚举如何在范围命名中工作。例如,以下内容:

with tf.variable_scope("foo"):
    with tf.variable_scope("bar") as scope:
         v = tf.get_variable("v", [1])

会创建一个变量, v.name 设置为 foo / bar / v:0

如果我重用该变量,它将再次为 foo / bar / v:0 。如果我使用另一个变量,它将是其他变量,例如富/酒吧/×:0

有人可以解释一下这个枚举器的目的吗:0 并告诉我该怎么办才能得到像 foo / bar / x:1 这样的名字?

1 个答案:

答案 0 :(得分:0)

后缀:0表示Tensorflow操作的输出参数的索引。在幕后,get_variable使用Variable op,它只有一个输出。因此,使用get_variable定义的任何变量的后缀只能是:0。也许这可以通过例子来澄清:

import tensorflow as tf
import numpy as np

tf.reset_default_graph()

# First we instantiate a tensor using get_variable
v = tf.get_variable("v", [1])
print("operation used is {}".format(v.op.op_def.name))
print("The output tensor is named {}".format(v.name))
print()

# Now we instantiate two more tensors using split.
# We'll try to use the same name but Tensorflow will add the suffix "_1".
# Furthermore Tensorflow will use :[suffix] to indicate the index of the output.
v = tf.split(1, 2, tf.constant(np.random.randn(10, 4)), name='v')
print("length of v is {}".format(len(v)))
print("operation used is {}".format(v[0].op.op_def.name))
print("The first and second outputs are named {} and {} respectively".format(v[0].name, v[1].name))

结果输出:

operation used is Variable
The output tensor is named v:0

length of v is 2
operation used is Split
The first and second outputs are named v_1:0 and v_1:1 respectively