参考自tf2.1
官方文档:
https://www.tensorflow.org/api_docs/python/tf/name_scope
A context manager for use when defining a Python op.
用于定义Python操作op的上下文管理器
tf.name_scope(
name
)
This context manager pushes a name scope, which will make the name of all operations added within it have a prefix.
这个上下文管理器将创建一个名字作用域(或翻译为范围、或翻译为上下文,不太明白的可以看例子)(scope),使得在其中的所有(operations)操作,都加上一个给定的前缀
For example, to define a new Python op called my_op:
举个例子,我们定义一个叫my_op的Python操作(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`.(在接下来的代码中,
# 可以定义一些使用a, b, 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
(注:即在前原当定义名字中加上前缀)
If the scope name already exists, the name will be made unique by appending `_n`. For example, calling `my_op` the second time will generate `MyOp_1/a`, etc.
如果在当前命名域(scope)内名称已经存在,则通过追加将使名称唯一 _n
。
例如,若MyOp/a
已经存在,在将生成:MyOp_1/a
,若仍然存在,则生成MyOp_2/a
,直到不重名为止