6

tensorflow入门学习,继续巩固,placeholder 和 variable op的使用(七)

 3 years ago
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.
neoserver,ios ssh client

tensorflow入门学习,继续巩固,placeholder 和 variable op的使用(七)

发表于 2018-06-26 23:06:10   |   已被 访问: 398 次   |   分类于:   tensorflow   |   1 条评论

上一节,我们 一行一行写出了线性回归代码,这一节,先对 tensorflow 基础做了一些心得总结,然后继续巩固 tensorflow 的基础:placeholdervariable 节点(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 连接了节点 matrix1matrix2

采用 python 描述完 tensorflow 网络后,按照前面几节的介绍,tensorflow 为了效率,会将计算图完全放在 python 外部的高速计算环境计算,session 起到连接作用。所以,在第 9 行代码之前,咱们只是描述了 tensorflow 网络,并没有开始计算,直到调用 sessionrun 方法。

现在再回头整理这些内容,好理解多了哈。下面再巩固下 placeholdervariable 的使用。

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 则能够长久的保存数据,非常适合存储训练网络的权值偏置placeholdervariable 的区别,有点像 verilog 语言里的 wirereg。那么,怎么使用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 行,通过默认图 tfadd 方法,将常量 1 和计数器变量连接了起来。执行,结果如下:

$ python holder_variable.py 
1
2
3

经过这一节,感觉对 tensorflow 的了解又深入了一点。

阅读更多:   tensorflow


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK