前一节讲变量
tf.get_variable和tf.Variable新建变量的区别,区别在于重复定义变量时,计算机是否自动更改变量属性名,tf.variable会自动更改属性名加‘_n’,tf.get_variable就会报错。
本节讲作用域
结论1:变量名就是一个名字,变量的name属性是地址
a = tf.Variable(3.0,dtype=tf.float32)
print(a.name)
输出:Variable_1:0
定义了变量,变量名是a,name属性为Variable_1:0,就是一个地址
结论2:tf.variable_scope('scope1')修订了块内的变量name属性,相当于新找了个地址存储变量值
with tf.variable_scope('scope1'):
a = tf.Variable(3.0,dtype=tf.float32)
print(a.name)
输出:scope1/Variable:0
结论3:tf.name_scope('scope1')未修订了块内的变量name属性,未开辟空间存储变量
with tf.name_scope('scope2'):
b = tf.get_variable(initializer=3.0,dtype=tf.float32,name='b')
print(b.name)
输出:b:0
注:若name_scope对变量name属性有影响,那么输出应该为scope2/b:0
结论4:tf.name_scope有点类似于 b = a ,重新定义了名字,而且这个名字是给tensorboard用的,用以分块;未考虑tensorboard时,跟删掉没什么差别
with tf.name_scope('scope2'):
with tf.variable_scope('v_scope'):
b = tf.get_variable(initializer=3.0,dtype=tf.float32,name='b')
print(b.name)
print(b.name)
输出:
v_scope/b:0
v_scope/b:0