一、 課程目標
- 理解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