1.简介
对比分析tf.Variable / tf.get_variable | tf.name_scope / tf.variable_scope的异同
2.说明
- tf.Variable创建变量;tf.get_variable创建与获取变量
- tf.Variable自动检测命名冲突并且处理;tf.get_variable在没有设置reuse时会报错
- tf.name_scope没有reuse功能,tf.get_variable在变量冲突时报错;tf.variable_scope有reuse功能,可配合tf.get_variable实现变量共享
- tf.get_variable变量命名不受tf.name_scope的影响;tf.Variable受两者的影响
3.代码示例
3.1 tf.Variable
tf.Variable在命名冲突时自动处理冲突问题
1 import tensorflow as tf 2 a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 3 a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 4 print(a1) 5 print(a2) 6 print(a1==a2) 7 8 9 ### 10 <tf.Variable 'a:0' shape=(1,) dtype=float32_ref> 11 <tf.Variable 'a_1:0' shape=(1,) dtype=float32_ref> 12 False
3.2 tf.get_variable
tf.get_variable在没有设置命名空间reuse的情况下变量命名冲突时报错
1 import tensorflow as tf 2 a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 3 a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 4 5 6 ### 7 ValueError: Variable a already exists, disallowed. 8 Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
3.3 tf.name_scope
tf.name_scope没有reuse功能,tf.get_variable命名不受它影响,并且命名冲突时报错;tf.Variable命名受它影响
1 import tensorflow as tf 2 a = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 3 with tf.name_scope('layer2'): 4 a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 5 a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 6 a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 7 # a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 该句会报错 8 print(a) 9 print(a1) 10 print(a2) 11 print(a3) 12 print(a1==a2) 13 14 15 ### 16 <tf.Variable 'a:0' shape=(1,) dtype=float32_ref> 17 <tf.Variable 'layer2/a:0' shape=(1,) dtype=float32_ref> 18 <tf.Variable 'layer2/a_1:0' shape=(1,) dtype=float32_ref> 19 <tf.Variable 'a_1:0' shape=(1,) dtype=float32_ref> 20 False
3.4 tf.variable_scope
tf.variable_scope可以配tf.get_variable实现变量共享;reuse默认为None,有False/True/tf.AUTO_REUSE可选:
- 设置reuse = None/False时tf.get_variable创建新变量,变量存在则报错
- 设置reuse = True时tf.get_variable只讲获取已存在的变量,变量不存在时报错
- 设置reuse = tf.AUTO_REUSE时tf.get_variable在变量已存在则自动复用,不存在则创建
1 import tensorflow as tf 2 with tf.variable_scope('layer1',reuse=tf.AUTO_REUSE): 3 a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 4 a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 5 a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 6 a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 7 print(a1) 8 print(a2) 9 print(a1==a2) 10 print(a3) 11 print(a4) 12 print(a3==a4) 13 14 15 ### 16 <tf.Variable 'layer1_1/a:0' shape=(1,) dtype=float32_ref> 17 <tf.Variable 'layer1_1/a_1:0' shape=(1,) dtype=float32_ref> 18 False 19 <tf.Variable 'layer1/a_2:0' shape=(1,) dtype=float32_ref> 20 <tf.Variable 'layer1/a_2:0' shape=(1,) dtype=float32_ref> 21 True
!!!