iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0
AI & Data

30天搞懂機器學習是否搞錯了什麼系列 第 12

【Day 12】邏輯迴歸(Logistic Regression)(上)

步驟一:Function Set

昨天的最後我們提到我們要找一個事後機率(Posterior Probability),如果它大於 0.5 就輸出 https://chart.googleapis.com/chart?cht=tx&chl=C_1,否則就輸出 https://chart.googleapis.com/chart?cht=tx&chl=C_2。如果是使用Gaussian,則 https://chart.googleapis.com/chart?cht=tx&chl=P_%7Bw%2Cb%7D(C_1%7Cx)%20%3D%20%5Csigma%20(z)https://chart.googleapis.com/chart?cht=tx&chl=z%20%3D%20w%20%5Ccdot%20x%20%2B%20b

  • Function set:https://chart.googleapis.com/chart?cht=tx&chl=f_%7Bw%2Cb%7D(x)%20%3D%20P_%7Bw%2Cb%7D(C_1%7Cx)

如果用圖像化表示則整個模型如下圖所示,將所有的輸入 https://chart.googleapis.com/chart?cht=tx&chl=x_i 分別乘上 https://chart.googleapis.com/chart?cht=tx&chl=w_i 再加上 https://chart.googleapis.com/chart?cht=tx&chl=b 就會得得到 https://chart.googleapis.com/chart?cht=tx&chl=z,即 https://chart.googleapis.com/chart?cht=tx&chl=z%20%3D%20%5Cdisplaystyle%20%5Csum_i%20w_ix_i%20%2B%20b,接著 https://chart.googleapis.com/chart?cht=tx&chl=z 通過Sigmoid function輸出的值就是事後機率,這整件事情我們就稱為邏輯迴歸(Logistic Regression)

邏輯迴歸(Logistic Regression) v.s. 線性迴歸(Linear Regression)

步驟二:Goodness of a Function

訓練資料需要標記(Label),也就是說每個 https://chart.googleapis.com/chart?cht=tx&chl=x 都要標記它屬於哪一個class。假設我們的訓練資料是從事後機率所產生的,只要給我們一個 https://chart.googleapis.com/chart?cht=tx&chl=w%2C%20b 我們就決定了事後機率,就可以去計算某一組 https://chart.googleapis.com/chart?cht=tx&chl=w%2C%20b 產生 https://chart.googleapis.com/chart?cht=tx&chl=N 筆訓練資料的機率,而我們要找一組可以最大化這個機率的 https://chart.googleapis.com/chart?cht=tx&chl=w%5E*%2C%20b%5E*

經過一些數學運算則可以將式子改為 https://chart.googleapis.com/chart?cht=tx&chl=w%5E*%2C%20b%5E*%20%3D%20arg%20%5C%20%5Cdisplaystyle%20%5Cmin_%7Bw%2C%20b%7D%20-lnL(w%2Cb)

整理過後可以發現 https://chart.googleapis.com/chart?cht=tx&chl=%5CSigma 後面那一長串是兩個伯努利分佈(Bernoulli distribution)的交叉熵(Cross-entropy),Cross-entropy代表的涵義是這兩個distribution有多接近,如果兩個分佈是一樣的,那Cross-entropy算出來就會是 0。因此我們在做的就是去算所有樣本的Cross-entropy的總和。

邏輯迴歸(Logistic Regression) v.s. 線性迴歸(Linear Regression)

步驟三:Find the best function

要找到一個最好的function,就要讓 https://chart.googleapis.com/chart?cht=tx&chl=-lnL(w%2C%20b) 最小化,也就是要最小化所有樣本的Cross-entropy的總和。可以用梯度下降法(Gradient Descent),就是做對 https://chart.googleapis.com/chart?cht=tx&chl=w 的偏微分。

經過整理之後會得到 https://chart.googleapis.com/chart?cht=tx&chl=-lnL(w%2C%20b)%20%3D%20%5Cdisplaystyle%20%5Csum_n%20-(%5Chat%20y%20-%20f_%7Bw%2C%20b%7D(x%5En))%20x%5En_i。從括弧中的式子可以看出 https://chart.googleapis.com/chart?cht=tx&chl=w 的更新取決於Learning rate https://chart.googleapis.com/chart?cht=tx&chl=%5Cetahttps://chart.googleapis.com/chart?cht=tx&chl=x_i%5En 以及 https://chart.googleapis.com/chart?cht=tx&chl=%5Chat%20y%5En%20-%20f_%7Bw%2C%20b%7D(x%5En),這個式子代表function的輸出跟理想目標的差距大小,如果離目標越遠,則應該更新越多次參數。

邏輯迴歸(Logistic Regression) v.s. 線性迴歸(Linear Regression)

邏輯迴歸(Logistic Regression) + 方差(Square error)

將邏輯迴歸的式子的輸出減掉 https://chart.googleapis.com/chart?cht=tx&chl=%5Chat%20y 的平方加總當作Loss function,一樣用梯度下降法對 https://chart.googleapis.com/chart?cht=tx&chl=%5CSigma 後面的式子對 https://chart.googleapis.com/chart?cht=tx&chl=w 做偏微分,會得到如下圖所示的結果。

會遇到一個問題就是,假設今天第 https://chart.googleapis.com/chart?cht=tx&chl=n 筆資料是 class 1 https://chart.googleapis.com/chart?cht=tx&chl=%5C%20%5Chat%20y%5En%20%3D%201,當 https://chart.googleapis.com/chart?cht=tx&chl=f_%7Bw%2C%20b%7D(x%5En)%20%3D%201 就代表你很接近你的目標,將它們的值代入式子算出來微分等於 0,而當 https://chart.googleapis.com/chart?cht=tx&chl=f_%7Bw%2C%20b%7D(x%5En)%20%3D%200 就代表你離你的目標還很遠,將它們的值代入式子算出來微分也等於 0。

如果把參數的變化對Total Loss作圖,選擇Cross-entropy跟選擇Square error的參數的變化跟loss的變化如下圖所示。
從圖中可以看到,以Cross-entropy來說距離目標越遠,微分值就越大,參數更新越快,但是如果是Square error,當距離目標很遠的時候,微分卻非常小,就會導致不知道距離目標的遠近。


參考資料

李宏毅老師 - ML Lecture 5


上一篇
【Day 11】分類(Classification)(下)
下一篇
【Day 13】邏輯迴歸(Logistic Regression)(下)
系列文
30天搞懂機器學習是否搞錯了什麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言