1. 程式人生 > >吳恩達機器學習 Coursera 筆記(二) - 單變數線性迴歸

吳恩達機器學習 Coursera 筆記(二) - 單變數線性迴歸

Model and Cost Function

1 模型概述 - Model Representation

To establish notation for future use, we’ll use

  • x(i)
  1. the “input” variables (living area in this example), also called input features, and
  • y(i)
  1. the “output” or target variable that we are trying to predict (price).

A pair (x(i),y(i)) is called a training example
the dataset that we’ll be using to learn—a list of m training examples (x(i),y(i));i=1,...,m—is called a training set.
the superscript “(i)” in the notation is simply an index

into the training set, and has nothing to do with exponentiation

  • X
  1. the space of input values
  • Y
  1. the space of output values

In this example

X = Y = R



To describe the supervised learning problem slightly more formally, our goal is,
given a training set, to learn a function h : X → Y

so that h(x) is a “good” predictor for the corresponding value of y.
For historical reasons, this function h is called a hypothesis. Seen pictorially, the process is therefore like this

  • regression problem
    When the target variable that we’re trying to predict is continuous, such as in our housing example
  • classification problem
    When y can take on only a small number of discrete values (such as if, given the living area, we wanted to predict if a dwelling is a house or an apartment, say)

簡單的介紹了一下資料集的表示方法,並且提出來h(hypothesis),即通過訓練得出來的一個假設函式,通過輸入x,得出來預測的結果y。並在最後介紹了線性迴歸方程

2 代價函式 - Cost Function

代價函式是用來測量實際值和預測值精確度的一個函式模型.
We can measure the accuracy of our hypothesis function by using a cost function.
This takes an average difference (actually a fancier version of an average) of all the results of the hypothesis with inputs from x's and the actual output y's.


首先需要搞清楚假設函式和代價函式的區別
當假設函式為線性時,即線性迴歸方程,其由兩個引數組成:theta0和theta1


我們要做的就是選取兩個引數的值,使其代價函式的值達到最小化

J(θ0,θ1)=12m∑i=1m(y^i−yi)2=12m∑i=1m(hθ(xi)−yi)2

To break it apart, it is 1/2 x ̄ where x ̄ is the mean of the squares of hθ(xi)−yi , or the difference
between the predicted value and the actual value.
This function is otherwise called theSquared error function, or Mean squared error.
The mean is halved (1/2)as a convenience for the computation of the gradient descent, as the derivative term of the square function will cancel out the 1/2 term.
The following image summarizes what the cost function does:


3 代價函式(一)



If we try to think of it in visual terms, our training data set is scattered on the x-y plane.
We are trying to make a straight line (defined by hθ(x)) which passes through these scattered data points.

Our objective is to get the best possible line. The best possible line will be such so that the average squared vertical distances of the scattered points from the line will be the least.

Ideally, the line should pass through all the points of our training data set. In such a case, the value of J(θ0,θ1) will be 0.

The following example shows the ideal situation where we have a cost function of 0.

When θ1=1, we get a slope of 1 which goes through every single data point in our model.
Conversely, when θ1=0.5, we see the vertical distance from our fit to the data points increase.

This increases our cost function to 0.58. Plotting several other points yields to the following graph:
Thus as a goal, we should try to minimize the cost function. In this case, θ1=1 is our global minimum.



4 代價函式(二)


  • 等高線圖是包含許多等高線的圖形,雙變數函式的等高線在同一條線的所有點處具有恆定值
    採用任何顏色並沿著'圓',可得到相同的成本函式值
  • 當θ0= 800且θ1= -0.15時,帶圓圈的x顯示左側圖形的成本函式的值
    取另一個h(x)並繪製其等高線圖,可得到以下圖表


例如,在上面的綠線上找到的三個紅點具有相同的J(θ0,θ1)值,因此,它們能夠被沿著同一條線找到

  • 當θ0= 360且θ1= 0時,等高線圖中J(θ0,θ1)的值越接近中心,從而降低了成本函式誤差
    現在給出我們的假設函式略微正斜率可以更好地擬合數據。

上圖儘可能地使成本函式最小化,因此,θ1和θ0的結果分別約為0.12和250。
在我們的圖表右側繪製這些值似乎將我們的觀點置於最內圈“圓圈”的中心。

5 梯度下降 - Gradient Descent



對於假設函式,我們有一種方法可以衡量它與資料的匹配度
現在我們需要估計假設函式中的引數。
這就是梯度下降使用到的地方。
想象一下,我們根據其欄位θ0和θ1繪製我們的假設函式(實際上我們將成本函式繪製為引數估計函式)
我們不是繪製x和y本身,而是我們的假設函式的引數範圍以及選擇一組特定引數所產生的成本值
我們將θ0放在x軸上,θ1放在y軸上,成本函式放在z軸
我們的圖上的點將是使用我們的假設和那些特定的θ引數的成本函式的結果

我們知道,當我們的成本函式位於圖中凹坑的最底部時,即當它的值是最小值時,我們已經成功了
紅色箭頭顯示圖表中的最小點。
我們這樣做的方法是採用我們的成本函式的導數(一個函式的切線)
切線的斜率是該點的導數,它將為我們提供一個朝向的方向
我們在最陡下降的方向上降低成本函式

每個步驟的大小由引數α確定,該引數稱為學習率
例如,上圖中每個“星”之間的距離表示由引數α確定的步長
較小的α將導致較小的步長,較大的α將導致較大的步長
採取步驟的方向由J(θ0,θ1)的偏導數確定,根據圖表的開始位置,可能會在不同的點上結束
兩個不同的起點,最終出現在兩個不同的地方.
梯度下降演算法:重複直到收斂


θj:=θj−α∂∂θjJ(θ0,θ1)

where
j=0,1 represents the feature index number.
在每次迭代j,應該同時更新引數θ1,θ2,...,θn。
在第j次迭代計算另一個引數之前更新特定引數將導致錯誤的實現。

6 梯度下降知識點總結


在本文,我們探討了使用一個引數θ1並繪製其成本函式以實現梯度下降的場景
對單個引數的公式是:重複直到收斂

θ1:=θ1−αddθ1J(θ1)


無論ddθ1J(θ1)的斜率符號如何,θ1最終會收斂到其最小值
下圖顯示當斜率為負時,θ1的值增加,為正時,θ1的值減小
斜率為正值
斜率為負值

Choose Learning Rate α

另外,我們應該調整引數α以確保梯度下降演算法在合理的時間內收斂。
沒有收斂或太多時間來獲得最小值意味著我們的步長是錯誤的。

  • 如果J(θ)在下降,但是下降的速度很慢的話,就需要增大學習率α,因為每一步走的都太短了,導致到達最優解的速度下降,即收斂速度下降。

  • 因為α大的原因,每次都一步跳過了最優解點,導致距離最優解越來越遠,J(θ)不斷上升


梯度下降如何由一個定步長的α收斂?

收斂背後的直覺是當我們接近凸函式的底部時ddθ1J(θ1)接近0。
至少,導數總是0

因此我們得到:

θ1:=θ1−α∗0

7 線性迴歸的梯度下降

回顧下之前所學

  • 梯度下降演算法
  • 線性迴歸模型

    • 線性假設
    • 平方差代價函式

我們要做的就是將梯度下降演算法應用於線性迴歸模型的平方差代價函式
其中關鍵的是這個導數項

當具體應用於線性迴歸的情況時,可以匯出梯度下降方程的新形式
我們可以替換我們的實際成本函式和我們的實際假設函式,並將等式修改為:

repeat until convergence: {θ0:=θ1:=}θ0−α1m∑i=1m(hθ(xi)−yi)θ1−α1m∑i=1m((hθ(xi)−yi)xi)

derivation of ∂∂θjJ(θ) for a single example :
其中m是訓練集的大小
θ0是一個常數,它將與給定訓練集(資料)的θ1和xi,yi 的值同步變化
注意,我們已經將θj的兩種情況分離為θ0和θ1的兩種情況的偏導數方程

而對於θ1,由於導數,我們在末尾乘以xi
以下是一個單個例子的∂∂θjJ(θ)的推導:

所有這一切的要點是,如果我們從某個猜想開始,然後重複應用這些梯度下降方程,我們的假設將變得越來越準確
因此,這只是原始成本函式J的梯度下降
該方法在每個步驟中檢視整個訓練集中的每個示例,並稱為批量梯度下降

需要注意的是,雖然梯度下降一般對區域性最小值敏感,但我們線上性迴歸中提出的優化問題只有一個全域性,而沒有其他區域性最優; 因此,梯度下降總是收斂(假設學習率α不是太大)於全域性最小值
實際上,J是凸二次函式。 下面是梯度下降的示例,因為它是為了最小化一個二次函式而執行的

上面顯示的橢圓是二次函式的輪廓
還示出了梯度下降所採用的軌跡,其在(48,30)處初始化
圖中的x(由直線連線)標記了漸變下降經歷的θ的連續值,當它收斂到其最小值時