week2,有監督學習(的線性機器學習模型)

2.1 有監督學習的描述:

有監督學習 是比較經典的一個機器學習場景。有監督學習可以被這樣描述:

(1)Supervised learning is the machine learning task of inferring a function from labeled training data.[1] -監督學習完成“從labeled training data中推斷出函數”的任務。
(2)labeled training data是由一對(input object, expected output)組成的數據。
(3)從labeled training data中學習到的函數可以將unseen instance的輸入映射到輸出(舉個例子: 對于inferred function來說,我們再給它一個 input object 它就能夠輸出一個新的 expected output)。這種暗含在函數中的映射邏輯被稱作inductive bias。(最簡單的inductive bias就是occam's razor - Occam's razor, assuming that the simplest consistent hypothesis about the target function is actually the best. Here consistent means that the hypothesis of the learner yields correct outputs for all of the examples that have been given to the algorithm.)

最常用到的inductive bias:
Maximum conditional independence: if the hypothesis can be cast in a Bayesian framework, try to maximize conditional independence. This is the bias used in the Naive Bayes classifier.
Minimum cross-validation error: when trying to choose among hypotheses, select the hypothesis with the lowest cross-validation error. Although cross-validation may seem to be free of bias, the "no free lunch" theorems show that cross-validation must be biased.
Maximum margin: when drawing a boundary between two classes, attempt to maximize the width of the boundary. This is the bias used in support vector machines. The assumption is that distinct classes tend to be separated by wide boundaries.
Minimum description length: when forming a hypothesis, attempt to minimize the length of the description of the hypothesis. The assumption is that simpler hypotheses are more likely to be true. See Occam's razor.
Minimum features: unless there is good evidence that a feature is useful, it should be deleted. This is the assumption behind feature selection algorithms.
Nearest neighbors: assume that most of the cases in a small neighborhood in feature space belong to the same class. Given a case for which the class is unknown, guess that it belongs to the same class as the majority in its immediate neighborhood. This is the bias used in the k-nearest neighbors algorithm. The assumption is that cases that are near each other tend to belong to the same class.

2.2 有監督學習(supervised learning)的主要步驟:

(1)任務導向部分:
確定監督學習任務

  • 分類(二分類,多分類),多分類比二分類復雜在哪里?
  • 回歸(預測問題)

確定訓練集的數據類型(圖像,數字,音頻?)

(2)數據工程部分:
收集訓練集,數據預處理

(3)特征工程部分:
從數據集中選擇有可能有用的特征,特征預處理, curse of dimensionality;

(4)模型工程部分:
根據任務特點選擇有監督學習算法(不同的監督學習算法定義了不同的函數式,這里又叫hypothesis) ,如何合理地選擇模型,衡量不同模型的優缺點參考:No free lunch theorem

(5)模型訓練工程:
訓練模型,計算cost function,計算optimization function,可視化訓練結果,把握好underfitting和overfitting,調整模型參數,繼續訓練模型

(6)模型評估工程:
評估模型,交叉驗證,模型泛化能力 cross-validation.

這些步驟里面, “訓練,計算cost function,使用optimization function” 這三步是需要迭代多次才能得到優秀結果的。
此外

  • 在有監督學習下面 使用的 cost function 和 optimization function的種類有很多.。
  • 而且對于同一種cost function 或 optimization function 在不同的有監督學習算法下面也有些許的不同之處,主要還是因為模型不一樣,然后cost function 和 optimization function 對于不同模型 也改變了格式。(主要是要掌握每種cost function和optimization function的核心思想)

2.2.1 任務導向

2.1.1 分類任務
(1) 分類任務有兩種:

分類任務有兩種,其一是二分類任務,其二是多分類任務。
通常在實際應用中,都是多分類任務。

2.2.2 數據工程

2.2.3 特征工程

2.2.4 模型工程部分

2.2.4.1 分類模型
(1) 分類模型有兩種:

分類任務有兩種,其一是二分類任務,其二是多分類任務。
有監督學習里面可以的分類器,有適合做二分類的分類器(logistic regression, basic SVM)和天生就適合做多分類的分類器(K近鄰,神經網絡,樸素貝葉斯,決策樹,拓展SVM)。

  • 分類常用模型
  • Linear classifiers
  • Logistic regression
  • Naive Bayes classifier
  • Support vector machines
    • SVM with no kernel(又可以稱作: SVM with linear kernel) 也是線性分類器
    • SVM with Gaussian kernel 作為非線性分類器
  • k-nearest neighbor
  • Decision trees
    • Random forests
  • Neural networks
2.2.4.2使用分類模型執行分類任務
(1) 執行二分類任務:

二分類任務是最直接的分類任務,同時也是多分類任務的基礎。
二分類任務主要步驟:

  • 選擇分類模型(如:logistic regression, support vector mahine)
  • 設定類別(比如:我們有第0類和第1類)
  • 設定二分類輸出閾值(比如:設定logistic function的閾值為0.5)
  • 設定二分類規則(比如:輸出小于閾值則分為第0類,輸出大于閾值則分為第1類)。

舉個例子:
<1> 選擇分類模型:
假設使用logistic regression作二分類問題
logistic regression的hypothesis(即模型的函數表達式)是:

(theta 和 x都是 N維向量,N代表輸入特征個數)

其hypothesis的函數圖像是:


(縱軸是h_theta(x),橫軸是 -theta*x)

<2> 設定有第0類和第1類:


<3> 我們設定閾值為: h_theta(x)=0.5。 分類規則為:小于0.5算作第0類, 大于0.5算作第1類。則此時可知:

  • 一次函數thetaX=0是當前二分類問題的decision boundary。該函數圖像的特征由具體從訓練中學習到的theta參數值決定。
  • 當thetaX < 0時: 當前訓練樣本X(i)被分作第0類
  • 當thetaX > 0時: 當前訓練樣本X(i)被分作第1類。
    此時我們可以畫出關于theta*X=0的函數,并稱該函數為decision boundary,可以發現,decision boundary將其左右兩邊分作了不同類,完成了分類任務,至于分類效果,就涉及了如何調參等問題。(以theta, X二維為例):


    二分類decision boundary圖像
(2) 執行多分類任務

多分類任務的重點在于‘進行多分類的手段’,常見應用于解決多分類問題的方法有三種:

The existing multi-class classification techniques can be categorized into (i) Transformation to binary (ii) Extension from binary and (iii) Hierarchical classification.[1] -三種解決多分類問題的方法:
1.將多分類問題拆分成多個二分類問題(常見的手段有:one-vs-all/又叫one-vs-rest, 和 one-vs-one又叫all-vs-all)
2.將二分類技術拓展成多分類技術(如:拓展SVM,神經網絡)
3.進行層次分類

<1> 將多分類問題拆成多個二分類問題:

one-vs-all(one-vs-rest)

one-vs-all的主要思想就是:

  • 對于一個N分類問題,為N個類別都構建一個二分類器(所以one-vs-all一共產生N個分類器),使得每個分類器將當前類別分作一類、其余類別統一算作另一類。
  • 對一個new instance進行分類時,需要將該instance帶入N個分類器中進行計算,然后選取分類器輸出最大的那個類作為分類結果。(值得注意的是:進行one-vs-all分類時,分類器必須輸出real-valued confidence score而不僅僅是class label,因為,對于一個new instance來說,N個分類器的分類結果多多少少會有不精確、重疊的地方,假設讓one-vs-all中的分類器都以Label的形式進行輸出的話,最后可能同一個instance會有幾個分類器都輸出1,此時就不好判斷new instance到底屬于哪個類別)

one-vs-one(all-vs-all)

one-vs-one的主要思想就是:

  • 對于一個N分類問題,我們需要以排列組合的方式,為N個類中每2個類構造一個分類器,one-vs-one的分類器輸出的是class label,并不需要real-valued confidence score(所以one-vs-one分類器一共產生N(n-1)/2個分類器)
  • 對一個new instance進行分類時,需要將該instance帶入N(N-1)/2個分類器中分別計算。顯然,N(N-1)/2個分類器會針對該new instance到底屬于N個類中的哪個類別進行投票,我們選擇票數最高的類別作為當前instance的類型。

<2> 二分類模型拓展成多分類模型:
以下分類器可以直接進行多分類:

  • Neural networks
  • Extreme learning machines
  • k-nearest neighbours
  • Naive Bayes
  • Decision trees
  • Support vector machines

<3> 進行層次分類:

Hierarchical classification tackles the multi-class classification problem by dividing the output space i.e. into a tree. Each parent node is divided into multiple child nodes and the process is continued until each child node represents only one class. Several methods have been proposed based on hierarchical classification.
--層次分類法首先將所有類別分成兩個子類,再將子類進一步劃分成兩個次級子類,如此循環,直到得到一個單獨的類別為止。對層次支持向量機的詳細說明可以參考論文《支持向量機在多類分類問題中的推廣》

2.2.4.3 分類模型詳解

(1) Logistic Regression(邏輯回歸)
Logistic Regression是由一種基于概率解釋的線性分類模型,可以解決:
<1> 二分類問題(使用sigmoid function將函數值映射到(0,1)概率區間內)
<2> 多分類問題(需要用到one-vs-all或者one-vs-one)。
Logistic Regression的模型(hypothesis)可以說是用 sigmoid function套上Linear Regression的hypothesis得來的。

  • Logistic Regression的hypothesis:

  • 假設Linear Regression的hypothesis為:


    theta和x為N維向量,代表訓練樣本有N維特征
  • Sigmoid Function(又叫Logistic Function)為:


  • 將Linear Regression的hypothesis作為z帶入Sigmoid Function中即得到Logistic Regression的hypothesis:


    其hypothesis的函數圖像是:
    (縱軸是h_theta(x),橫軸是 theta*x)

    ps: Logistic Regression的sigmoid function值域不是固定的而是可變化的,這樣Logistic Regression的hypothesis的值域也是可以調整的,調整過函數外型的hypothesis,可以讓Logistic Regression的分類功能更定制化,比如:用1表示positive class 而用 -1表示negative class(而且這里值得注意的是,當hypothesis改變時,cost function的表達式同樣也要重新推理過,optimization function表達式也要重新推理過)。
    舉個例子:
    <1>使用這樣一個sigmoid function作為Logistic Regression hypothesis的外框架:該sigmoid function以[-1,1]為值域,使用-1表示negative class,使用1表示positive class。


    <2>此時Logistic Regression的hypothesis表達式則變化成:

    <3>此時cost function應該是:(這里是暫時猜測,還沒驗證對不對)

  • Logistic Regression基于概率解釋二分類問題
    對于一個訓練樣本的特征向量X(i), Logistic Regression hypothesis current的參數向量是theta(i),那么hypothesis的計算結果可以表示為:


    hthetax的值就是使用當前模型Logistic Regression和參數theta估計的,樣本i屬于y=1(第一類)類的概率值P。

  • Logistic Regression hypothesis的閾值與分類,Decision Boundary
    <1>閾值與分類
    二分類問題對訓練樣本進行分類的決策邏輯在于‘怎樣判別樣本i是屬于1類(positive class)還是0類(negative class)’。Logistic Regression是基于概率解釋的分類模型,顯然地,可以通過設定閾值p,當樣本i關于hypothesis的結果值(概率值)大于p時,我們說樣本i被分作1類。當結果值小于p時,我們說樣本i被分作0類。
    <2>閾值與Decision Boundary
    前面說過,Logistic Regression hypothesis的閾值大小決定了二分類時的決策邏輯,也就是說通過確定樣本i的hypothesis結果值確定了樣本i該屬于哪一類。同樣的,閾值和當前訓練參數的值共通以Decision Boundary的形式,將分類結果體現在了圖像上面。
    舉個例子:
    使用Logistic Regression進行二分類問題,定義閾值為:

    閾值

    當htheta大于0.5時,樣本i屬于第1類(positive class), 當htheta小于0.5時,樣本i屬于第0類(negative class)。
    我們已知Logistic Regression hypothesis的函數圖像是:
    (縱軸是h_theta(x),橫軸是 theta*x)

    因此,我們可以知道關于函數thetaX的閾值是:

    對于訓練樣本i,如果:

    則訓練樣本i的ththeta大于0.5,訓練樣本i為第1類(positive class)。
    而如果:

    則訓練樣本i的ththeta小于0.5,訓練樣本i為第0類(negative class)。
    此外,因為當前訓練好的參數值theta是已知的,所以我們可以直接將關于thetaX=0的函數畫出來:

    那么顯然,在thetaX=0函數右側的都是帶入thetaX大于0的訓練樣本i,因此會被分為positive class。在thetaX=0函數左側的都是帶入thetaX小于0的訓練樣本i,因此會被分為negative class。
    綜上,Decision Boundary是由hypothesis的閾值以及當前訓練好的關于hypothesis的參數共通決定。同時,Decision Boundary也從函數圖像一側體現了當前Logistic Regression的配置下(閾值和參數已定)的分類結果。

  • Logistic Regression的cost function
    cost function作為機器學習算法中的optimization objective(優化函數進行優化的目標)是必須有的,并且cost function的優劣直接影響了機器學習的結果。
    對于Logistic Regression來說,通常使用-log表達它的cost function。對于單個訓練樣本,有cost function方程組:


    根據數學上的技巧,可以寫出上述方程組的等價表達式:

    因此,假設訓練集有m個訓練樣本,那么,Logistic Regression的cost function可以表示為:


    要理解為什么用log函數表達Logistic Regression的cost function,就要聯系hypothesis的圖像與log圖像的關系:
    我們已知Logistic Regression hypothesis的函數圖像是:
    (縱軸是h_theta(x),橫軸是 theta*x)

    -log(x)函數的圖像是:

    因此:
    <1>當excepted output: y = 1時,hypothesis函數值越接近1,Logistic Regression 的 cost function值越小;hypothesis函數值越接近0,Logistic Regression的cost function值接近無窮大。
    <2>當excepted output: y = 0時,hypothesis函數值越接近0,則1-hypothesis的值越接近1,此時Logistic Regression的cost function值越小;hypothesis函數值越接近1, 則1-hypothesis的值越接近0,此時Logistic Regression的cost function值接近無窮。
    這樣一來,cost function的值正好表示了當前Logistic Regression分類效果的好與壞。

  • Logistic Regression的optimization function
    優化Logistic Regression的cost function,可以使用經典的Gradient descent方法。
    對于cost function:


    我們想要:

    則要:
    alpha是learning rate

    偏導解出來是:

  • 關于Logistic Regression的優化函數:

  • Gradient Descent

  • Conjugate Gradient

  • BFGS

  • L-BFGS
    conjugate gradient, BFGS和L-BFGS的優點都是不需要自己選取learning rate alpha

(2) Support Vector Machine(支持向量機)
支持向量機作為同時支持線性和非線性分類的分類模型,也支持二分類和多分類問題。SVM的算法模型

2.2.4.4 回歸任務(經常用于預測) !!!!這里涉及大量統計學,還需要學習后補全知識點

回歸的定義:回歸是一種估計自變量(independent variable)和因變量(dependent variable)之間的對應關系的統計學方法。- 以統計學的角度說,回歸就是:regression analysis estimates the conditional expectation of the dependent variable given the independent variables。

  • 回歸函數:通過回歸的方法得到的:“由自變量估計因變量的函數”就叫做Regression function。
  • 概率散布:如果將Regression function和訓練樣本點畫出來,那么訓練樣本的dependent variable值在Regression function周圍的散布叫做probability distribution
  • 用于預測:由于回歸的特性(估計函數自變量和因變量之間的變化關系),所以回歸主要被用來做預測任務

2.2.5 模型訓練工程

監督學習的cost function(還需完善)

In statistics, typically a loss function is used for parameter estimation, and the event in question is some function of the difference between estimated and true values for an instance of data. supervised learning tasks such as regression or classification can be formulated as the minimization of a loss function over a training set. Hence,The loss function quantifies the amount by which the prediction deviates from the actual values. --在統計學中,loss function被用在參數估計中。而監督學習的方法,從參數估計的角度來說可以被定義為:“監督學習就是那些通過不斷修改自身模型(函數)的參數從而達到滿意效果的regression 或者 classification任務”。 因此,監督學習天然地需要借用cost function作為參數估計的手段,使用cost function去估量:當前的監督學習模型的參數估計的結果和真實參數的差距。

常見的Loss function:
包括log對數損失函數,平方損失函數,指數損失函數,Hinge損失函數

常見的optimization function(還需完善)

我目前知道的包括 梯度下降, normal equation, conjugate gradient, BFGS, L-BFGS

常見的有監督學習算法:
hypothesis

hypothesis在這里被用在有監督學習的線性學習方法的函數表示上。
比如: linear regression 的 hypothesis 就可以是:


這是一個具有一維特征的linear regression hypothesis。
其中, theta代表hypothesis中的參數,x uppercase i代表第i個樣本的輸入特征值。

損失函數詳解
  • 目前所知道的,損失函數對于 有監督學習(包括回歸,分類和深度學習)來說是重要的,而對于其他機器學習場景,暫時不清楚是不是需要用到損失函數(比如:無監督學習貌似是不需要用到損失函數的)。
  • 損失函數的作用: 有監督學習通過計算“訓練過程中得到的真實輸出(the practical output)” 和 訓練樣本的標簽值(期望輸出值expected output) 之間的距離(差距),將得到的距離(差距)作為調整整個模型的參照。對于優化函數(optimization function)來說,損失函數計算的值就是它們的輸入值,不管是哪種優化函數,其核心的優化思想就是:“根據損失值,合理地、更好地調整整個模型的表現P”。
  • 損失函數是關于參數theta的函數,hypothesis是關于樣本輸入特征x的函數。
線性有監督機器學習后三步的細節
  • 首先,確認有監督機器學習的hypothesis,比如,以m個具有三個特征的樣本的訓練集和linear
    regression算法為例:

我們后面為了cost function 和 optimization function方便,需要假設x0 = 1.
所以我們有:

  • 然后,根據hypothesis,和需要使用哪種cost function類型以及將會結合的optimization function,確認當前cost function的式子。比如,我們這里使用squared error function作為cost function,后面使用gradient descent作為optimization function,則我們有:


其中,y uppercase i值得是對應于第i個輸入訓練樣本的期望輸出expected output。
這里設置1/2m而不是1/m的原因是我們后面會使用gradient descent作為optimization function,而gradient descent其中有求導數的步驟,那么指數2和分數1/2就會抵消掉,這樣就簡化了計算。

  • 現在,我們確認optimization function的格式,由于我們用的是gradient descent,所以有:

注意,對于所有的theta 我們需要同時進行更新。

  • 最后根據之前的cost function 和 optimization function,同時計算出了最新的theta參數。
    然后再次重復以上步驟,計算cost function 和 optimization function,更新theta參數,直到cost function 計算出的loss 小于我們預先設定的閾值
image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容