TensorFlow:作用域name_scope和variable_scope

本文详细介绍了TensorFlow中名称作用域(name_scope)和变量作用域(variable_scope)的使用方法及区别。name_scope主要用于组织和命名操作节点,而variable_scope则用于管理和共享变量。通过实例演示了如何使用这两种作用域来提高TensorBoard中的可视化效果。

Tensorflow中的名称作用域和变量作用域

简单来说name_scope是给Op_name加前缀的,variable_scope是给变量variable_name和Op_name加前缀的.作用域在使用Tensorboard对Graph对象进行可视化的时候很有帮助,作用域会把一些Op划分到较大的语句块当中.使用tensorboard可视化数据流图的时候,每个作用域都对自己的Op进行封装,从而获得更好的可视化效果.

name_scope

基本用法是把Op添加到with tf.name_scope()语句块当中

import tensorflow as tf
# 接下来在默认图中,划分作用域,并添加Op
with tf.name_scope('A'):
    a = tf.add(1, 2, name='A_add')
    b = tf.subtract(5, a, name='A_sub')
# 作用域B
with tf.name_scope('B'):
    c = tf.add(3, 4, name='B_add')
    d = tf.subtract(c, 5, name='B_sub')
# 在作用域外部,添加Op
e = tf.add(b, d, name='output')

#使用TensorBoard对计算图进行可视化,创建一个Summary对象,传入路径参数和图对象
writer = tf.summary.FileWriter('./name_scope_2', graph=tf.get_default_graph())   
writer.close()

Variable变量的作用域

tf.varibale_scope() #变量指定命名空间
tf.get_variable(name, shape, dtype, initializer) # 通过标识名参数创建或返回一个变量

# tf.get_variable_scope().reuse == False时,(默认为False)变量作用域只能用来创建新的变量,
with tf.variable_scope('var'):
    v = tf.get_variable('v1', [1])
    v2 = tf.get_variable('v2', [1])
# tf.get_variable_scope().reuse == True时,作用域可以共享变量
#with tf.variable_scope('var2') as scope:
    #v = tf.get_variable('v', [1])
# 或者
with tf.variable_scope('var3', reuse=True):  # scope.reuse_variables()
    v3 = tf.get_variable('v3', [1])
---------------------------------------------------------------------------

ValueError                       Traceback (most recent call last)

<ipython-input-29-df5e8f9929d5> in <module>()
----> 6     v3 = tf.get_variable('v3', [1])

ValueError: Variable var3/v3 does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
# 获取变量的作用域
with tf.variable_scope('var4') as var4_scope:
    v4 = tf.get_variable('v', [1])
    with tf.variable_scope('var5') as var5_scope:
        v5 = tf.get_variable('w', [1])
assert var4_scope.name == 'var4'
assert v4.name == 'var4/v:0'
assert var5_scope.name == 'var4/var5'
assert v5.name == 'var4/var5/w:0'

变量作用域的初始化

变量作用域可以默认携带一个初始化器,在这个作用域中的子作用域或者变量都可以继承或者重新父作用域的初始化器
with tf.variable_scope('v1', initializer=tf.constant_initializer(0.4)):
    a  = tf.get_variable('a', [1])
    #assert a.eval() == 0.4   # a被作用域初始化为0.4
    b  = tf.get_variable('b', [1], initializer=tf.constant_initializer(0.3))
    #assert b.eval() == 0.3   # 复写初始化器的值
    with tf.variable_scope('v2'):
        a = tf.get_variable('a', [1])
        # assert a.eval() == 0.4    继承第一个初始化器
---------------------------------------------------------------------------

ValueError                     Traceback (most recent call last)

<ipython-input-32-c3e093686d98> in <module>()
----> 5     b  = tf.get_variable('b', [1], initializer=tf.constant_initializer(0.3))

ValueError: Variable v1/b does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
name_scope示例
name_scope的作用域范围会影响到Op_name,但是不会作用到tf.get_variable()创建的变量
会作用到tf.Variable()创建的变量
 with tf.variable_scope('funny',reuse=True):
    with tf.name_scope('haha'):
        e = tf.get_variable('E', [1])
        # name参数,给op指定标识
        d = tf.Variable(tf.zeros([1]), name='zero')
        f = 1 + e  # 等价于 f = tf.add(1, e)

print 'e.name    '   ,e.name 
print 'd.name    '   ,d.name 
print 'd.op.name ',d.op.name
print'f.name     '   ,f.name
print'f.op.name  ',f.op.name
e.name     funny/E:0
d.name     funny_8/haha/zero:0
d.op.name  funny_8/haha/zero
f.name      funny_8/haha/add:0
f.op.name   funny_8/haha/add
funny_数字,这个数字没执行一次,获取作用域的操作都会+1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值