使用tf.Variable方法创建变量
使用tf.Variable方法创建变量时有两点需要注意:
①一般情况下,使用tf.Variable方法创建的变量都有作用域,也可叫做变量的可用性范围,即在变量所属的模型内,变量的名字是有效可用的。
②使用tf.Variable方法创建变量时,会生成一个新的变量。如果在一个模型中先后定义了两个名字相同的变量,那么后面那个变量是生效的,将覆盖第一个变量。
示例代码如下:
python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
abc1 = tf.Variable(1.0,name = 'firstvar')
print("abc1:",abc1.name)
abc2 = tf.Variable(1.56,name = 'firstvar')
print("abc2:",abc2.name)
abc2 = tf.Variable(1.88,name = 'firstvar')
print("abc2:",abc2.name)
abc3 = tf.Variable(2.0)
print("abc3:",abc3.name)
abc4 = tf.Variable(3.0)
print("abc4:",abc4.name)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print("abc1 = ",abc1.eval())
print("abc2 = ",abc2.eval())
print("abc3 = ",abc3.eval())
print("abc4 = ",abc4.eval())
使用tf.get_variable方法创建变量
在有些情况下,一个模型需要使用其他模型创建的变量,达到两个模型一起训练变量的效果。这时需要使用get_variable方法,以实现共享变量。
示例代码如下:
python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
abc1 = tf.Variable(1.0,name = 'firstvar')
print("abc1:",abc1.name)
abc2 = tf.Variable(1.56,name = 'firstvar')
print("abc2:",abc2.name)
abc2 = tf.Variable(1.88,name = 'firstvar')
print("abc2:",abc2.name)
abc3 = tf.Variable(2.0)
print("abc3:",abc3.name)
abc4 = tf.Variable(3.0)
print("abc4:",abc4.name)
get_abc2 = tf.get_variable('firstvar',[1],initializer = tf.constant_initializer(6.6))
print("get_abc2 = ",get_abc2.name)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print("get_abc2 = ",get_abc2.eval())
get_abc2 = tf.get_variable('firstvar1',[1],initializer = tf.constant_initializer(8.8))
print("get_abc2 = ",get_abc2.name)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print("get_abc2 = ",get_abc2.eval())
简而言之,tf.Variable可以创建同名的变量,但是tf.get_variable创建同名变量会报错,所以在使用的时候,你用变量名去索引,tf.get_variable会得到唯一的值。
在特定的作用域下获取变量
使用get_variable创建两个同样名字的变量是行不通的。可以配合variable_scope(变量的作用域),创建两个同名的变量。
示例代码如下:
python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
with tf.variable_scope('test1'):
get_abc1 = tf.get_variable('firstvar',[1],initializer = tf.constant_initializer(6.6))
with tf.variable_scope('test2'):
get_abc2 = tf.get_variable('firstvar',[1],initializer = tf.constant_initializer(8.8))
print("get_abc1:",get_abc1.name)
print("get_abc2:",get_abc2.name)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print("get_abc1:",get_abc1.eval())
print("get_abc2:",get_abc2.eval())
使用作用域中的reuse参数来实现共享变量功能
variable_scope里有个reuse属性,当reuse = True时,表示使用已经定义过的变量。这时get_variable将不会再创建新的变量,而是去模型中在使用get_variable所创建过的变量中找与name相同的变量。
示例代码如下:
python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
with tf.variable_scope('test1'):
get_abc1 = tf.get_variable('firstvar',[1],initializer = tf.constant_initializer(6.6))
with tf.variable_scope('test2'):
get_abc2 = tf.get_variable('firstvar',[1],initializer = tf.constant_initializer(8.8))
with tf.variable_scope('test1',reuse = True):
get_abc3 = tf.get_variable('firstvar',[1],initializer = tf.constant_initializer(9.6))
with tf.variable_scope('test2',reuse = True):
get_abc4 = tf.get_variable('firstvar',[1],initializer = tf.constant_initializer(9.8))
print("get_abc1:",get_abc1.name)
print("get_abc2:",get_abc2.name)
print("get_abc3:",get_abc3.name)
print("get_abc4:",get_abc4.name)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print("get_abc1:",get_abc1.eval())
print("get_abc2:",get_abc2.eval())
print("get_abc3:",get_abc3.eval())
print("get_abc4:",get_abc4.eval())
共享变量的作用域与初始化
使用get_variable方法获得变量时是可以初始化的。同样,在variable_scope中也可以初始化。并且如果variable_scope中有嵌套,还有继承功能,定义变量时,如果没有进行初始化,则TensorFlow会默认使用作用域的初始化方法对其初始化,并且作用域的初始化方法也有继承功能。
示例代码如下:
python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
with tf.variable_scope("test1",initializer = tf.constant_initializer(6.6)):
get_abc1 = tf.get_variable("firstvar",shape = [2],dtype = tf.float32)
with tf.variable_scope("test2"):
get_abc2 = tf.get_variable("firstvar",shape = [2],dtype = tf.float32)
get_abc3 = tf.get_variable("secondvar",shape = [2],initializer = tf.constant_initializer(8.8))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print("get_abc1 = ",get_abc1.eval())
print("get_abc2 = ",get_abc2.eval())
print("get_abc3 = ",get_abc3.eval())
作用域与操作符的受限范围
variable_scope还可以通过采用with variable_scope("name") as xxx的方式定义作用域,当使用这种方式时,所定义的作用域变量xxx将不再受到外围的scope所限制
操作符不仅受到tf.name_scope作用域的限制,同时也受到tf.variable_scope作用域的限制。
示例代码如下:
python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
with tf.variable_scope("test1") as sp:
get_abc1 = tf.get_variable("firstvar",[1])
print("sp:",sp.name)
print("get_abc1:",get_abc1.name)
with tf.variable_scope("test2"):
get_abc2 = tf.get_variable("firstvar",[1])
with tf.variable_scope(sp) as sp1:
get_abc3 = tf.get_variable("firstvar3",[1])
print("sp1:",sp1.name)
print("get_abc2:",get_abc2.name)
print("get_abc3:",get_abc3.name)
with tf.variable_scope("test3"):
with tf.name_scope("ops"):
v = tf.get_variable("var",[1])
y = 3.0 + v
print("v:",v.name)
print("y.op:",y.op.name)