1.Scope
作用:
①为scope内的op和tensor的name增加前缀,方便管理
②其reuse参数可用来变量共享
两种scope:
①tf.name_scope(name): 为scope内的op和tf.Variable()/tf.constant()创建的tensor添加前缀name,例子如下
with tf.name_scope('scope1') as scope1:
var1=tf.Variable(name='var1',initial_value=1)
var2=tf.get_variable(name='var2',shape=())
init_op=tf.initialize_all_variables()
print(var1.name)
print(var2.name)
print(init_op.name)
result:
scope1/var1:0
var2L0
scope1/init
②tf.variable_scope(name): 为scope内的所有op和tensor添加前缀name,例子如下
with tf.variable_scope('scope1') as scope1:
var1=tf.Variable(name='var1',initial_value=1)
var2=tf.get_variable(name='var2',shape=())
init_op=tf.initialize_all_variables()
print(var1.name)
print(var2.name)
print(init_op.name)
scope1/var1:0
scope1/var2:0
scope1/init
利用tf.get_variable_scope()返回的obj可获取当前scope的信息,如.name()获取scope名等
2.tf.Variable()和tf.get_variable()的区别
①如前所述,tf,Variable()受name_scope影响,tf.get_variable()则不受
②创建name冲突的变量时,tf.Variable()自动变更name,而tf.get_variable()根据scope的reuse是否为true,返回或创建参数,实现变量共享
共享参数,是指name已存在,则获取,若不存在,则创建。
例子:
with tf.variable_scope('scope1') as scope1:
var1=tf.get_variable(name='var1',shape=())
var2 = tf.get_variable(name='var1', shape=())
print(var1.name)
print(var2.name)
result:
ValueError: Variable scope1/var1 already exists, disallowed.
with tf.variable_scope('scope1') as scope1:
var1 = tf.get_variable(name='var1', shape=())
with tf.variable_scope(scope1, reuse=True):
var2 = tf.get_variable(name='var1', shape=())
print(var1.name)
print(var2.name)
print(var1==var2)
result:
scope1/var1:0
scope1/var1:0
True
事实上可以直接修改scope的reuse参数,更为简便
with tf.variable_scope('scope1') as scope1:
var1 = tf.get_variable(name='var1', shape=())
scope1.reuse_variables()
var2 = tf.get_variable(name='var1', shape=())
print(var1.name)
print(var2.name)
print(var1==var2)