tf.name_scope()用法

转载自:https://www.jianshu.com/p/0ccffe98d1ef

tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。

Signature: tf.name_scope(*args, **kwds)
Docstring:
Returns a context manager for use when defining a Python op.

# 也就是说,它的主要目的是为了更加方便地管理参数命名。
# 与 tf.Variable() 结合使用。简化了命名
with tf.name_scope('conv1') as scope:
    weights1 = tf.Variable([1.0, 2.0], name='weights')
    bias1 = tf.Variable([0.3], name='bias')

# 下面是在另外一个命名空间来定义变量的
with tf.name_scope('conv2') as scope:
    weights2 = tf.Variable([4.0, 2.0], name='weights')
    bias2 = tf.Variable([0.33], name='bias')

# 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突
print weights1.name
print weights2.name
conv1/weights:0
conv2/weights:0
# 注意,这里的 with 和 python 中其他的 with 是不一样的
# 执行完 with 里边的语句之后,这个 conv1/ 和 conv2/ 空间还是在内存中的。这时候如果再次执行上面的代码
# 就会再生成其他命名空间
with tf.name_scope('conv1') as scope:
    weights1 = tf.Variable([1.0, 2.0], name='weights')
    bias1 = tf.Variable([0.3], name='bias')

with tf.name_scope('conv2') as scope:
    weights2 = tf.Variable([4.0, 2.0], name='weights')
    bias2 = tf.Variable([0.33], name='bias')

print weights1.name
print weights2.name
conv1_1/weights:0
conv2_1/weights:0
发布了44 篇原创文章 · 获赞 0 · 访问量 1911

猜你喜欢

转载自blog.csdn.net/weixin_39331401/article/details/104530311