如何重命名尊重名称范围的变量? [英] How to rename a variable which respects the name scope?

查看:16
本文介绍了如何重命名尊重名称范围的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given x, y are tensors, I know I can do

with tf.name_scope("abc"):
    z = tf.add(x, y, name="z")

所以 z 被命名为 "abc/z".

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

I am wondering if there exists a function f which assign the name directly in the following case:

with tf.name_scope("abc"):
    z = x + y
    f(z, name="z")

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

The stupid f I am using now is z = tf.add(0, z, name="z")

推荐答案

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

If you want to "rename" an op, there is no way to do that directly, because a tf.Operation (or tf.Tensor) is immutable once it has been created. The typical way to rename an op is therefore to use tf.identity(), which has almost no runtime cost:

with tf.name_scope("abc"):
    z = x + y
    z = tf.identity(z, name="z")

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

Note however that the recommended way to structure your name scope is to assign the name of the scope itself to the "output" from the scope (if there is a single output op):

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)

这是 TensorFlow 库的结构方式,它往往会在 TensorBoard 中提供最佳的可视化效果.

This is how the TensorFlow libraries are structured, and it tends to give the best visualization in TensorBoard.

这篇关于如何重命名尊重名称范围的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆