tensorflow中有两个关于variable的op,tf.Variable()
与tf.get_variable()
下面介绍这两个的区别
区别
- 使用
tf.Variable
时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()
时,系统不会处理冲突,而会报错
import tensorflow as tf
w_1 = tf.Variable(3,name="w_1")
w_2 = tf.Variable(1,name="w_1")
print w_1.name
print w_2.name
#输出
#w_1:0
#w_1_1:
import tensorflow as tf
w_1 = tf.get_variable(name="w_1",initializer=1)
w_2 = tf.get_variable(name="w_1",initializer=2)
#错误信息
#ValueError: Variable w_1 already exists, disallowed. Did
#you mean to set reuse=True in VarScope?
- 基于这两个函数的特性,当我们需要共享变量的时候,需要使用
tf.get_variable()
。在其他情况下,这两个的用法是一样的
get_variable()与Variable的实质区别
来看下面一段代码:
import tensorflow as tf
with tf.variable_scope("scope1"):
w1 = tf.get_variable("w1", shape=[])
w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
w1_p = tf.get_variable("w1", shape=[])
w2_p = tf.Variable(1.0, name="w2")
print(w1 is w1_p, w2 is w2_p)
#输出
#True False
看到这,就可以明白官网上说的参数复用的真面目了。由于tf.Variable()
每次都在创建新对象,所有reuse=True
和它并没有什么关系。对于get_variable()
,来说,如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的。
实例:
import tensorflow as tf
#print(dir(tf))
#print(help(tf.Variable))
#with tf.name_scope("a_name_scope") as scope:
with tf.variable_scope("a_variable_scope") as scope:
var1 = tf.get_variable(name = 'var1', initializer=[[1.,2.],[3.,4.]], dtype = tf.float32)
#scope.reuse_variables()
#print(dir(scope))
with tf.variable_scope("a_variable_scope", reuse = True) as scope:
var2 = tf.get_variable(name='var1', initializer=[2])
with tf.variable_scope("a_variable_scope", reuse = None) as scope:
var6 = tf.get_variable(name='var2', initializer=[2])
var3 = tf.Variable(initial_value = [3], name='var3')
var4 = tf.Variable(initial_value = [4], name='var3')
var5 = tf.Variable(initial_value = [5], name='var3')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(var1.name)
print(sess.run(var1))
print(var2.name)
print(sess.run(var2))
print(var3.name)
print(sess.run(var3))
print(var4.name)
print(sess.run(var4))
print(var5.name)
print(sess.run(var5))
输出:
a_variable_scope/var1:0
[[ 1. 2.]
[ 3. 4.]]
a_variable_scope/var1:0
[[ 1. 2.]
[ 3. 4.]]
a_variable_scope_2/var3:0
[3]
a_variable_scope_2/var3_1:0
[4]
a_variable_scope_2/var3_2:0
[5]