TensorFlow图变量tf.Variable的用法解析
来源:互联网 发布:刘涛用什么软件直播 编辑:程序博客网 时间:2024/06/05 03:11
TensorFlow图变量tf.Variable的用法解析
TensorFlow中的图变量,跟我们平时所接触的一般变量在用法上有很大的差异。尤其对于那些初次接触此类深度学习库的编程人员来说,会显得十分难上手。
本文将按照如下篇幅深入剖析tf.Variable这个核心概念:
- 图变量的初始化方法
- 两种定义图变量的方法
- scope如何划分命名空间
- 图变量的复用
- 图变量的种类
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是必需的,用法如下(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])
- TensorFlow图变量tf.Variable的用法解析
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别:
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- 【TensorFlow】变量管理tf.get_variables/tf.Variable/tf.variable_scope的应用(七)
- tensorflow之变量初始化(tf.Variable)
- tf.Variable函数的用法
- tensorflow 变量生成 变量管理 tf.Variable & tf.get_variable & tf.variable_scope
- TensorFlow--tf.Variable
- tensorflow API:tf.Variable
- 【tensorflow 学习】tf.get_variable()和tf.Variable()的区别
- TensorFlow tf.Variable()与tf.get_variable()的简单介绍
- 【TensorFlow】tf.sparse_to_dense的用法
- 【TensorFlow】tf.concat的用法
- tensorflow:tf.stack的用法
- Tensorflow:tf.flags的用法
- 【TensorFlow】tf.concat的用法
- TensorFlow 插曲-- tf.placeholder 与 tf.Variable
- 单例设计模式
- 关联容器和顺序容器
- POJ 2955
- 进程与进程之间的通信
- FFMPEG Qt视频播放器之播放控制
- TensorFlow图变量tf.Variable的用法解析
- python数据可视化(三)字云
- 55. Jump Game
- czl的知识点整理3——LCA
- 嵌入式Linux驱动笔记(十六)------设备驱动模型(kobject、kset、ktype)
- electron介绍及安装
- spring源码分析(一)IOC管理Bean
- 使用按位异或运算符进行两数交换
- Java 组件及事件处理实训 实训1: 编写一个窗体程序,能够对文本区中的文字设置字体和大小。