图变量作用域tf.get_variable

1.图变量

TensorFlow中的图变量,跟我们平时所接触的一般变量在用法上有很大的差异。在TensorFlow的世界里,变量的定义和初始化是分开的,所有关于图变量的赋值和计算都要通过tf.Session的run来进行想要将所有图变量进行集体初始化时应该使用tf.global_variables_initializer。
import tensorflow as tf
x = tf.Variable(3, name='x')
y = x * 5
v2 = x.assign(5)
print(y) #Tensor("mul:0", shape=(), dtype=int32)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
print(sess.run(y)) #15
print(sess.run(v2))#5

2.两种定义图变量的方法
         

tf.Variable.init(initial_value, trainable=True, collections=None, validate_shape=True, name=None)

参数名称参数类型含义
initial_value所有可以转换为Tensor的类型变量的初始值
trainablebool如果为True,会把它加入到GraphKeys.TRAINABLE_VARIABLES,才能对它使用Optimizer
collectionslist指定该图变量的类型、默认为[GraphKeys.GLOBAL_VARIABLES]
validate_shapebool如果为False,则不进行类型和维度检查
namestring变量的名称,如果没有指定则系统会自动分配一个唯一的值
        虽然有一堆参数,但只有第一个参数initial_value是必需的,用法见前面代码(assign函数用于给图变量赋值)
(2)  tf.get_variable跟tf.Variable都可以用来定义图变量,但是前者的必需参数(即第一个参数)并不是图变量的初始值,而是           图变量的名称。但 tf.get_variable的用法要更丰富一点,当指定名称的图变量已经存在时表示获取它,当指定名称的图变量         不存在时表示定义它,用法如下:
       tf.get_variable(name, shape, initializer): name 就是变量的名称,shape是变量的维度,initializer是变量初始化的方式,          初始化的方式有以下几种:

       tf.constant_initializer:常量初始化函数

      tf.random_normal_initializer:正态分布

      tf.truncated_normal_initializer:截取的正态分布

      tf.random_uniform_initializer:均匀分布

      tf.zeros_initializer:全部是0

      tf.ones_initializer:全是1

     tf.uniform_unit_scaling_initializer:满足均匀分布,但不影响输出数量级的随机值

注意:不同的变量之间不能有相同的名字
3.scope如何划分命名空间
     一个深度学习模型的参数变量往往是成千上万的,不加上命名空间加以分组整理,将会成为可怕的灾难。TensorFlow的命名空间分为两种,tf.variable_scope和tf.name_scope。
tf.variable_scope
当使用tf.get_variable定义变量时,如果出现同名的情况将会引起报错:
with tf.variable_scope('scope'): v1 = tf.get_variable('var', [1]) v2 = tf.get_variable('var', [1])
ValueError: Variable scope/var already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
而对于tf.Variable来说,却可以定义“同名”变量
with tf.variable_scope('scope'):
    v1 = tf.Variable(1, name='var')
    v2 = tf.Variable(2, name='var')
    print(v1.name) #scope/var:0
    print(v2.name)#scope/var_1:0

可以发现若把这些图变量的name属性打印出来,就可以发现它们的名称并不是一样的。

如果想使用tf.get_variable来定义另一个同名图变量,可以考虑加入新一层scope,比如:
with tf.variable_scope('scope'):
    v1 = tf.get_variable('var', [1])
    with tf.variable_scope('scope2'):
        v2 = tf.get_variable('var', [1])
    print(v1.name)#scope/var:0
    print(v2.name)#scope/scope2/var:0

tf.name_scope

当tf.get_variable遇上tf.name_scope,它定义的变量的最终完整名称将不受这个tf.name_scope的影响,如下:
import tensorflow as tf
with tf.variable_scope('v_scope'):
    with tf.name_scope('n_scope'):
        x = tf.Variable([1], name='x')        y = tf.get_variable('x', shape=[1], dtype=tf.int32)         z = x + yprint(x.name, y.name, z.name)#v_scope/n_scope/x:0 v_scope/x:0 v_scope/n_scope/add:0

4.图变量的复用

  如果我们正在定义一个循环神经网络RNN,想复用上一层的参数以提高模型最终的表现效果,应该怎么做呢?
 做法1:
import tensorflow as tf
with tf.variable_scope('scope'):
    v1 = tf.get_variable('x', [1]) 
with tf.variable_scope('scope', reuse=True):
    v2 = tf.get_variable('x', [1])
print(v1.name, v2.name)
#scope/x:0 scope/x:0
做法2:
import tensorflow as tf
with tf.variable_scope('scope'):
    v1 = tf.get_variable('x', [1]) 
    tf.get_variable_scope().reuse_variables()
    v2 = tf.get_variable('x', [1])
print(v1.name, v2.name)
#scope/x:0 scope/x:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值