tensorflow入门学习,继续巩固,placeholder 和 variable op的使用(七)
source link: https://blog.popkx.com/tensorflow-study-use-of-placeholder-and-variable-1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
tensorflow入门学习,继续巩固,placeholder 和 variable op的使用(七)
上一节,我们 一行一行写出了线性回归代码,这一节,先对 tensorflow 基础做了一些心得总结,然后继续巩固 tensorflow 的基础:
placeholder
和variable
节点(op)的使用。它俩是 tensorflow 常用的节点,弄清楚点,会用的更加顺心应手。
再看第一节
第一节提到,tensorflow 的计算是以图
为单位的,这里不要把图
当作一副图片,当作 tensorflow 里的一个容器吧。在下面一段 python tensorflow 代码中:
import tensorflow as tf
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
session = tf.Session()
res = session.run(product)
print res
session.close()
tf
就是 tensorflow 的一个默认图,tf.constant 中的 constant
就是默认图 tf
的一个节点(op),节点可以赋值给别的变量,例如上图中的 matrix1
product
等。所以,当打印matrix1
product
等变量时,打印的其实是节点,节点的信息通常比较丰富,有点类似于结构体。使用 python 编写 tensorflow,实际上是描述出一个网络模型。想象一下:我们在图
中构造很多节点,然后将这些节点连接起来,不就构成网络了吗?
以上面代码为例,product 连接了节点
matrix1
和matrix2
。
采用 python 描述完 tensorflow 网络后,按照前面几节的介绍,tensorflow 为了效率,会将计算图完全放在 python 外部的高速计算环境计算,session
起到连接作用。所以,在第 9 行代码之前,咱们只是描述了 tensorflow 网络,并没有开始计算,直到调用 session
的 run
方法。
现在再回头整理这些内容,好理解多了哈。下面再巩固下 placeholder
和 variable
的使用。
tensorflow 的 placeholder
上面咱们说到,再运行 tensorflow 网络之前,需要先描述网络。有些时候,并不能确定什么时候需要什么数值。这时,placeholder
就有大用处了,它可以在网络中“占好位置”,等需要用的时候,即可通过 feed
传送数值,以下是 python tensorflow 例子:
#encoding=utf8
import tensorflow as tf
import numpy as np
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.add(input1, input2)
sess = tf.Session()
print sess.run(output, feed_dict={input1:[2.], input2:[3.]})
sess.close()
首先,创建了两个 holder,都是 float32 型的,不考虑具体数值,先占着位。什么时候传入数值呢?肯定是需要的时候了。这里的例子,是在执行 tf.add 时,通过字典
的形式 feed
数据的。[2.]
的意思是传递一个数据 2.0
。运行结果如下:
$ python holder_variable.py
[5.]
tensorflow 的 variable
通过这几节的学习,知道通过 feed
传送的数据都是临时的,在计算之后,就被释放了。所以,训练网络的权值
和偏置
肯定不能用 placeholder。与之相对的,variable
则能够长久的保存数据,非常适合存储训练网络的权值
和偏置
。placeholder
和 variable
的区别,有点像 verilog 语言里的 wire
和 reg
。那么,怎么使用variable
呢?下面是一个 python tensorflow 例子:
#encoding=utf8
import tensorflow as tf
import numpy as np
state = tf.Variable(0, name='counter') # 取名为 counter
one = tf.constant(1) # 常量
new = tf.add(state, one) # 将常量加到 state
update = tf.assign(state, new) # 赋值
init = tf.global_variables_initializer() # 必须有的,tensorflow 规定的
sess = tf.Session()
sess.run(init)
for _ in range(3): # 执行 3 次累加
sess.run(update)
print sess.run(state)
sess.close()
以上代码,建立了一个累加器,每次加 1。第 8 行,通过默认图 tf
的 add
方法,将常量 1 和计数器变量连接了起来。执行,结果如下:
$ python holder_variable.py
1
2
3
经过这一节,感觉对 tensorflow 的了解又深入了一点。
阅读更多: tensorflow
Recommend
-
6
tensorflow入门学习,保存训练好的权值和偏置,并且再导入到空网络模型(13) 发表于...
-
12
tensorflow入门学习,打造自己的工具库,将构建神经层动作抽象为可复用python库(12) 发表于...
-
8
tensorflow入门学习,打造自己的工具箱,将构建神经层的动作抽象为可复用函数(12) 发表于...
-
5
tensorflow入门学习,对二次函数的非线性回归,并使用matplotlib库使训练结果动态可视化(11) 发表于...
-
6
tensorflow 支持结果可视化。可视化的流程是:在训练过程中,将要观察的数据写入文件,最后调用 tensorboard 载入文件,即可在浏览器中查看结果。本节将学习这一功能。可视化结果告诉我们什么得到...
-
3
tensorflow入门学习,深度学习神经网络为什么要使用激活函数,激活函数的作用是什么(八) 发表于...
-
7
tensorflow入门学习,继续巩固,一行一行写出线性回归代码(六) 发表于 201...
-
15
tensorflow入门学习,对前面几节,MINST手写数字识别的总结(五) 发表于 20...
-
6
tensorflow入门学习,建立多层卷积网络模型,提升MNIST手写数字识别正确率到99%(三) 发表于...
-
5
你是否在夜深人静的时候,想看一些会让你更睡不着的图片却苦于没有资源…你是否在节假日出行高峰的时候,想快速抢购火车票成功…你是否在网上购物的时候,想快速且精准的定位到口碑质量最好的商品…
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK