tensorflow low level api

低级别的API构建TensorFlow模型
使用低级别的Tensorflow API (Tensorflow Core)构建tensorflow程序
Tensorflow Core 程序可以看成互相独立的两部分:

  1. 构建计算图(tf.Graph
  2. 运行计算图(tf.Session

示例下面的代码code1

1
2
3
4
5
6
7
8
9
10
11
# 构建计算图
a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0)
total = a + b
print(a)
print(b)
print(total)

# 运行计算图
sess = tf.Session()
print(sess.run(total))

打印语句会生成

1
2
3
4
Tensor("Const:0", shape=(), dtype=float32)
Tensor("Const_1:0", shape=(), dtype=float32)
Tensor("add:0", shape=(), dtype=float32)
7.0

tf.Graph

计算图是排列成一个图的一系列 TensorFlow 指令。图由两种类型的对象组成。

  • 操作(简称“op”):图的节点。操作描述了消耗和生成张量的计算。
  • 张量:图的边。它们代表将流经图的值。大多数 TensorFlow 函数会返回 tf.Tensors

计算图是有向无环图

操作(tf.Operation)

图的节点(tf.Operation)
上面的代码code1使用tensorboard可视化为
计算图
里面total = a + b这个a+b语句会产生一个Add operation,即图的节点,该节点接受两个边(Tensor)ab然后产生total这个边(其实调用tf.constant()也会产生一个新的tf.Operation, 并返回一个Tensor, 这个Tensor被赋给了a)

张量(tf.Tensor)

图的边(tf.Tensor)

普通的张量

  • tf.Tensor

特殊的张量

普通的张量

普通的张量类似于code1的里面的total
详细介绍可参考:

tf.Variable

tf.Tensor 对象不同,tf.Variable 存在于单个 session.run 调用的上下文之外。

在 TensorFlow 内部,tf.Variable 会存储持久性张量。具体 op 允许您读取和修改此张量的值。这些修改在多个 tf.Session 之间是可见的,因此对于一个 tf.Variable,多个工作器可以看到相同的值。

我们构建tensorflow模型并进行训练的目的便是学习到一组Variable值,这些Variable代表了一个学习后的模型。

虽然tf.Variable是一种特殊的张量(特殊点在于其会被持久化,以及修改在多个tf.Session之间是可见的)。但是其还是张量,因此在tensorflow中使用tf.Variable的时候,只需将其视为普通的tf.Tensor就行

具体变量如何重用共享可参考:

会话

对于构建的计算图,我们需要使用tf.Session封装TensorFlow 运行时的状态,并运行 TensorFlow 操作。如上面的code1。
如果说 tf.Graph 像一个 .py 文件,那么 tf.Session 就像一个 python 可执行对象。
绝大部分tensorflow程序会话是使用tf.Session来实现的。
特殊的会话还有很多。下面列出的这些会话主要用于进行debug作用,还是比较常用的一种

  • tf.InteractiveSession
  • tf_debug.LocalCLIDebugWrapperSession
  • tf_debug.TensorBoardDebugWrapperSession

下图展示四种Session类的继承关系
会话继承关系

tf.Session

这是最常用的会话

tf.InteractiveSession

tf.InteractiveSession()是一种交互式的session方式,它让自己成为了默认的session,也就是说用户在不需要指明用哪个session运行的情况下,就可以运行起来,这就是默认的好处。这样的话就是run()和eval()函数可以不指明session啦。

对比一下:

1
2
3
4
5
6
7
8
import tensorflow as tf import numpy as np

a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
init=tf.global_variables_initializer()
sess=tf.Session()
print(c.eval())

上面的代码编译是错误的,显示错误如下:
ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use with sess.as_default() or pass an explicit session to eval(session=sess)

1
2
3
4
5
6
7
8
import tensorflow as tf import numpy as np

a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
init=tf.global_variables_initializer()
sess=tf.InteractiveSession()
print(c.eval())

而用InteractiveSession()就不会出错,说白了InteractiveSession()相当于:

1
2
sess=tf.Session()
with sess.as_default():

换句话说,如果说想让sess=tf.Session()起到作用,一种方法是上面的with sess.as_default();另外一种方法是

1
2
sess=tf.Session() 
print(c.eval(session=sess))

其实还有一种方法也是with,如下:

1
2
3
4
5
6
7
8
import tensorflow as tf import numpy as np

a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
init=tf.global_variables_initializer()
with tf.Session() as sess: #print (sess.run(c))
print(c.eval())

总结:tf.InteractiveSession()默认自己就是用户要操作的session,而tf.Session()没有这个默认,因此用eval()启动计算时需要指明session。

tf_debug.LocalCLIDebugWrapperSession

详细参考:

tf_debug.TensorBoardDebugWrapperSession

详细参考:

图和会话

详细的图和会话交互参考: