是否可以在 TensorFlow 中重新输入现有的名称范围?

And*_*bis 1 python tensorflow

两次输入同名的名称范围:

c = tf.constant(1)
with tf.name_scope("test"):
    a = tf.add(c, c)
with tf.name_scope("test"):
    b = tf.add(a, a)
Run Code Online (Sandbox Code Playgroud)

导致创建两个名称范围:testtest_1.

是否可以在单独的上下文管理器中重新输入范围而不是创建新的范围?

And*_*bis 5

事实证明,这实际上很容易。查看framework/ops.pyTensorFlow 发现,在范围名称中添加“/”不会使范围名称唯一,从而有效地重新进入现有范围。例如:

c = tf.constant(1)
with tf.name_scope("test"):
    a = tf.add(c, c)
with tf.name_scope("test/"):
    b = tf.add(a, a)
Run Code Online (Sandbox Code Playgroud)