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

编程入门 行业动态 更新时间:2024-10-11 23:16:57
本文介绍了如何重命名尊重名称范围的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

鉴于 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 05:09:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   重命名   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!