【機器學習】Coursera Machine Learning - Linear Regression 線性迴歸

最近打算系統地過下Coursera公開課平臺上的《Machine Learning》, 剛把前兩週課程的上完,雖然內容簡單,感覺收穫還是挺大的,有些細節Andrew老師是講得非常好。後面計劃針對每次編程作業,將對應的課程內容中,這輪學習中我認爲值得關注的內容記錄如此。

還有因爲自己聽課或做實驗有些筆記是採用的英文,所以博客也會採用中英文混雜的方式,如有讀者看着不便,敬請見諒哈。


一些基本概念

What is machine learning?

  • Arthur Samuel: The field of study that gives computers the ability to learn without being explicitly programmed.
  • Tom Mitchell: A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.

Unsupervised Learning
Unsupervised learning allows us to approach problems with little or no idea what our results should look like. We can derive structure from data where we don’t necessarily know the effect of the variables.

  • Clustering: news search, social network analysis, market segmentation, astronmical data analysis
  • Non-clustering: cocktail party problem

使用基於梯度的優化方法注意事項

在使用梯度下降(SGD)對線性迴歸問題(L2問題, MSE損失函數)進行優化的時候,有兩點需要特別注意:
1. Feature scaling and mean normalization
Make sure features are on a similar scale, so that the gradient can be more consistent. Close to the range of [-1, 1] is just okay.
@Andrew - machine learning
上圖表示的是feature scaling, 此外再結合mean normaliztion, 綜合如下:
xi:=xiuisix_i := \frac{x_i - u_i} {s_i}
Where uiu_i is the average of all the values for feature (i) and sis_i is the range of values (max - min), or sis_i is the standard deviation.

現在流行的Batch normalization的基礎操作也是如此,可以看出掌握機器學習的基本概念和方法是多麼重要,很多先進的算法都是來源於這些基礎知識!

2. learning rate
選擇一個合適的learn rate非常重要,太大了,損失函數很可能發散或者是震盪,太小了,收斂速度就會變得很慢,可以借鑑Andrew老師的方式,每隔3倍取。
在這裏插入圖片描述
在這裏插入圖片描述


正規方程與閉式解

對於這個問題,除了梯度的方法,還可以直接通過normal equation (正規方程) 求解閉式解,式子爲θ=(XTX)1XTy\theta = (X^TX)^{-1}X^Ty ,這個式子可以通過求解最小二乘問題(MSE損失函數)推導得到。此時,feature scaling 就沒有必要採用了。

與基於梯度的方法的對比:當nn非常大時,因爲求逆的原因,閉式解求解將非常耗時,此時就最好用梯度方法,當nn比較小時,直接求解閉式解將更加方便。
在這裏插入圖片描述
如果XTXX^TX不可逆,這個問題就變得麻煩一些了,Andrew老師提出了下面建議:首先查看是否有冗餘的特徵(特徵之間線性相關了),然後看能否刪除一些重要性不大的特徵,或採用正則化。
在這裏插入圖片描述


實驗部分

ex1作業完成後的代碼放在了github這個頁面上,需要的同學可以查看。

  • Learn rate
    The learn rate influence the convergence speed of the optimization. Following figure (1) illustrates the convergence curves along with different learn rates for the linear regression problem.
    Figure 1
  • Feature scaling and mean normalization
    上面那個圖是對特徵進行了歸一化的,這裏我去掉歸一化再來使用基於梯度的優化看看。

Following experiments (Figure 2) setting is without feature normalization, in order to converge, the learn rate should be very small because of the extreme sharp loss slope.
Figure 2
Although the Figure 1 and 2 look very close, the final loss after 500 epochs are different, where is 2.0433e9 for Figure 1 and 2.3978e9 for Figure 2.

最後我們在將這兩種方式與閉式解的方法進行對比,採用一個test data (means 1650 sq-ft, 3 br house) ,結果如下:

Method Prediction
SGD w/o feature normalization $272882.552145
SGD w feature normalization $293081.464335
Normal equation (analytic method) $293081.464338

可以看出,使用了feature normalization的梯度優化的結果,跟閉式解結果幾乎一樣,而沒有使用feature normalization的結果就差不少!可以看出因爲沒有采用feature normalization,其學習速率必須設得非常小,優化時就會可能會停留在最優解的附近,很難前進到最優解。


最後多說一句,最小二乘問題是機器學習中非常經典和基礎的問題,它涉及到非常多的知識點,如機器學習中的損失函數、優化方法(基於梯度的,基於數值的),利用矩陣分析中的知識(僞逆、值域、張成子空間、正交投影矩陣、超定方程)來理解最小二乘的幾何意義,與最大似然之間的聯繫(誤差的先驗分佈),不同的正則化方式(L2約束-嶺迴歸,L1約束-Lasso迴歸)等等,後面有機會專門寫個總結下,這裏暫時不表。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章