tf.name_scope()与tf.variable_scope()

        TensorFlow的tf.name_scope()、tf.variable_scope()是两个作用域函数,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。常用于:1)变量共享;2)tensorboard画流程图进行可视化封装变量。

       命名域 (name scope),通过tf.name_scope 或 tf.op_scope创建;
       变量域 (variable scope),通过tf.variable_scope 或 tf.variable_op_scope创建。
       通俗理解就是:tf.name_scope()、tf.variable_scope()会在模型中开辟各自的空间,而其中的变量均在这个空间内进行管理,但是之所以有两个,主要还是有着各自的区别。

     (1)tf.Variable() :每次都会新建变量。

     (2)tf.get_variable():如果希望重用(共享)一些变量,就需要用到tf.get_variable(),它会去搜索变量名,有就直接用,没有再新建。拥有一个变量检查机制,会检测已经存在的变量是否设置为共享变量,如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错。

1. tf.name_scope(‘scope_name’)

tf.name_scope 主要结合 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
'''

2.下面来看看 tf.variable_scope(‘scope_name’)

     tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现 变量共享。

# 这里是正确的打开方式~~~可以看出,name 参数才是对象的唯一标识

import tensorflow as tf
with tf.variable_scope('v_scope') as scope1:
    Weights1 = tf.get_variable('Weights', shape=[2,3])
    bias1 = tf.get_variable('bias', shape=[3])

# 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
    Weights2 = tf.get_variable('Weights')

print(Weights1.name)
print(Weights2.name)

# 可以看到这两个引用名称指向的是同一个内存对象

'''
v_scope/Weights:0
v_scope/Weights:0
'''

也可以结合 tf.Variable() 一块使用:

import tensorflow as tf
# 注意: bias1 的定义方式
with tf.variable_scope('v_scope') as scope1:
    Weights1 = tf.get_variable('Weights', shape=[2,3])
  # bias1 = tf.Variable([0.52], name='bias')

# 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
    Weights2 = tf.get_variable('Weights')
    bias2 = tf.Variable([0.52], name='bias')

print(Weights1.name)
print(Weights2.name)
print(bias2.name)

'''
v_scope/Weights:0
v_scope/Weights:0
v_scope_1/bias:0
'''

 如果 reuse=True 的scope中的变量没有已经定义,会报错!!

import tensorflow as tf
# 注意, bias1 的定义方式
with tf.variable_scope('v_scope') as scope1:
    Weights1 = tf.get_variable('Weights', shape=[2,3])
    bias1 = tf.Variable([0.52], name='bias')  # tf.Variable不会报错

print(Weights1.name)
print(bias1.name)

'''
v_scope/Weights:0
v_scope/bias:0
'''

# 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
    Weights2 = tf.get_variable('Weights')
    bias2 = tf.get_variable('bias', [1])  # tf.get_variable会报错

print(Weights2.name)
print(bias2.name)

# 这样会报错
# Variable v_scope/bias does not exist, or was not created with tf.get_variable()

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满腹的小不甘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值