TensorFlow图变量tf.Variable的用法解析

来源:互联网 发布:刘涛用什么软件直播 编辑:程序博客网 时间:2024/06/05 03:11

TensorFlow图变量tf.Variable的用法解析

变量

TensorFlow中的图变量,跟我们平时所接触的一般变量在用法上有很大的差异。尤其对于那些初次接触此类深度学习库的编程人员来说,会显得十分难上手。

本文将按照如下篇幅深入剖析tf.Variable这个核心概念:

  1. 图变量的初始化方法
  2. 两种定义图变量的方法
  3. scope如何划分命名空间
  4. 图变量的复用
  5. 图变量的种类

1.图变量的初始化方法

对于一般的Python代码,变量的初始化就是变量的定义,向下面这样:

In [1]: x = 3In [2]: y = 3 * 5In [3]: yOut[3]: 15

如果我们模仿上面的写法来进行TensorFlow编程,就会出现下面的”怪现象”:

In [1]: import tensorflow as tfIn [2]: x = tf.Variable(3, name='x')In [3]: y = x * 5In [4]: print(y)Tensor("mul:0", shape=(), dtype=int32)

y的值并不是我们预想中的15,而是一个莫名其妙的输出——”

In [1]: import tensorflow as tfIn [2]: x = tf.Variable(3, name='x')In [3]: y = x * 5In [4]: sess = tf.InteractiveSession()In [5]: sess.run(tf.global_variables_initializer())In [6]: sess.run(y)Out[6]: 15

在TensorFlow的世界里,变量的定义和初始化是分开的,所有关于图变量的赋值和计算都要通过tf.Session的run来进行。想要将所有图变量进行集体初始化时应该使用tf.global_variables_initializer。

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

tf.Variable

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

参数名称 参数类型 含义 initial_value 所有可以转换为Tensor的类型 变量的初始值 trainable bool 如果为True,会把它加入到GraphKeys.TRAINABLE_VARIABLES,才能对它使用Optimizer collections list 指定该图变量的类型、默认为[GraphKeys.GLOBAL_VARIABLES] validate_shape bool 如果为False,则不进行类型和维度检查 name string 变量的名称,如果没有指定则系统会自动分配一个唯一的值

虽然有一堆参数,但只有第一个参数initial_value是必需的,用法如下(assign函数用于给图变量赋值):

In [1]: import tensorflow as tfIn [2]: v = tf.Variable(3, name='v')In [3]: v2 = v.assign(5)In [4]: sess = tf.InteractiveSession()In [5]: sess.run(v.initializer)In [6]: sess.run(v)Out[6]: 3In [7]: sess.run(v2)Out[7]: 5

tf.get_variable

tf.get_variable跟tf.Variable都可以用来定义图变量,但是前者的必需参数(即第一个参数)并不是图变量的初始值,而是图变量的名称。

tf.Variable的用法要更丰富一点,当指定名称的图变量已经存在时表示获取它,当指定名称的图变量不存在时表示定义它,用法如下:

In [1]: import tensorflow as tfIn [2]: init = tf.constant_initializer([5])In [3]: x = tf.get_variable('x', shape=[1], initializer=init)In [4]: sess = tf.InteractiveSession()In [5]: sess.run(x.initializer)In [6]: sess.run(x)Out[6]: array([ 5.], dtype=float32)

3.scope如何划分命名空间

一个深度学习模型的参数变量往往是成千上万的,不加上命名空间加以分组整理,将会成为可怕的灾难。TensorFlow的命名空间分为两种,tf.variable_scope和tf.name_scope。

下面示范使用tf.variable_scope把图变量划分为4组:

for i in range(4):    with tf.variable_scope('scope-{}'.format(i)):        for j in range(25):             v = tf.Variable(1, name=str(j))

可视化输出的结果如下:

可视化输出

下面让我们来分析tf.variable_scope和tf.name_scope的区别:

tf.variable_scope

当使用tf.get_variable定义变量时,如果出现同名的情况将会引起报错

In [1]: import tensorflow as tfIn [2]: 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 in VarScope? Originally defined at:

而对于tf.Variable来说,却可以定义“同名”变量

In [1]: import tensorflow as tfIn [2]: with tf.variable_scope('scope'):   ...:     v1 = tf.Variable(1, name='var')   ...:     v2 = tf.Variable(2, name='var')   ...:In [3]: v1.name, v2.nameOut[3]: ('scope/var:0', 'scope/var_1:0')

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

如果想使用tf.get_variable来定义另一个同名图变量,可以考虑加入新一层scope,比如:

In [1]: import tensorflow as tfIn [2]: with tf.variable_scope('scope1'):   ...:     v1 = tf.get_variable('var', shape=[1])   ...:     with tf.variable_scope('scope2'):   ...:         v2 = tf.get_variable('var', shape=[1])   ...:In [3]: v1.name, v2.nameOut[3]: ('scope1/var:0', 'scope1/scope2/var:0')

tf.name_scope

当tf.get_variable遇上tf.name_scope,它定义的变量的最终完整名称将不受这个tf.name_scope的影响,如下:

In [1]: import tensorflow as tfIn [2]: 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 + y   ...:In [3]: x.name, y.name, z.nameOut[3]: ('v_scope/n_scope/x:0', 'v_scope/x:0', 'v_scope/n_scope/add:0')

4.图变量的复用

想象一下,如果我们正在定义一个循环神经网络RNN,想复用上一层的参数以提高模型最终的表现效果,应该怎么做呢?

做法一:

In [1]: import tensorflow as tfIn [2]: with tf.variable_scope('scope'):   ...:     v1 = tf.get_variable('var', [1])   ...:     tf.get_variable_scope().reuse_variables()   ...:     v2 = tf.get_variable('var', [1])   ...:In [3]: v1.name, v2.nameOut[3]: ('scope/var:0', 'scope/var:0')

做法二:

In [1]: import tensorflow as tfIn [2]: with tf.variable_scope('scope'):   ...:     v1 = tf.get_variable('x', [1])   ...:In [3]: with tf.variable_scope('scope', reuse=True):   ...:     v2 = tf.get_variable('x', [1])   ...:In [4]: v1.name, v2.nameOut[4]: ('scope/x:0', 'scope/x:0')

5.图变量的种类

TensorFlow的图变量分为两类:local_variables和global_variables。

如果我们想定义一个不需要长期保存的临时图变量,可以向下面这样定义它:

with tf.name_scope("increment"):    zero64 = tf.constant(0, dtype=tf.int64)    current = tf.Variable(zero64, name="incr", trainable=False, collections=[ops.GraphKeys.LOCAL_VARIABLES])
阅读全文
1 0
原创粉丝点击