您的位置:首页 > 运维架构

TensorFlow:作用域name_scope和variable_scope

2017-06-29 15:52 435 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息