原文链接:https://blog.csdn.net/u012436149/article/details/53081454
https://www.cnblogs.com/jiangxinyang/p/9392898.html
在训练深度网络时,为了减少需要训练参数的个数(比如LSTM模型),或者是多机多卡并行化训练大数据、大模型等情况时,往往就需要共享变量。另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复,同时维护一个条理清晰的graph非常重要。因此,tensorflow中用tf.Variable(), tf.get_variable, tf.Variable_scope(), tf.name_scope() 几个函数来实现:
一.tf.Variable() 与 tf.get_variable() 的作用与区别:
1)tf.Variable() 会自动监测命名冲突并自行处理,即有重名时自动在变量名后面增加数字后缀以区别.但是tf.get_variable() 遇到重名的变量创建且没有设置为共享变量时,则会报错。
import tensorflow as tf
a1 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2')
a2 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2')
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a2.name)
# 输出
a2:0
a2_1:0
自动在名字后加了数字后缀,以区别
而tf.get_variable() 使用同样的名字,不加复用标志则会报错.
import tensorflow as tf;
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a3.name)
# 输出
ValueError: Variable a1 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
2) tf.Variable() 和 tf.get_variable() 都是用于在一个name_scope下面获取或创建一个变量的两种方式,区别在于: tf.Variable()用于创建一个新变量,在同一个name_scope下面,可以创建相同名字的变量,底层实现会自动引入别名机制,两次调用产生了其实是两个不同的变量。tf.get_variable(<variable_name>)用于获取一个变量,并且不受name_scope的约束(不受约束是什么意思呢?,不同的name_scope下,使用get_variable获得相同名称的变量仍然会报错)。当这个变量已经存在时,则自动获取;如果不存在,则自动创建一个变量。
import tensorflow as tf;
import numpy as np;
with tf.name_scope('V1'):
a1 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.name_scope('V2'):
a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (a1.name)
print (a2.name)
# 输出
V1/a2:0
V2/a2:0
import tensorflow as tf;
with tf.name_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.name_scope('V2'):
a2 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (a1.name)
print (a2.name)
# 输出
Variable a1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:
3)tf.name_scope() 与 tf.variable_scope(): tf.name_scope():主要用于管理一个图里面的各种op(op是什么?),返回的是一个以scope_name命名的context manager。一个graph会维护一个name_space的 堆,每一个namespace下面可以定义各种op或者子namespace,实现一种层次化有条理的管理,避免各个op之间命名冲突。 tf.variable_scope() 一般与tf.get_variable()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突。
import tensorflow as tf;
import numpy as np;
with tf.variable_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.variable_scope('V2'):
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (a1.name)
print (a2.name)
print (a3.name)
print (a4.name)
# 输出
V1/a1:0
V1/a2:0
V2/a1:0
V2/a2:0
4)当要重复使用变量共享时,可以用tf.variable_scope() 和 tf.get_variable()来实现
import tensorflow as tf
with tf.variable_scope('V1', reuse=None):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.variable_scope('V1', reuse=True):
a2 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a2.name)
#输出
V1/a1:0
V1/a1:0
上面的代码在第一个variable_scope中的reuse=None,在之后的variable_scope中若是要共享变量,就要将reuse=True。
name_scope会给op(操作加前缀),而对get_variable不起作用.
import tensorflow as tf
with tf.name_scope('name1'):
z=tf.Variable([1],name='var1')
w=tf.get_variable('var1',shape=[2])
res = tf.add(w,[3])
print z.name
print w.name
print res.name
name1/var1:0
var1:0
name1/Add:0