variable_scope和name_scope差别

先看代码:

  1.  
    #命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用
  2.  
     
  3.  
    import tensorflow as tf
  4.  
     
  5.  
    with tf.variable_scope("foo"):
  6.  
    a = tf.get_variable( "bar", [1])
  7.  
    print(a.name) #foo/bar:0
  8.  
    b = tf.Variable( "b", [1])
  9.  
    print(b.name) #foo/Variable:0
  10.  
     
  11.  
    with tf.variable_scope("bar"):
  12.  
    a = tf.get_variable( "bar", [1])
  13.  
    print(a.name) #bar/bar:0
  14.  
    b = tf.Variable( "b", [1])
  15.  
    print(b.name) #bar/Variable:0
  16.  
     
  17.  
    with tf.name_scope("a"):
  18.  
    a = tf.Variable([ 1])
  19.  
    print(a.name) #a/Variable:0
  20.  
     
  21.  
    with tf.name_scope("b"):
  22.  
    b = tf.get_variable( "b", [1])
  23.  
    print(b.name) #b_1:0
  24.  
     
  25.  
    with tf.name_scope("b"):
  26.  
    c = tf.get_variable( "b", [1])
  27.  
    print(c.name) #出错,b已存在

可以看出,对于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所示。

[python]  view plain  copy
 
  1. import tensorflow as tf  
  2.   
  3. # case 1:  
  4. with tf.name_scope('l1'):  
  5.     with tf.name_scope('l2'):  
  6.         wgt1 = tf.Variable([1,2,3], name='wgts')  
  7.         bias1 = tf.Variable([0.1], name='biases')  
  8.   
  9. print wgt1.name, bias1.name  
  10. # >>> l1/l2/wgts:0 l1/l2/biases:0  
  11.   
  12. # case 2:  
  13. with tf.name_scope('l1') as l1_scp:  
  14.     with tf.name_scope('l2'):  
  15.         wgt0 = tf.Variable([1,2,3], name='wgts')  
  16.         bias0 = tf.Variable([0.1], name='biases')  
  17.         with tf.name_scope(l1_scp):  
  18.             wgt1 = tf.Variable([1,2,3], name='wgts')  
  19.             bias1 = tf.Variable([0.1], name='biases')  
  20.   
  21. print wgt0.name, bias0.name, wgt1.name, bias1.name  
  22. # >>> l1_1/l2/wgts:0 l1_1/l2/biases:0 l1_1/wgts:0 l1_1/biases:0  

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参数一样。

[python]  view plain  copy
 
  1. # case 1  
  2. with tf.variable_scope('lv1'):  
  3.     with tf.variable_scope('lv2'):  
  4.         init = tf.constant_initializer(0.1)  
  5.         wgt1 = tf.get_variable('wgts', [2,2])  
  6.         bias1 = tf.get_variable('biases', [2,2])  
  7.   
  8. print wgt1.name, bias1.name  
  9. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0  

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

[python]  view plain  copy
 
  1. with tf.variable_scope('lv1'):  
  2.     with tf.variable_scope('lv2'):  
  3.         init = tf.constant_initializer(0.1)  
  4.         wgt1 = tf.get_variable('wgts', [2,2])  
  5.         bias1 = tf.get_variable('biases', [2,2])  
  6. print wgt1.name, bias1.name  
  7. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0  
  8.   
  9. with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):  
  10.     with tf.variable_scope('lv2'):  
  11.         init = tf.constant_initializer(0.1)  
  12.         wgt2 = tf.get_variable('wgts', [2,2])  
  13.         bias2 = tf.get_variable('biases', [2,2])  
  14. print wgt2.name, bias2.name  
  15. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0  
[python]  view plain  copy
 
  1. with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):  
  2.     with tf.variable_scope('lv2'):  
  3.         init = tf.constant_initializer(0.1)  
  4.         wgt2 = tf.get_variable('wgts', [2,2])  
  5.         bias2 = tf.get_variable('biases', [2,2])  
  6.   
  7. print wgt2.name, bias2.name  
  8. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0  

当 reuse=True 时,tf.get_variable会查找该命名变量,如果没有找到,则会报错;所以设置reuse=True之前,要保证该命名变量已存在。

[python]  view plain  copy
 
  1. with tf.variable_scope('lv1', reuse=True):  
  2.     with tf.variable_scope('lv2'):  
  3.         init = tf.constant_initializer(0.1)  
  4.         wgt1 = tf.get_variable('wgts', [2,2])  
  5.         bias1 = tf.get_variable('biases', [2,2])  
  6.   
  7. print wgt1.name, bias1.name  
  8. # >>> ValueError: Variable lv1/lv2/wgts does not exist,   
  9. # or was not created with tf.get_variable(). Did you mean   
  10. # to set reuse=tf.AUTO_REUSE in VarScope?  
命名变量已存在:
[python]  view plain  copy
 
  1. with tf.variable_scope('lv1'):  
  2.     with tf.variable_scope('lv2'):  
  3.         init = tf.constant_initializer(0.1)  
  4.         wgt1 = tf.get_variable('wgts', [2,2])  
  5.         bias1 = tf.get_variable('biases', [2,2])  
  6.   
  7. print wgt1.name, bias1.name  
  8. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0  
  9.   
  10. # case 2  
  11. with tf.variable_scope('lv1', reuse=True):  
  12.     with tf.variable_scope('lv2'):  
  13.         init = tf.constant_initializer(0.1)  
  14.         wgt1 = tf.get_variable('wgts', [2,2])  
  15.         bias1 = tf.get_variable('biases', [2,2])  
  16.   
  17. print wgt1.name, bias1.name  
  18. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0  

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

[python]  view plain  copy
 
  1. with tf.variable_scope('lv1'):  
  2.     with tf.variable_scope('lv2'):  
  3.         init = tf.constant_initializer(0.1)  
  4.         wgt1 = tf.get_variable('wgts', [2,2])  
  5.         bias1 = tf.get_variable('biases', [2,2])  
  6.   
  7. print wgt1.name, bias1.name  
  8. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0  
  9.   
  10. # case 2  
  11. with tf.variable_scope('lv1', reuse=False):  
  12.     with tf.variable_scope('lv2'):  
  13.         init = tf.constant_initializer(0.1)  
  14.         wgt1 = tf.get_variable('wgts', [2,2])  
  15.         bias1 = tf.get_variable('biases', [2,2])  
  16.   
  17. print wgt1.name, bias1.name  
  18. # ValueError: Variable lv1/lv2/wgts already exists, disallowed.   
  19. # Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?   

猜你喜欢

转载自www.cnblogs.com/DjangoBlog/p/9235023.html