TensorFlow中的name_scope和variable_scope的使用

一、概要

tf.name_scope()      #主要是方便参数变量的“ 分组 ”和 “ 管理 ”,主要是结合tf.Variable()一起使用

tf.variable_scope()  #一方面也是可以实现变量的“ 分组 ”和“ 管理 ”,主要是结合tf.get_variable()一起使用

tf.Variable()             #创建一个全新的变量,由于tf.Variable() 每次都在创建新对象,所有reuse=True 和它并没有什么关系。                                   对   于get_variable(),来说,如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的                                       话,  就创建一个新的

tf.get_variable()      #创建共享变量

二、tf.name_scope()的使用

1、tf.name_scope()结合tf.Variable() 的使用

import tensorflow as tf

with tf.name_scope('left_add'):
    a_add_left=tf.Variable(initial_value=10,name='a_add')
    b_add_left=tf.Variable(initial_value=20,name='b_add')
    c_result=tf.add(a_add_left,b_add_left,name='c_result')   #同名变量

print(c_result.name)
print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

with tf.name_scope('left_add'):    
    a_add_right=tf.Variable(initial_value=30,name='a_add')
    b_add_right=tf.Variable(initial_value=40,name='b_add')
    c_result=tf.add(a_add_right,b_add_right,name='c_result') #同名变量

print(c_result.name)
print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')


with tf.Session() as sess:
    writer=tf.summary.FileWriter('name_scope_graph',graph=sess.graph)

运行结果为:

left_add/c_result:0
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
left_add_1/c_result:0
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

结论:

(1)因为name_scope的主要作用就是变量的分类与管理,即使两个name_scopede名称一样,依然没有关系,会默认为后面的增加一个序号1、2、3、。。。。依次为:eft_add 、eft_add_1、eft_add_2、eft_add_3、eft_add_4.

(2)因为name_scope会相当于给每个区域制定一个完全不同的名字(即使名字相同,但后缀不同),所以不同的name_scope里面定义名称相同的变量完全不会有问题,因为每一个name_scope里面的变量名称会添加name_scope的名称作为前缀,如上的left_add/c_result:0  和  left_add_1/c_result:0

如果将上面的第二个name_scope改为如下:

with tf.name_scope('left_add/'):  #添加一个斜杠,表示name_scope名称不再逐渐递增,按原样显示

此时显示的结果为:

left_add/c_result:0
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
left_add/c_result_1:0
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

结论:

(1)当name_scope按照原名称显示的时候,不同name_scope里面的同名变量又会采用“ 逐渐递增 ”的形式,如c_result_1 、c_result_2、c_result_3、c_result_3. 即总是会保证每个变量的唯一性,即不管是name_scope相同还是变量名相同,都是不会报错的。

(2)因为name_scope的作用主要就是变量的分类管理,在编程时最好不要将每个name_scope的名称设置为相同,否则就是去了“ 分类管理 ”本身的初衷。

2、tf.name_scope()结合tf.get_variable() 的使用

前面的使用tf.Variable()无论是在同一个name_scope中定义同名变量,还是在不同的name_scope中定义同名变量,都不会出错,因为tf.Variable()创建的是以全新的变量,不是共享的,故而名称会以序号的形式“ 逐渐递增 ”,但是与tf.get_variable() 的使用就不一样了,因为tf.get_variable() 时创建的 共享变量。

(1)在name_scope中使用tf.get_variable() 创建不同名的变量,如下:

import tensorflow as tf

with tf.name_scope('name_scope_1'):
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
    var2 = tf.get_variable(name='var2', shape=[1], dtype=tf.float32)  #不同名
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var1.name, sess.run(var1))
    print(var2.name, sess.run(var2))

输出结果为:

var1:0 [-1.5012715]
var2:0 [-0.37785244]

以为本来名称就不同,故而不会错误。而且我们发现,他不显示name_scope的前缀了。

(2)在name_scope中使用tf.get_variable() 创建同名的变量,如下:

import tensorflow as tf

with tf.name_scope('name_scope_1'):
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
    var2 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)  #同名
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var1.name, sess.run(var1))
    print(var2.name, sess.run(var2))

运行结果:Variable var1 already exists, disallowed.

总结:可以在name_scope创建同名的非共享变量,但是不能创建同名的共享变量。如果要创建同名的共享变量,该怎么办呢?

            则使用tf.get_variable()和tf.variable_scope()相结合

三、tf.variable_scope()的使用

如下面的代码:


import tensorflow as tf

with tf.variable_scope('variable_scope') as scope:
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32)
    print(var1.name)

    scope.reuse_variables()  # 设置共享变量

    var1 = tf.get_variable(name='var1')
    print(var1.name)

    var2 = tf.Variable(initial_value=[2.], name='var2', dtype=tf.float32)
    print(var2.name)

    var2 = tf.Variable(initial_value=[2.], name='var2', dtype=tf.float32)
    print(var2.name)

打印结果如下:

variable_scope/var1:0
variable_scope/var1:0
variable_scope/var2:0
variable_scope/var2_1:0

结论:从上面的例子可以看出,在同一个variable_scope里面,可以丁一两个两个完全同名的变量var1,即使变量名称和属性name的名称完全一样,依然不会出错,因为使用tf.get_variable()创建的是共享变量。当然使用tf.Vriable()自然没有问题,因为它创建的本身就是完全不一样的变量,依然会遵循前面的变量名按顺序逐渐递增的形式。

(1)使用同名变量是要进行相关的设置的。下面会有几种方式:

      方式一:scope.reuse_variables()   # 设置共享变量

      方式二:

with tf.variable_scope('foo') as foo_scope:
    v = tf.get_variable('v', [1])
with tf.variable_scope('foo', reuse=True):  #设置相同名称的foo,
    v1 = tf.get_variable('v')
assert v1 == v

      方式三:

with tf.variable_scope('foo') as foo_scope:
    v = tf.get_variable('v', [1])
with tf.variable_scope(foo_scope, reuse=True):  #直接指定前面的那个variable_scope
    v1 = tf.get_variable('v')
assert v1 == v

四、总结

不管是在name_scope还是在variable_scope里面,tf.Variable()的作用都是一样的;但是tf.get_variable()并不是不能在name_scope里面使用,创建非同名变量(注意这里的非同名指的是name指定的名称不一样)依然是可以的,只是不能创建同名变量;要创建共享变量,需要将variable_scope和tf.get_variable()结合起来使用。如下:

tf.get_variable()可以重新使用一个已经存在的同名变量或者重新创建一个新的变量。比如:

a = tf.get_variable(name='v', shape=[1])

在Python中这个变量保存为a,而在TensorFlow中这个变量保存为v。如果进一步定义变量b

b = tf.get_variable(name='v', shape=[1])
# ValueError: Variable v already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

出现这个错误的原因在于,在使用TensorFlow中一个已经定义的变量时,需要申明reuse。为了重复使用tf.get_variable()定义的变量,必须在variable_scope中声明reuse

with tf.variable_scope('one'):
    a = tf.get_variable('v', [2])
with tf.variable_scope('one', reuse=True):
    c = tf.get_variable('v', [2])

即创建名称name同为 “v” 的变量,需要使用tf.variable_scope(reuse=True),而且需要指定reuse参数为True。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值