如何重命名一个尊重名称范围的变量?

col*_*ang 15 tensorflow

鉴于x, y是张量,我知道我能做到

with tf.name_scope("abc"):
    z = tf.add(x, y, name="z")
Run Code Online (Sandbox Code Playgroud)

z就是命名"abc/z".

我想知道f在以下情况下是否存在直接分配名称的函数:

with tf.name_scope("abc"):
    z = x + y
    f(z, name="z")
Run Code Online (Sandbox Code Playgroud)

f我现在使用的愚蠢是z = tf.add(0, z, name="z")

mrr*_*rry 29

如果你想"重命名"一个op,就没有办法直接这样做,因为一旦tf.Operation(或tf.Tensor)创建后它就是不可变的.因此,重命名op的典型方法是使用tf.identity(),几乎没有运行时成本:

with tf.name_scope("abc"):
    z = x + y
    z = tf.identity(z, name="z")
Run Code Online (Sandbox Code Playgroud)

但请注意,构建名称范围的推荐方法是将范围本身的名称分配给范围的"输出"(如果有单个输出操作):

with tf.name_scope("abc") as scope:
    # z will get the name "abc". x and y will have names in "abc/..." if they
    # are converted to tensors.
    z = tf.add(x, y, name=scope)
Run Code Online (Sandbox Code Playgroud)

这就是TensorFlow库的结构,它倾向于在TensorBoard中提供最佳的可视化.

  • 完全没有引用:我在TensorFlow团队工作,所以我帮助弥补了这个建议. (3认同)