文章目录
第10章:支持向量机
10.1 代价函数 Cost function
支持向量机SVM(Support Vector Machine)可以用于分类和回归。SVM将向量映射到高维空间中,在空间中建立一个最大间隔的超平面,这个超平面两边建有两个相互平行的分开数据的超平面,使得其与中间的超平面距离最大化。
在逻辑回归中,代价函数为: J ( θ ) = − 1 m ∑ i = 1 m [ y ( i ) l o g ( h θ ( x ( i ) ) ) + ( 1 − y ( i ) ) l o g ( 1 − h θ ( x ( i ) ) ) ] + λ 2 m ∑ j = 1 n θ j 2 J(\theta) = -\frac{1}{m}\sum_{i=1}^m[y^{(i)}log(h_\theta(x^{(i)}))+(1-y^{(i)})log(1-h_\theta(x^{(i)}))]+\frac{\lambda}{2m}\sum_{j=1}^n\theta_j^2 J(θ)=−m1i=1∑m[y(i)log(hθ(x(i)))+(1−y(i))log(1−hθ(x(i)))]+2mλj=1∑nθj2其中,当 y = 1 y=1 y=1或 y = 0 y=0 y=0时,代价函数图像分别如下图所示:
|
|
对其进行修改,得到SVM的代价函数:
J
(
θ
)
=
C
∑
i
=
1
m
[
y
(
i
)
c
o
s
t
1
(
θ
T
x
(
i
)
)
+
(
1
−
y
(
i
)
)
c
o
s
t
0
(
θ
T
x
(
i
)
)
]
+
1
2
∑
j
=
1
n
θ
j
2
J(\theta) =C\sum_{i=1}^m[y^{(i)}cost_1(\theta^Tx^{(i)})+(1-y^{(i)})cost_0(\theta^Tx^{(i)})]+\frac{1}{2}\sum_{j=1}^n\theta_j^2
J(θ)=Ci=1∑m[y(i)cost1(θTx(i))+(1−y(i))cost0(θTx(i))]+21j=1∑nθj2其中,
C
C
C是一个类似权重的系数,
c
o
s
t
1
(
θ
T
x
)
,
c
o
s
t
0
(
θ
T
x
)
cost_1(\theta^Tx),cost_0(\theta^Tx)
cost1(θTx),cost0(θTx)的函数图像如下图所示:
于是对代价函数的要求如下:
{
i
f
y
=
1
⇒
θ
T
x
≥
1
i
f
y
=
0
⇒
θ
T
x
≤
−
1
\begin{cases} if \ y=1\Rightarrow \theta^Tx\geq 1 \\ if \ y=0\Rightarrow \theta^Tx\leq -1 \end{cases}
{if y=1⇒θTx≥1if y=0⇒θTx≤−1
注:如果
C
C
C太大,会导致SVM代价函数前一部分的值很小,后一部分的值很大,容易造成过拟合(对异常点敏感)。
10.2 假设函数 Hypothesis
SVM的假设函数为: h θ ( x ) = { 1 , i f θ T x ≥ 0 0 , e l s e h_\theta(x)=\begin{cases} 1,\ if \ \theta^Tx \geq 0 \\ 0,\ else \end{cases} hθ(x)={1, if θTx≥00, else
10.3 范数表示
设向量
u
=
[
u
1
u
2
]
,
v
=
[
v
1
v
2
]
u=\begin{bmatrix} u_1 \\ u_2 \end{bmatrix},v=\begin{bmatrix} v_1 \\ v_2 \end{bmatrix}
u=[u1u2],v=[v1v2],如下图所示:
∥
u
∥
=
u
1
2
+
u
2
2
\Vert u\Vert=\sqrt{u_1^2+u_2^2}
∥u∥=u12+u22称为
u
u
u的范数,即
u
u
u的长度;
p
p
p为
v
v
v在
u
u
u上的投影长度;满足
u
T
v
=
p
⋅
∥
u
∥
u^Tv=p \cdot\Vert u \Vert
uTv=p⋅∥u∥。
所以,SVM代价函数的后一部分可以表示如下: 1 2 ∑ j = 1 n θ j 2 = 1 2 ( θ 1 2 + θ 2 2 + ⋯ + θ n 2 ) = 1 2 ( θ 1 2 + θ 2 2 + ⋯ + θ n 2 ) 2 = 1 2 ∥ θ ∥ 2 \frac{1}{2}\sum_{j=1}^n\theta_j^2=\frac{1}{2}(\theta_1^2+\theta_2^2+\cdots+\theta_n^2)=\frac{1}{2}(\sqrt{\theta_1^2+\theta_2^2+\cdots+\theta_n^2})^2=\frac{1}{2}\Vert \theta \Vert^2 21j=1∑nθj2=21(θ12+θ22+⋯+θn2)=21(θ12+θ22+⋯+θn2)2=21∥θ∥2
则代价函数的要求可以表示为: { i f y = 1 ⇒ p ⋅ ∥ θ ∥ ≥ 1 i f y = 0 ⇒ p ⋅ ∥ θ ∥ ≤ − 1 \begin{cases} if \ y=1\Rightarrow p \cdot\Vert \theta \Vert\geq 1 \\ if \ y=0\Rightarrow p \cdot\Vert \theta \Vert\leq -1 \end{cases} {if y=1⇒p⋅∥θ∥≥1if y=0⇒p⋅∥θ∥≤−1
10.4 高斯核函数 Gaussian Kernel
已知,在线性SVM中,计算的是 θ T x \theta^Tx θTx,如果对其进行修改,计算 θ T f \theta^Tf θTf ,则是高斯核函数的SVM, f f f的定义如下: f 1 = s i m i l a r i t y ( x , l ( 1 ) ) = e x p ( − ∥ x − l ( 1 ) ∥ 2 2 σ 2 ) f_1=similarity(x,l^{(1)})=exp(-\frac{\Vert x-l^{(1)} \Vert^2}{2\sigma^2}) f1=similarity(x,l(1))=exp(−2σ2∥x−l(1)∥2) f 2 = s i m i l a r i t y ( x , l ( 2 ) ) = e x p ( − ∥ x − l ( 2 ) ∥ 2 2 σ 2 ) f_2=similarity(x,l^{(2)})=exp(-\frac{\Vert x-l^{(2)} \Vert^2}{2\sigma^2}) f2=similarity(x,l(2))=exp(−2σ2∥x−l(2)∥2) ⋯ \cdots ⋯其中, l l l 称为标记点, l ( 1 ) , l ( 2 ) , ⋯ , l ( m ) l^{(1)},l^{(2)},\cdots,l^{(m)} l(1),l(2),⋯,l(m),每一个标记点与每一个样本数据在空间中位于相同位置。于是就有:
- 如果 x x x 与 l l l 相隔很近 ⇒ f ≈ e x p ( 0 ) ≈ 1 \Rightarrow f \approx exp(0)\approx1 ⇒f≈exp(0)≈1
- 如果 x x x 与 l l l 相隔很远 ⇒ f = e x p ( − ∞ ) ≈ 0 \Rightarrow f =exp(-\infty)\approx0 ⇒f=exp(−∞)≈0
高斯核函数的SVM流程可表示为:
- 给定数据集 ( x ( 1 ) , y ( 1 ) ) , ⋯ , ( x ( m ) , y ( m ) ) (x^{(1)},y^{(1)}),\cdots,(x^{(m)},y^{(m)}) (x(1),y(1)),⋯,(x(m),y(m));
- 设 l ( 1 ) = x ( 1 ) , ⋯ , l ( m ) = x ( m ) l^{(1)}=x^{(1)},\cdots,l^{(m)}=x^{(m)} l(1)=x(1),⋯,l(m)=x(m);
- 对于测试样本 x x x,计算 f = [ f 1 ⋮ f m ] f=\begin{bmatrix} f_1 \\ \vdots \\ f_m \end{bmatrix} f=⎣⎢⎡f1⋮fm⎦⎥⎤;
- θ T f ≥ 0 ⇒ y = 1 , θ T f ≤ 0 ⇒ y = 0 \theta^Tf\geq 0 \Rightarrow y=1 , \theta^Tf\leq 0 \Rightarrow y=0 θTf≥0⇒y=1,θTf≤0⇒y=0
此时代价函数修改为:
J
(
θ
)
=
C
∑
i
=
1
m
[
y
(
i
)
c
o
s
t
1
(
θ
T
f
(
i
)
)
+
(
1
−
y
(
i
)
)
c
o
s
t
0
(
θ
T
f
(
i
)
)
]
+
1
2
∑
j
=
1
n
θ
j
2
J(\theta) =C\sum_{i=1}^m[y^{(i)}cost_1(\theta^Tf^{(i)})+(1-y^{(i)})cost_0(\theta^Tf^{(i)})]+\frac{1}{2}\sum_{j=1}^n\theta_j^2
J(θ)=Ci=1∑m[y(i)cost1(θTf(i))+(1−y(i))cost0(θTf(i))]+21j=1∑nθj2
注:
σ
\sigma
σ 较大,容易造成欠拟合,
σ
\sigma
σ 较小,容易造成过拟合。
10.5 SVM实现多分类
SVM与逻辑回归一样,本质上属于二分类的算法,但是可以通过构建多个二分类器的思想,实现多分类。
- 针对类别 A A A ,设 A A A为正类,非 A A A为负类,构建二分类器 h θ ( 1 ) ( x ) h_\theta^{(1)}(x) hθ(1)(x);
- 针对类别 B B B ,设 B B B为正类,非 B B B为负类,构建二分类器 h θ ( 2 ) ( x ) h_\theta^{(2)}(x) hθ(2)(x);
- 针对类别 C C C ,设 C C C为正类,非 C C C为负类,构建二分类器 h θ ( 3 ) ( x ) h_\theta^{(3)}(x) hθ(3)(x);
- ⋯ \cdots ⋯
由于 h θ ( i ) ( x ) = P ( y = i ∣ x ; θ ) h_\theta^{(i)}(x)=P(y=i|x;\theta) hθ(i)(x)=P(y=i∣x;θ),即求 m a x ( h θ ( i ) ( x ) ) max(h_\theta^{(i)}(x)) max(hθ(i)(x))时的 i i i。
10.6 逻辑回归和SVM的选择
设定 n n n 为特征, m m m 为样本数量。
- 如果 n n n 相对于 m m m 很大 ⇒ \Rightarrow ⇒ 逻辑回归、线性SVM
- 如果 n n n 很小, m m m 适中 ⇒ \Rightarrow ⇒ 高斯核函数的SVM
- 如果 m m m 相对于 n n n 很大 ⇒ \Rightarrow ⇒ 增加多个特征,然后使用逻辑回归或线性SVM