聲明:
- 參考自Python TensorFlow Tutorial – Build a Neural Network,本文簡化了文字部分
- 文中有很多到官方文檔的鏈接,畢竟有些官方文檔是中文的,而且寫的很好。
Tensorflow入門
Tensorflow graphs
Tensorflow是基於graph的並行計算模型。關於graph的理解可以參考官方文檔。舉個例子,計算a=(b+c)∗(c+2)a=(b+c)∗(c+2),我們可以將算式拆分成一下:
d = b + c
e = c + 2
a = d * e
- 1
- 2
- 3
轉換成graph後的形式爲:
講一個簡單的算式搞成這樣確實大材小用,但是我們可以通過這個例子發現:d=b+cd=b+c是不相關的,也就是可以並行計算。對於更復雜的CNN和RNN,graph的並行計算的能力將得到更好的展現。
實際中,基於Tensorflow構建的三層(單隱層)神經網絡如下圖所示:
Tensorflow data flow graph
上圖中,圓形或方形的節點被稱爲node,在node中流動的數據流被稱爲張量(tensor)。更多關於tensor的描述見官方文檔。
0階張量 == 標量
1階張量 == 向量(一維數組)
2階張量 == 二維數組
…
n階張量 == n維數組
tensor與node之間的關係:
如果輸入tensor的維度是5000×645000×64,表示有5000個訓練樣本,每個樣本有64個特徵,則輸入層必須有64個node來接受這些特徵。
上圖表示的三層網絡包括:輸入層(圖中的input)、隱藏層(這裏取名爲ReLU layer表示它的激活函數是ReLU)、輸出層(圖中的Logit Layer)。
可以看到,每一層中都有相關tensor流入Gradient節點計算梯度,然後這些梯度tensor進入SGD Trainer節點進行網絡優化(也就是update網絡參數)。
Tensorflow正是通過graph表示神經網絡,實現網絡的並行計算,提高效率。下面我們將通過一個簡單的例子來介紹TensorFlow的基礎語法。
A Simple TensorFlow example
用Tensorflow計算a=(b+c)∗(c+2)a=(b+c)∗(c+2), 1. 定義數據:
import tensorflow as tf
# 首先,創建一個TensorFlow常量=>2
const = tf.constant(2.0, name='const')
# 創建TensorFlow變量b和c
b = tf.Variable(2.0, name='b')
c = tf.Variable(1.0, dtype=tf.float32, name='c')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
如上,TensorFlow中,使用tf.constant()
定義常量,使用tf.Variable()
定義變量。Tensorflow可以自動進行數據類型檢測,比如:賦值2.0就默認爲tf.float32
,但最好還是顯式地定義。更多關於TensorFlow數據類型的介紹查看官方文檔。
2. 定義運算(也稱TensorFlow operation):
# 創建operation
d = tf.add(b, c, name='d')
e = tf.add(c, const, name='e')
a = tf.multiply(d, e, name='a')
- 1
- 2
- 3
- 4
發現了沒,在TensorFlow中,+−×÷+−×÷都有其特殊的函數表示。實際上,TensorFlow定義了足夠多的函數來表示所有的數學運算,當然也對部分數學運算進行了運算符重載,但保險起見,我還是建議你使用函數代替運算符。
!!TensorFlow中所有的變量必須經過初始化才能使用,初始化方式分兩步:
- 定義初始化operation
- 運行初始化operation
# 1. 定義init operation
init_op = tf.global_variables_initializer()
- 1
- 2
以上已經完成TensorFlow graph的搭建,下一步即計算並輸出。
運行graph需要先調用tf.Session()
函數創建一個會話(session)。session就是我們與graph交互的handle。更多關於session的介紹見官方文檔。
# session
with tf.Session() as sess:
# 2. 運行init operation
sess.run(init_op)
# 計算
a_out = sess.run(a)
print("Variable a is {}".format(a_out))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
值得一提的是,TensorFlow有一個極好的可視化工具TensorBoard,詳見官方文檔。將上面例子的graph可視化之後的結果爲:
The TensorFlow placeholder
對上面例子的改進:使變量b可以接收任意值。TensorFlow中接收值的方式爲佔位符(placeholder),通過tf.placeholder()
創建。
# 創建placeholder
b = tf.placeholder(tf.float32, [None, 1], name='b')
- 1
- 2
第二個參數值爲[None, 1],其中None表示不確定,即不確定第一個維度的大小,第一維可以是任意大小。特別對應tensor數量(或者樣本數量),輸入的tensor數目可以是32、64…
現在,如果得到計算結果,需要在運行過程中feed佔位符b的值,具體爲將a_out = sess.run(a)
改爲:
a_out = sess.run(a, feed_dict={b: np.arange(0, 10)[:, np.newaxis]})
- 1
輸出:
Variable a is [[ 3.]
[ 6.]
[ 9.]
[ 12.]
[ 15.]
[ 18.]
[ 21.]
[ 24.]
[ 27.]
[ 30.]]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
A Neural Network Example
神經網絡的例子,數據集爲MNIST數據集。
1. 加載數據:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
- 1
- 2
one_hot=True
表示對label進行one-hot編碼,比如標籤4可以表示爲[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]。這是神經網絡輸出層要求的格式。
Setting things up
2. 定義超參數和placeholder
# 超參數
learning_rate = 0.5
epochs = 10
batch_size = 100
# placeholder
# 輸入圖片爲28 x 28 像素 = 784
x = tf.placeholder(tf.float32, [None, 784])
# 輸出爲0-9的one-hot編碼
y = tf.placeholder(tf.float32, [None, 10])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
再次強調,[None, 784]中的None表示任意值,特別對應tensor數目。
3. 定義參數w和b
# hidden layer => w, b
W1 = tf.Variable(tf.random_normal([784, 300], stddev=0.03), name='W1')
b1 = tf.Variable(tf.random_normal([300]), name='b1')
# output layer => w, b
W2 = tf.Variable(tf.random_normal([300, 10], stddev=0.03), name='W2')
b2 = tf.Variable(tf.random_normal([10]), name='b2')
- 1
- 2
- 3
- 4
- 5
- 6
在這裏,要了解全連接層的兩個參數w和b都是需要隨機初始化的,tf.random_normal()
生成正態分佈的隨機數。
4. 構造隱層網絡
# hidden layer
hidden_out = tf.add(tf.matmul(x, W1), b1)
hidden_out = tf.nn.relu(hidden_out)
- 1
- 2
- 3
上面代碼對應於公式:
5. 構造輸出(預測值)
# 計算輸出
y_ = tf.nn.softmax(tf.add(tf.matmul(hidden_out, W2), b2))
- 1
- 2
對於單標籤多分類任務,輸出層的激活函數都是tf.nn.softmax()
。更多關於softmax的知識見維基百科。
6. BP部分—定義loss
損失爲交叉熵,公式爲
公式分爲兩步:
- 對n個標籤計算交叉熵
- 對m個樣本取平均
y_clipped = tf.clip_by_value(y_, 1e-10, 0.9999999)
cross_entropy = -tf.reduce_mean(tf.reduce_sum(y * tf.log(y_clipped) + (1 - y) * tf.log(1 - y_clipped), axis=1))
- 1
- 2
7. BP部分—定義優化算法
# 創建優化器,確定優化目標
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimizer(cross_entropy)
- 1
- 2
TensorFlow中更多優化算法詳見官方文檔。
8. 定義初始化operation和準確率node
# init operator
init_op = tf.global_variables_initializer()
# 創建準確率節點
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
- 1
- 2
- 3
- 4
- 5
- 6
correct_predicion
會返回一個m×1m×1的tensor,tensor的值爲True/False表示是否正確預測。
Setting up the trianing
9. 開始訓練
# 創建session
with tf.Session() as sess:
# 變量初始化
sess.run(init)
total_batch = int(len(mnist.train.labels) / batch_size)
for epoch in range(epochs):
avg_cost = 0
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size=batch_size)
_, c = sess.run([optimizer, cross_entropy], feed_dict={x: batch_x, y: batch_y})
avg_cost += c / total_batch
print("Epoch:", (epoch + 1), "cost = ", "{:.3f}".format(avg_cost))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
輸出:
Epoch: 1 cost = 0.586
Epoch: 2 cost = 0.213
Epoch: 3 cost = 0.150
Epoch: 4 cost = 0.113
Epoch: 5 cost = 0.094
Epoch: 6 cost = 0.073
Epoch: 7 cost = 0.058
Epoch: 8 cost = 0.045
Epoch: 9 cost = 0.036
Epoch: 10 cost = 0.027
Training complete!
0.9787
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
通過TensorBoard可視化訓練過程: