简体   繁体   English

了解 tf.name_scope

[英]Understanding tf.name_scope

I am trying to understand tf.name_scope.我想了解 tf.name_scope。 The documentation mentions the following:该文档提到以下内容:

"This context manager pushes a name scope, which will make the name of all operations added within it have a prefix. “这个上下文管理器会推送一个名称 scope,这将使添加到其中的所有操作的名称都有一个前缀。

For example, to define a new Python op called my_op:例如,要定义一个名为 my_op 的新 Python op:

def my_op(a, b, c, name=None):
  with tf.name_scope("MyOp") as scope:
    a = tf.convert_to_tensor(a, name="a")
    b = tf.convert_to_tensor(b, name="b")
    c = tf.convert_to_tensor(c, name="c")
    # Define some computation that uses `a`, `b`, and `c`.
    return foo_op(..., name=scope)

When executed, the Tensors a, b, c, will have names MyOp/a, MyOp/b, and MyOp/c."执行时,张量 a、b、c 将具有名称 MyOp/a、MyOp/b 和 MyOp/c。”

My understanding is that the with block does not introduce a new local scope in Python.我的理解是 with 块不会在 Python 中引入新的本地 scope。 Under normal situation, the tensor variable a will also refer to the local parameter a of function my_op.一般情况下,张量变量a也会引用function my_op的局部参数a。 How is the name prefixing with "MyOp/" implemented using Python context?如何使用 Python 上下文实现以“MyOp/”为前缀的名称? In the source code link for tf.name_scope ( https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/framework/ops.py#L6423-L6442 ) there is an invocation of在 tf.name_scope 的源代码链接( https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/framework/ops.py#L6423-L6442 )中有一个调用

ctx = context.context()

but I could not find the semantics of context.context().但我找不到 context.context() 的语义。 Most context manager discussion talk about enter and exit , but no mention of variable renaming with some prefix.大多数上下文管理器讨论都讨论了enterexit ,但没有提到带有一些前缀的变量重命名。 Is this some introspective mechanism in Python that allows the manipulation of Python variable scopes?这是 Python 中允许操纵 Python 变量范围的一些内省机制吗? Many thanks for any insights.非常感谢您的任何见解。

Name scopes are entirely for naming convenience.名称范围完全是为了命名方便。 It has nothing to do with python scope.它与 python scope 无关。 Whenever you perform an operation in tensorflow it will get a name according to some state maintained by tensorflow, but if you want those operations to be named clearly for your use case then name scope is used. Whenever you perform an operation in tensorflow it will get a name according to some state maintained by tensorflow, but if you want those operations to be named clearly for your use case then name scope is used. Any operation within the name scope will have the provided value appended in it's name, there is no use more than that.名称 scope 中的任何操作都会在其名称中附加提供的值,除此之外没有其他用处。 Regarding the implementation, I highly doubt that those scope implementations will be exposed in python.关于实现,我非常怀疑那些 scope 实现会在 python 中公开。 All tensor creation operations are executed by C++ backend of tensorflow, so scopes should be mostly handled by them所有张量创建操作都由 tensorflow 的 C++ 后端执行,因此范围应该主要由它们处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM