ML(Machine Learning)到底是嘛?
Tom Mitchell於1998年寫到:
A computer program is said to learn from experience E with respect to some task T and some performance measure P, if its performance on T as measured by P, improves with experiance E
計算機程序根據現有的經驗能夠完成某種任務,達到一定的性能指標,並且隨著經驗的增多,能夠不斷的提升性能。
一、TensorFlow是什麼?
TensorFlow是一個採用數據流圖,用於數值計算的開源軟件庫,主要用於機器學習和深度學習,由Google Brain開發,於2015年9月開源。
計算圖(computational graph)
TensorFlow是基於計算圖的框架,在具體介紹TensorFlow之前,我們先看看什麼是計算圖。假設我們有一個需要計算的表達式:y = a * b + c,該表達式包含一個乘法和一個加法,該表達式可以表示為:
上圖完整描述了計算任務的依賴關係,這種有向無環圖就叫做計算圖。
在TensorFlow中主要有以下幾個概念:
Tensor
Tensor(”張量“)是TensorFlow中最重要的數據單元,一個tensor由多維數組構成。
tensor的rank表示其數組的維度,tensor的shape代表每個緯度數據的個數,例如:
3 # 0維張量,即標量, rank = 0, shape = 1
[1. ,2., 3.] # 1維張量,即向量, rank = 1, shape = 3
[[1., 2., 3.,4.], [5., 6., 7.,8.]] # 2維張量, 一個2 x 3 的矩陣, rank = 2, shape =3
Operation
執行計算的單元,可以是加減乘除等數學運算,也可以是各種各樣的優化算法,Operation接收0個或多個Tensor,輸出一個Tensor
Node
在計算圖中用於代表Tensor或者Operation
Graph
Graph用於定義整個計算任務,沒有進行任何計算,在TensorFlow中,可以通過查看session.graph_def來得到graph的定義:
node {
name: "a/initial_value"
op: "Const"
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_INT32
tensor_shape {
}
int_val: 1
}
}
}
}
...
Session
Graph僅僅定義了所有的Node,沒有進行任何計算,而session根據graph的定義分配資源,執行計算任務。
二、安裝
TensorFlow的安裝非常簡單,有興趣的同學可以參考官網(https://www.tensorflow.org/install/),筆者這裡使用pip在mac上面進行安裝,遇到一個小坑,安裝TensorFlow時出現了[Errno 1] Operation not permitted,遇到相同問題的可以移步這裡(https://stackoverflow.com/questions/32659348/operation-not-permitted-when-on-root-el-capitan-rootless-disabled/32661637)。
三、TensorFlow的使用
假設現在我們有一組Data Set表示房屋的面積以及對應房屋的價格,我們想預測面積為90的房屋價格應該是多少,DataSet如下:
X | Y |
40 | 37.0000 |
65 | 72.0164 |
80 | 93.0456 |
115 | 130.4864 |
150 | 153.4161 |
其中X為房屋的面積,Y為房屋的價格,對於Tom給出的機器學習定義來說,這裡的E就是已有的房屋價格數據,T就是對房屋價格進行預測,P就是房屋價格預測的準確性。
先在座標系中繪製房屋價格數據如下:
觀察上圖,幾乎是一條直線,可以使用linear regression來處理這個問題,來對這些數據進行學習,獲得最優的θ0和θ1,使得平方誤差函數的值最小
擬合函數
h0 = θ0 + θ1 * x
參數
θ0, θ1
損失函數:
其中m為數據集的大小,xi表示第i個數據集輸入,yi表示對應的正確房價。
當使用TensorFlow構建graph時,大致分為5個部分:
1. 為輸入x(對於多特徵問題有多個輸入)與輸出y定義placeholder
2. 定義參數
3. 定義模型結構
4. 定義損失函數
5. 定義優化算法
代碼
定義placeholers與Variables
import tensorflow as tf
# input
x = tf.placeholder(tf.float32, name="x")
# output
y = tf.placeholder(tf.float32, name="y")
# parameter
theta0 = tf.Variable(.5, name="theta0")
theta1 = tf.Variable(.5, name="theta1")
定義模型:
hypothesis = theta0 + theta1 * x
使用平方誤差函數計算損失:
squared_delta = tf.square(hypothesis - y)
loss = tf.reduce_sum(squared_delta) / (2 * 5)
定義優化器:
這裡使用梯度下降算法,學習率設定為0.00001,梯度下降算法會不斷的更新θ0和θ1的值,使損失變小。
learning rate的選取不宜過大或者過小,過大可能導致損失函數無法收斂,過小導致循環的次數增多
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00001)
training = optimizer.minimize(loss)
進行訓練:
dataSet = {x: [40, 65, 80, 115, 150], y: [37.0000, 72.0164, 93.0456, 130.4864, 153.4161]}
session = tf.Session()
init = tf.global_variables_initializer()
session.run(init)
for i in range(1000):
session.run(training, dataSet)
print(session.run([theta0, theta1]))
print(session.run(loss, dataSet))
訓練1000次之後,得到θ0= 0.50757158, θ1 = 1.0718392, 對比之前的θ0= 0.5, θ1 = 0.5,如圖:
可以看出,在訓練1000次之後,得到的擬合函數明顯優於之前最初的函數,將面積90代入擬合函數,得到價格96.97309958,預測90平米房屋售價約等於97萬。
總結
好了,其實房價是很複雜的,不是僅僅跟面積線性相關的,這裡我忽略掉了很多其他因素,將其簡化為一個單特徵的線性迴歸問題,僅僅是用於簡單介紹ML和TensorFlow的一些基本概念和使用,希望給想要了解ML和TensorFlow的你能有一丟丟幫助。
Link:
1.https://www.tensorflow.org/install
2.https://stackoverflow.com/questions/32659348/operation-not-permitted-when-on-root-el-capitan-rootless-disabled/32661637
本文作者:周夢(點融黑幫),就職於點融工程部CRM組,Java程序員一枚,喜歡折騰各種新奇有趣的技術。