一、 课程目标
- 理解TF的计算图方法
- 探索TF的内置函数
- 学会怎么去构建和组织最好的模型去做深度学习项目
二、书籍
- TensorFlow for Machine Intelligence (TFFMI)
- Hands-On Machine Learning with Scikit-Learn and TensorFlow. Chapter 9
Up and running with TensorFlow - Fundamentals of Deep Learning. Chapter 3: Implementing Neural Networks in TensorFlow (FODL)
书籍容易过时,推荐tensorflow官网
三、 简化版的Tensorflow
- TF Learn (tf.contrib.learn)
- TF Slim (tf.contrib.slim):
- 高级版 API: Keras, TFLearn, Pretty Tensor
四、Graphs and Sessions
1. tensor
一种n维的矩阵
0-d tensor: scalar (number)
1-d tensor: vector
2-d tensor: matrix
and so on
2. Data Flow Graphs
计算a的值的方式
import tensorflow as tf
a = tf.add(3, 5)
# with clause takes care
# of sess.close()
with tf.Session() as sess:
print sess.run(a)
数据流图如下所示
3. tf.Session()
主要用于执行需要计算的tensor。
x = 2
y = 3
op1 = tf.add(x, y)
op2 = tf.mul(x, y)
op3 = tf.pow(op2, op1)
with tf.Session() as sess:
op3 = sess.run(op3)
可以将图分成几部分,然后并行地计算在CPU,GPU上,如下图所示
4. tf.Graph()
添加一个运算给图,并将其设为默认图
g = tf.Graph()
with g.as_default():
a = 3
b = 5
x = tf.add(a, b)
sess = tf.Session(graph=g) # session is run on the graph g
# run session
sess.close()
获取默认的图
g = tf.get_default_graph()
不要将默认的图和用户创建的图混合在一起,如下情况会报错
g = tf.Graph()
# add ops to the default graph
a = tf.constant(3)
# add ops to the user created graph
with g.as_default():
b = tf.constant(5)
#Prone to errors
正确的使用方式
g1 = tf.get_default_graph()
g2 = tf.Graph()
# add ops to the default graph
with g1.as_default():
a = tf.Constant(3)
# add ops to the user created graph
with g2.as_default():
b = tf.Constant(5)
为什么使用Graph?
1. Save computation (only run subgraphs that lead to the values you want to fetch)
2. Break computation into small, differential pieces to facilitates auto-differentiation
3. Facilitate distributed computation, spread the work across multiple CPUs, GPUs, or devices
4. Many common machine learning models are commonly taught and visualized as directed graphs already