日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

week_3

發布時間:2024/4/15 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 week_3 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Andrew Ng機器學習筆記

Week_3 -- -Logistic Regression

This week, we’ll be covering logistic regression. Logistic regression is a method for classifying data into discrete outcomes. In this module, we introduce the notion of classification, the cost function for logistic regression, and the application of logistic regression to multi-class classification.

We’ll introduce regularization, which helps prevent models from over-fitting the training data.


1.Classification

例子:腫瘤與否,垃圾郵件與否

一般可以通過Threshold,閾值,來辨別
例如:0和1的話,可以選擇0.5作為閾值
和回歸問題差不多,不過這個數據是離散數據
For Now,we will focus on the Binary Classification Problem
in which you can take on only two values, 0 and 1. (Most of what we say here will also generalize to the multiple-class case.)

例如:識別垃圾郵件的時候,我們用\(x^{(i)}\) 來表示垃圾郵件的特征量feature, Y則只有2個值,1和0,表示是垃圾郵件和非垃圾郵件。

2.Hypothesis Representation假設函

討論邏輯函數的假設函數----邏輯回歸

首先我們需要的假設函數應該預測值在0到1之間
模仿線性回歸的形式: 
\[h_\theta(x) = g(\theta^T X)\]
其中定義g(Z) = \(\frac1{1+e^{-z}}\)

即是:\(h_\theta(x) = \frac1{1+e^{\theta^T x}}\)
如下圖:

這可以理解為一個概率函數
可以寫為   \(h_\theta(x) = {(y = 1 | x,\theta)}\)

概率參數為\(\theta\)和x,y只有1或者0 。值就是有腫瘤的概率(1)

3. Decision Boundary 決策界限

如上圖所示:0.5即為決策界限
那么如何算出 決策界限呢?

上面是線性的,那么,非線性擬合呢?

那就要通過增加參數\theta

 5. 優化目標,代價函數cost function

目標:如何擬合\(\theta\)??

和線性回歸的代價函數相似:

定義:\(J(\theta) = \frac1m\sum_{i=1}^mCost(h_\theta(x),y)\)

其中Cost 函數可以等于 \(\frac12(h_\theta(x) - y)^2\)

但是,在classification 中,h(x)是非線性的,所以,h(x)圖像可能為:

所以,根據h(x)的定義
可以定義cost函數

它的圖像為:

J(\theta)將會是一個凸函數且沒有局部最優

6.更簡單的方式找到代價函數.用梯度下降來擬合邏輯回歸的參數\(\theta\).Simplified Cost Function and Gradient Descent

  • 代價函數可以不用分段函數:還可以用
    \(Cost(h_\theta(x),y) = -yln(1 - h_\theta(x)) - (1-y)ln(1-h_\theta(x))\)

    來表示,這樣就不用分段了

    所以,代價函數就是:

    或者,用向量來表示:


  • 類似,我們要找到J($\theta $)的最小值

    當然,使用Gradient Descent梯度下降

    如圖操作,注意此時h函數不是和線性回歸的是一樣的

    接下來,如何監測梯度下降?

    當然,也可以用向量方法來實現:?????(如何推導??)

    看這里更清楚:(截圖功能真的是太贊了!)

    7.高級優化Advanced Optimization

    算法優化,Optimization algorithms

    • Gradient descent

    • Conjugate gradient
    • BFGS
    • L-BFGS 共軛梯度算法

    后三種算法的優點:

    • No need to manually pick \(\alpha\)
    • Often faster than gradient descent

    缺點:

    • More complex

    代碼細節不用知道!

    可以以后學習tensorflow 來實現

    8. Multiclass classification

    例如,把郵件貼上不同的標簽

    那么,如何找到多元分類的決策界限?

    例如,3種的話,通過兩兩分類

    一對多方法。分為3個二元問題

    上圖擬合出3個分類器

    \(h_\theta^{(i)}(x) = P(y = i| x,\theta), (i = 1,2,3)\)

    9. 過度擬合的問題over-fitting

    變量太多,無法通過更多的數據來進行約束
    以至于無法泛化到新數據中

    線性:

    第一個是under fitting ,欠擬合,有 high bias

    第二個just right

    第三個則是over fitting

    邏輯回歸:

    如何解決?

    • 減少變量數目 reduce number of features
    • Regularization正則化

    10. Regularization and its Cost function 正則化及其代價函數

    像上節課一樣,當過度擬合的時候,我們可以讓其他的參數的影響盡可能的小

    penalize懲罰\(\theta_3和\theta_4\)這兩個參數。使他們盡可能為0

    如何操作?

    在最初的cost function中添加 正則化項

    \(\lambda\) 叫做正則化參數

    $\lambda $太大的話,會under fitting
    所以應該選擇合適的正則化參數

    一張圖來解釋:

    11. 正則線性回歸Regularized Linear Regression

    算法:

  • Gradient descent
  • 可以等價的寫為:

    \(\theta_j := \theta_j(1- \alpha\frac1m) - \alpha\frac1m\sum_{i=1}^m(h_\theta(x^{(i)}_j)-y^{(i)}) x_j^{(i)}\)
    其中\(1-\alpha\frac1m\)這一項,如果學習率小,例子數量大的化,一般是比1小于一點點的值
    而后面這一大坨,則和以前一模一樣!!!

    只不過前面這一項把theta壓縮了!

  • Normal equation

    使用了正則化,如何得到矩陣式子?

    數學推導略!

  • 或者寫成:

    12. Regularized Logistic Regression 邏輯回歸的正則化

    和線性回歸差不多,要添加正則項

    算法類似,都要將0單獨寫出

    下面來說明如何在更高級的算法中,應用正則化:

    (學完octave后,應該就能看懂)

    綜上所述:




    題目摘錄:

    第 3 題
    Which of the following statements about regularization are true? Check all that apply.

    Using too large a value of λ can cause your hypothesis to overfit the data; this can be avoided by reducing λ.
    Using a very large value of λ cannot hurt the performance of your hypothesis; the only reason we do not set λ to be too large is to avoid numerical problems.
    Consider a classification problem. Adding regularization may cause your classifier to incorrectly classify some training examples (which it had correctly classified when not using regularization, i.e. when λ=0).
    Because logistic regression outputs values 0≤hθ(x)≤1, its range of output values can only be “shrunk” slightly by regularization anyway, so regularization is generally not helpful for it.

    • 答案: 3 * 正則化方法的公式: J(θ)=12m[∑i=1m(hθ(x(i))?y(i))2+λ∑i=1nθ2j]J(θ)=12m[∑i=1m(hθ(x(i))?y(i))2+λ∑i=1nθj2]
    • 選項1: λλ太大導致overfit不對,是underfit,當λλ太大時θ1θ2...θn≈0θ1θ2...θn≈0.只有θ0θ0起作用,擬合出來是一條直線. λλ太小才會導致overfit. 不正確 **
    • 選項2: 同1. 不正確 **
    • 選項3: 當λλ沒有選擇好時,可能會導致訓練效果還不如不加的λλ好. 正確 **
    • 選項4: “shrunk” slightly的是θθ, regularization是想要解決overfit. 不正確 !

    第 1 題
    You are training a classification model with logistic
    regression. Which of the following statements are true? Check
    all that apply.

    Introducing regularization to the model always results in equal or better performance on the training set.
    Adding many new features to the model helps prevent overfitting ont the training set.
    Introducing regularization to the model always results in equal or better performance on examples not in the training set.
    Adding a new feature to the model always results in equal or better performance on the training set.

    • 答案: 4 * 正則化方法的公式: J(θ)=12m[∑i=1m(hθ(x(i))?y(i))2+λ∑i=1nθ2j]J(θ)=12m[∑i=1m(hθ(x(i))?y(i))2+λ∑i=1nθj2]
    • 選項1: 將正則化方法加入模型并不是每次都能取得好的效果,如果λλ取得太大的化就會導致欠擬合. 這樣不論對traing set 還是 examples都不好. 不正確 **
    • 選項2: more features能夠更好的fit 訓練集,同時也容易導致overfit,是more likely而不是prevent. 不正確 **
    • 選項3: 同1,將正則化方法加入模型并不是每次都能取得好的效果,如果λλ取得太大的化就會導致欠擬合. 這樣不論對traing set 還是 examples都不好. 不正確 **
    • 選項4: 新加的feature會提高train set的擬合度,而不是example擬合度. 正確 *[]

    轉載于:https://www.cnblogs.com/orangestar/p/11178192.html

    總結

    以上是生活随笔為你收集整理的week_3的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。