先看代码:
-
-
-
-
-
with tf.variable_scope("foo"):
-
a = tf.get_variable("bar", [1])
-
-
b = tf.Variable("b", [1])
-
-
-
with tf.variable_scope("bar"):
-
a = tf.get_variable("bar", [1])
-
-
b = tf.Variable("b", [1])
-
-
-
-
-
-
-
-
b = tf.get_variable("b", [1])
-
-
-
-
c = tf.get_variable("b", [1])
-
可以看出,对于tf.Variable()函数,两者的使用情况都一样;而tf.get_variable()函数,它不受name_scope约束,已经声明过的变量就不能再声明了。
1. tf.name_scope('scope_name')或tf.name_scope(named_scope)
主要与tf.Variable搭配使用;
当传入字符串时,用以给变量名添加前缀,类似于目录,如case1所示;
当传入已存在的name_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的name_scope无关,如case2所示。
- import tensorflow as tf
-
- with tf.name_scope('l1'):
- with tf.name_scope('l2'):
- wgt1 = tf.Variable([1,2,3], name='wgts')
- bias1 = tf.Variable([0.1], name='biases')
-
- print wgt1.name, bias1.name
-
- with tf.name_scope('l1') as l1_scp:
- with tf.name_scope('l2'):
- wgt0 = tf.Variable([1,2,3], name='wgts')
- bias0 = tf.Variable([0.1], name='biases')
- with tf.name_scope(l1_scp):
- wgt1 = tf.Variable([1,2,3], name='wgts')
- bias1 = tf.Variable([0.1], name='biases')
-
- print wgt0.name, bias0.name, wgt1.name, bias1.name
2. tf.variable_scope('scope_name', reuse=None)或
tf.variable_scope(named_scope)
与name_scope一样:当传入字符串时,用以给变量名添加前缀,类似于目录;
当传入已存在的variable_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的variable_scope无关。
常于get_variable搭配使用,多用于变量共享;其中 reuse 参数可设为 None、tf.AUTO_REUSE、True、False;
当 reuse=None(默认情况)时,与上层variable_scope的reuse参数一样。
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
-
- print wgt1.name, bias1.name
当 reuse=tf.AUTO_REUSE 时,自动复用,如果变量存在则复用,不存在则创建。这是最安全的用法。
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
-
- with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt2 = tf.get_variable('wgts', [2,2])
- bias2 = tf.get_variable('biases', [2,2])
- print wgt2.name, bias2.name
- with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt2 = tf.get_variable('wgts', [2,2])
- bias2 = tf.get_variable('biases', [2,2])
-
- print wgt2.name, bias2.name
当 reuse=True 时,tf.get_variable会查找该命名变量,如果没有找到,则会报错;所以设置reuse=True之前,要保证该命名变量已存在。
- with tf.variable_scope('lv1', reuse=True):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
-
- print wgt1.name, bias1.name
命名变量已存在:
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
-
- print wgt1.name, bias1.name
-
- with tf.variable_scope('lv1', reuse=True):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
-
- print wgt1.name, bias1.name
当 reuse=False 时,tf.get_variable会调用tf.Variable来创建变量,并检查创建的变量是否以存在,如果已存在,则报错;
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
-
- print wgt1.name, bias1.name
-
- with tf.variable_scope('lv1', reuse=False):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
-
- print wgt1.name, bias1.name