1. Introduction
Comparison and Analysis of differences between TF. Variable/TF. get_variable | TF. name_scope/TF. variable_scope
2. Description
- TF. Variable: create variable; TF. get_variable: Create and obtain variable
- TF. Variable automatically detects and processes name conflicts. TF. get_variable reports an error when reuse is not set.
- TF. name_scope does not have the reuse function. TF. get_variable returns an error in variable conflict. TF. variable_scope has the reuse function, which can be used with TF. get_variable to share variables.
- TF. get_variable variable names are not affected by TF. name_scope; TF. variable is affected by both.
3. Sample Code
3.1 TF. Variable
TF. Variable automatically handles conflicts during name conflicts
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: an error is returned when the namespace reuse is not set.
1 import tensorflow as tf2 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 does not have the reuse function. The name of TF. get_variable is not affected by this function, and an error is returned when a name conflict occurs. The name of TF. variable is affected by this function.
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) the following error occurs: 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 can be used with TF. get_variable to share variables. The default value of reuse is none, and false/true/TF. auto_reuse is optional:
- When reuse = none/false is set, Tf. get_variable creates a new variable. If a variable exists, an error is returned.
- When reuse = true is set, Tf. get_variable only obtains the existing variables. If the variable does not exist, an error is returned.
- When reuse = TF. auto_reuse is set, Tf. get_variable is automatically reused if the variable already exists. If the variable does not exist, it is created
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 False19 <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
!!!
Namespace and variable naming in tensorflow