Lecture 5: Logistic Regression
Three Steps
Step 1: Function Set
我們想找的是:Pw,b(C1∣x)
fw,b={Pw,b(C1∣x)≥0.5elseoutput: C1,output: C2
Pw,b(C1∣x)=σ(z)=σ(w⋅x+b)
我們會有以下的 Function set(包含各種不同的 w 和 b):
fw,b(x)=Pw,b(C1∣x)

Step 2: Goodness of a Function
若 Training Data 為:
x1C1x2C1x3C2⋯⋯xNC1
接下來一樣要決定一個 function 的好壞,假設 data 是從 fw,b(x)=Pw,b(C1∣x) 產生。
Given a set of w and b, what is its probability of generating the data?
L(w,b)=fw,b(x1)fw,b(x2)(1−fw,b(x3))⋯fw,b(xN)
The most likely w∗ and b∗ is the one with the largest L(w,b):
w∗,b∗=argw,bmaxL(w,b)
求上式等同於求:
w∗,b∗=argw,bmin−lnL(w,b)=argw,bmin−lnfw,b(x1)−lnfw,b(x2)−ln(1−fw,b(x3))⋯−lnfw,b(xN)=argw,bminn∑−[y^nlnfw,b(xn)+(1−y^n)ln(1−fw,b(xn))]
其中,
- y^n:1 for C1, 0 for C2
Σn 項等同於求以下兩個分佈 p、q 的 cross entropy
-
Distribution p:
p(x=1)=y^np(x=0)=1−y^n
-
Distribution q:
q(x=1)=f(xn)q(x=0)=1−f(xn)
H(p,q)=−x∑p(x)ln(q(x))
問題是:為什麼在 Logistic Regression 不用 rms 當 loss function 了?
答:做微分後,某些項次會為 0,導致參數更新過慢。
Step 3: Find the best function
決定完 loss function 後,我們要從一個 set 中,找出 best function,先對 wi 進行偏微分:
∂wi−lnL(w,b)=n∑−[y^n∂wilnfw,b(xn)+(1−y^n)∂wiln(1−fw,b(xn))](*)
其中,
- fw,b(x)=σ(z)=1/(1+e−z)
- z=w⋅x+b=∑iwixi+b
∂wi∂lnfw,b(x)=∂z∂lnfw,b(x)∂wi∂z=∂z∂lnσ(z)⋅xi=σ(z)1∂z∂σ(z)⋅xi=σ(z)1σ(z)(1−σ(z))⋅xi=(1−σ(z))⋅xi=(1−fw,b(x))⋅xi
∂wi∂ln(1−fw,b(x))=∂z∂ln(1−fw,b(x))∂wi∂z=∂z∂ln(1−σ(z))⋅xi=−1−σ(z)1∂z∂σ(z)⋅xi=−1−σ(z)1σ(z)(1−σ(z))⋅xi=−σ(z)⋅xi=−fw,b(x)⋅xi
透過上面計算出來的結果,我們可以對 ∗ 式進行代換:
∂wi−lnL(w,b)=n∑−[y^n∂wilnfw,b(xn)+(1−y^n)∂wiln(1−fw,b(xn))]=n∑−[y^n(1−fw,b(xn))xin−(1−y^n)fw,b(xn)xin]=n∑−[y^n−y^nfw,b(xn)−fw,b(xn)+y^nfW,b(xn)]xin=n∑−(y^n−fw,b(xn))xin
參數更新方式如下:
wi←wi−ηn∑−(y^n−fw,b(xn))xin
Logistic v.s. Linear
下面對 Logistic Regression 和 Linear Regression 做比較:
Why not Logistic Regression with Square Error?
若我們的 loss function 改寫成 square error 版本:
L(f)=21n∑(fw,b(xn)−y^n)2
在 Step 3: Find the best function 對 wi 做微分時:
∂wi∂(fw,b(x)−y^)2=2(fw,b(x)−y^)∂z∂fw,b(x)∂wi∂z=2(fw,b(x)−y^)fw,b(x)(1−fw,b(x))xi
不論 y^n=1 或 y^n=0,都可能導致 ∂L/∂wi=0,無法有效更新參數。
Generative v.s. Discriminative
- Benefit of generative model
- With the assumption of probability distribution,
less training data is needed
- With the assumption of probability distribution, more robust to the noise
- Priors and class-dependent probabilities can be estimated from different sources.
Multi-class Classification
我們用 3 個 classes 來做例子:

Limitation of Logistic Regression

給定上述的 4 個 features,我們無法有效的分類,因為沒有任何線性的切法是可以完美將紅點和藍點分開,因此我們有以下兩種方法:
- Feature Transformation: Not always easy to find a good transformation
- Cascading logistic regression models