【二】TensorFlow的变量管理

庞大的神经网络构建及训练过程中,需要对变量进行多次索引,tf框架本身设计了比较完善的变量管理机制。变量作用域能够更好的管理模块的变量(特别是将来会重用到的变量)。
tf常用的变量作用域有两种:tf.name_scope和tf.variable_scope,
tf本身的变量管理有如下接口:tf.get_variables 和 tf.Variable

tf.name_scope

主要与tf.Variable搭配使用,调用方式为:


tf.name_scope('scope_name') 
#or
tf.name_scope(named_scope)

以上两种方式均可。当传入字符串时,用以给变量名添加前缀,类似于目录,如下述代码所示;


import tensorflow as tf
 
# case 1:
with tf.name_scope('s1'):
	with tf.name_scope('s2'):
		w1 = tf.Variable([1,2,3], name='w')
		bias1 = tf.Variable([0.1], name='biases')
 
print(w1.name, bias1.name)
# >>> s1/s2/w:0 s1/s2/biases:0
 
# case 2:
with tf.name_scope('s1') as scope_1:
	w2 = tf.Variable([1,2,3], name='w')
	bias2 = tf.Variable([0.1], name='biases')
	with tf.name_scope('s2'):
		w3 = tf.Variable([1,2,3], name='w')
		bias3 = tf.Variable([0.1], name='biases')
		with tf.name_scope(scope_1):
			w4 = tf.Variable([1,2,3], name='w')
			bias4 = tf.Variable([0.1], name='biases')
 
print(w2.name, bias0.name, w3.name, bias3.name, w4.name, bias4.name)
# >>> s1_1/w:0 s1_1/biases:0 s1_1/s2_1/w:0 s1_1/s2_1/biases:0 s1_1/w1_1:0 s1_1/biases_1:0

当传入已存在的name_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的name_scope无关,如case2所示。
注意:当name_scope重名时,会自动加入后缀,variable也是如此。

tf.variable_scope

常与get_variable搭配使用,多用于变量共享,调用方式为:


tf.variable_scope('scope_name', reuse=None)
#or
tf.variable_scope(named_scope)

其中 reuse 参数可设为 None、tf.AUTO_REUSE、True、False;

与name_scope一样:当传入字符串时,用以给变量名添加前缀,类似于目录;
当传入已存在的variable_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的variable_scope无关。

resue作用域:当 reuse=None(默认情况)时,与上层variable_scope的reuse参数一样。

当 reuse=tf.AUTO_REUSE 时,自动复用,如果变量存在则复用,不存在则创建。这是最安全的用法。相当于python字典的get方法。


with tf.variable_scope('s1'):
	with tf.variable_scope('s2'):
		init = tf.constant_initializer(0.1)
		w1 = tf.get_variable('w', [2,2])
		bias1 = tf.get_variable('biases', [2,2])
print(w1.name, bias1.name)
# >>> s1/s2/w:0 s1/s2/biases_1:0
 
with tf.variable_scope('s1', reuse=tf.AUTO_REUSE):
	with tf.variable_scope('s2'):
		init = tf.constant_initializer(0.1)
		w2 = tf.get_variable('w', [2,2])
		bias2 = tf.get_variable('biases', [2,2])
print(w2.name, bias2.name)
# >>> s1/s2/w:0 s1/s2/biases_1:0

当 reuse=True 时,tf.get_variable会查找该命名变量,如果没有找到,则会报错;所以设置reuse=True之前,要保证该命名变量已存在。
下述代码如为第一次运行,当变量未定义,则会报错


with tf.variable_scope('s1', reuse=True):
	with tf.variable_scope('s2'):
		init = tf.constant_initializer(0.1)
		w1 = tf.get_variable('w', [2,2])
		bias1 = tf.get_variable('biases', [2,2])
# >>> ValueError: Variable s1/s2/w does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?

当 reuse=False 时,tf.get_variable会调用tf.Variable来创建变量,并检查创建的变量是否已存在,如果已存在,则报错。


with tf.variable_scope('s1', reuse=True):
	with tf.variable_scope('s2'):
		init = tf.constant_initializer(0.1)
		w1 = tf.get_variable('w', [2,2])
		bias1 = tf.get_variable('biases', [2,2])
# first run
# >>> s1/s2/w:0 s1/s2/biases:0

# second run
# >>> ValueError: Variable s1/s2/w already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? 

上述代码第一次运行正确,第二次会发现变量已存在,继而报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值