在线性分类任务中,我们通常使用线性分类函数来分类:
d
(
x
)
=
w
1
x
1
+
w
2
x
2
+
.
.
.
+
w
n
x
n
+
w
0
d(\mathbf{x})=w_{1}x_{1}+w_{2}x_{2}+...+w_{n}x_{n}+w_{0}
d(x)=w1x1+w2x2+...+wnxn+w0
增广权向量可表示为:
w
=
(
w
1
,
w
2
,
.
.
.
,
w
n
,
w
0
)
T
\mathbf{w}=(w_{1},w_{2},...,w_{n},w_{0})^{T}
w=(w1,w2,...,wn,w0)T
增广模式向量可表示为:
x
=
(
x
1
,
x
2
,
.
.
.
,
x
n
,
1
)
T
\mathbf{x}=(x_{1},x_{2},...,x_{n},1)^{T}
x=(x1,x2,...,xn,1)T
感知器算法就是对线性函数进行优化,找出权向量。
二分类的感知算法:
已知训练集可分为
ω
1
\omega_{1}
ω1和
ω
2
\omega_{2}
ω2,我们需要找出具有如下分类规则的权向量:
{
w
T
x
i
>
0
,
x
i
∈
ω
1
w
T
x
i
⩽
0
,
x
i
∈
ω
2
\left\{\begin{matrix} \mathbf w^{T} \mathbf x_{i} >0, \mathbf x_{i} \in \omega_{1} \\ \mathbf w^{T} \mathbf x_{i} \leqslant 0, \mathbf x_{i} \in \omega_{2} \end{matrix}\right.
{wTxi>0,xi∈ω1wTxi⩽0,xi∈ω2
感知算法就是每次遍历所有的样本,在分类错误的样本下修正权向量
w
\mathbf w
w,直至所有样本都能被正确的分类。遍历时,对于某个样本,有如下3种操作:
(1)若
x
∈
ω
1
,
w
T
x
i
⩽
0
\mathbf{x \in}\omega_{1},\mathbf w^{T} \mathbf x_{i} \leqslant 0
x∈ω1,wTxi⩽0,此时权向量对
x
i
\mathbf x_{i}
xi做了错误分类,需要对权向量修正:
w
(
t
+
1
)
=
w
(
t
)
+
η
x
i
\mathbf{w}(t+1)=\mathbf w (t)+\eta\mathbf x_{i}
w(t+1)=w(t)+ηxi
其中
η
\eta
η是一个常数,校正增量,相当于学习率。表现在向量空间中为
w
(
t
)
−
(
−
η
x
i
)
=
w
(
t
+
1
)
\mathbf w (t)-(-\eta\mathbf x_{i})=\mathbf{w}(t+1)
w(t)−(−ηxi)=w(t+1),此时分类边界向朝
x
\mathbf x
x方向修正,即由红线转到绿线。
(2)若
x
∈
ω
2
,
w
T
x
i
>
0
\mathbf{x \in}\omega_{2},\mathbf w^{T} \mathbf x_{i} > 0
x∈ω2,wTxi>0,此时权向量对
x
i
\mathbf x_{i}
xi做了错误分类,需要对权向量修正:
w
(
t
+
1
)
=
w
(
t
)
−
η
x
i
\mathbf{w}(t+1)=\mathbf w (t)-\eta\mathbf x_{i}
w(t+1)=w(t)−ηxi
表现在向量空间中为
w
(
t
)
−
η
x
i
=
w
(
t
+
1
)
\mathbf w (t)-\eta\mathbf x_{i}=\mathbf{w}(t+1)
w(t)−ηxi=w(t+1),此时分类边界向朝
x
\mathbf x
x方向修正,即由红线转到绿线。
(3)若未出现上述情况,表明分类正确,
w
(
t
+
1
)
=
w
(
t
)
\mathbf w(t+1)=\mathbf w(t)
w(t+1)=w(t)
结论: 只要模式类别是可分的,那么该算法必定会收敛,即在有限的步数内得到权向量。
算法可总结为:
w
(
t
+
1
)
=
{
w
(
t
)
+
η
x
i
,
i
f
x
∈
ω
1
,
w
T
(
t
)
x
i
⩽
0
w
(
t
)
−
η
x
i
,
i
f
x
∈
ω
2
,
w
T
(
t
)
x
i
>
0
w
(
t
)
,
i
f
o
t
h
e
r
\mathbf w(t+1)=\left\{\begin{matrix} \mathbf w(t)+\eta \mathbf x_{i},if\ \mathbf{x \in}\omega_{1},\mathbf w^{T}(t) \mathbf x_{i} \leqslant 0\\ \mathbf w (t)-\eta\mathbf x_{i},if\ \mathbf{x \in}\omega_{2},\mathbf w^{T}(t) \mathbf x_{i} > 0\\ \mathbf w(t), if \ other \end{matrix}\right.
w(t+1)=⎩⎨⎧w(t)+ηxi,if x∈ω1,wT(t)xi⩽0w(t)−ηxi,if x∈ω2,wT(t)xi>0w(t),if other
两种方法对公式进行化简:
(1)如果对
x
i
∈
ω
2
\mathbf x_{i}\in \omega_{2}
xi∈ω2的模式样本都乘
−
1
-1
−1,此时
w
T
(
−
x
i
)
<
0
\mathbf w^{T} (-\mathbf x_{i}) <0
wT(−xi)<0时需要调整权向量向
w
T
(
−
x
i
)
≥
0
\mathbf w^{T} (-\mathbf x_{i}) \geq 0
wT(−xi)≥0靠近。也可以写成
w
T
(
−
x
i
)
≤
0
\mathbf w^{T} (-\mathbf x_{i}) \leq 0
wT(−xi)≤0时需要调整权向量向
w
T
(
−
x
i
)
>
0
\mathbf w^{T} (-\mathbf x_{i}) >0
wT(−xi)>0靠近。区别是前者要求
w
T
x
≤
0
,
x
i
\mathbf {w^{T}x}\leq0,\mathbf x_{i}
wTx≤0,xi才属于
ω
2
\omega_{2}
ω2;后者要求
w
T
x
<
0
,
x
i
\mathbf {w^{T}x}<0,\mathbf x_{i}
wTx<0,xi才属于
ω
2
\omega_{2}
ω2,这两种都可以。我们采用后者,所以此时的更新规则的形式和(1)相同。
w
(
t
+
1
)
=
w
(
t
)
+
η
(
−
x
i
)
\mathbf{w}(t+1)=\mathbf w (t)+\eta(-\mathbf x_{i})
w(t+1)=w(t)+η(−xi)
求解之前,对所有
∈
ω
2
\in \omega_{2}
∈ω2的样本进行
−
1
-1
−1操作更新样本,上述的感知器算法可写为:
w
(
t
+
1
)
=
{
w
(
t
)
,
i
f
w
T
(
t
)
x
i
>
0
w
(
t
)
+
η
x
i
,
i
f
w
T
(
t
)
x
i
≤
0
\mathbf w(t+1)=\left\{\begin{matrix} \mathbf w(t),\ if\ \mathbf w^{T}(t)\mathbf x_{i}>0\\ \mathbf w (t)+\eta\mathbf x_{i},\ if \ \mathbf w^{T}(t)\mathbf x_{i}\leq0 \end{matrix}\right.
w(t+1)={w(t), if wT(t)xi>0w(t)+ηxi, if wT(t)xi≤0
(2)假设
ω
1
\omega_{1}
ω1对应的标签为为+1,假设
ω
2
\omega_{2}
ω2对应的标签为为+1。
w
(
t
+
1
)
=
{
w
(
t
)
,
i
f
y
i
w
T
(
t
)
x
i
>
0
w
(
t
)
+
η
y
i
x
i
,
i
f
y
i
w
T
(
t
)
x
i
≤
0
\mathbf w(t+1)=\left\{\begin{matrix} \mathbf w(t),\ if\ y_{i}\mathbf w^{T}(t)\mathbf x_{i}>0\\ \mathbf w (t)+\eta y_{i}\mathbf x_{i},\ if \ y_{i}\mathbf w^{T}(t)\mathbf x_{i}\leq0 \end{matrix}\right.
w(t+1)={w(t), if yiwT(t)xi>0w(t)+ηyixi, if yiwT(t)xi≤0
上面有向量的形式证明了上式的合理性,下面用数学公式证明:
y
i
w
T
(
t
+
1
)
x
i
>
y
i
w
T
(
t
)
x
i
y
i
w
T
(
t
+
1
)
x
i
=
y
i
[
w
T
(
t
)
+
η
y
i
x
i
T
]
x
i
=
y
i
w
T
(
t
)
+
η
y
i
2
x
i
T
x
i
>
y
i
w
T
(
t
)
x
i
y_{i}\mathbf w^{T}(t+1)\mathbf x_{i}>y_{i}\mathbf w^{T}(t)\mathbf x_{i}\\ y_{i}\mathbf w^{T}(t+1)\mathbf x_{i}=y_{i}[\mathbf w ^{T}(t)+\eta y_{i}\bm x_{i}^{T}]\bm{x}_{i}=y_{i}\mathbf w ^{T}(t)+\eta y_{i}^{2}\bm x_{i}^{T}\bm{x}_{i}>y_{i}\mathbf w^{T}(t)\mathbf x_{i}
yiwT(t+1)xi>yiwT(t)xiyiwT(t+1)xi=yi[wT(t)+ηyixiT]xi=yiwT(t)+ηyi2xiTxi>yiwT(t)xi
例子:
数据点
(
0
,
0
)
,
(
0
,
1
)
∈
ω
1
(0,0),(0,1)\in \omega_{1}
(0,0),(0,1)∈ω1,
(
1
,
0
)
,
(
1
,
1
)
∈
ω
2
(1,0),(1,1)\in \omega_{2}
(1,0),(1,1)∈ω2。求解一条线性分类函数的权向量过程。
可得到分类函数为
d
(
x
)
=
−
2
x
1
+
1
d(\mathbf x)=-2x_{1}+1
d(x)=−2x1+1
import numpy as np
x1 = [np.array([0, 0]), np.array([0, 1])]
x2 = [np.array([1, 0]), np.array([1, 1])]
for i in range(len(x1)):
x1[i] = np.hstack((x1[i], [1]))
for i in range(len(x2)):
x2[i] = np.hstack((x2[i], [1]))
x2[i] = -x2[i]
x = x1 + x2
w = [0, 0, 0]
count = 1
step = 1
while True:
print('The step'+str(step))
flag = True
for i in range(len(x)):
if np.dot(w, x[i]) <= 0:
flag = False
tmp_w = w
w = w + x[i]
print('w'+str(count)+'^T*x'+str(i)+'=',str(tmp_w), str(x[i])+'^T<=0,',
'w'+str(count+1)+'=w'+str(count)+'+x'+str(i)+'=', str(w)+'^T')
else:
print('w' + str(count) + '^T*x' + str(i) + '=', w, str(x[i]) + '^T>0,',
'w' + str(count+1) + '=w' + str(count) + '=', str(w) + '^T')
count += 1
step = step + 1
if flag is True:
break
print('Perception weight vector:', w)
多分类的感知算法
对
M
M
M类模式分类问题,往往具有
M
M
M个判别函数
d
k
,
k
=
1
,
2
,
.
.
.
,
M
d_{k},k=1,2,...,M
dk,k=1,2,...,M。若
x
∈
ω
k
x\in \omega_{k}
x∈ωk,则有
d
k
(
x
)
>
d
j
(
x
)
,
∀
j
≠
k
d_{k}(\mathbf x)>d_{j}(\mathbf x), \forall j\neq k
dk(x)>dj(x),∀j̸=k。
在第
t
t
t次迭代中,对于某个属于
ω
k
\omega_{k}
ωk的样本,首先计算M个判别函数的值:
d
j
(
x
)
=
w
j
T
(
t
)
x
,
j
=
1
,
2
,
.
.
.
,
M
d_{j}(\mathbf x)=\mathbf w_{j}^{T}(t)\mathbf x,j=1,2,...,M
dj(x)=wjT(t)x,j=1,2,...,M
(1)若
d
k
(
x
)
>
d
j
(
x
)
,
∀
j
≠
k
d_{k}(\mathbf x)>d_{j}(\mathbf x), \forall j\neq k
dk(x)>dj(x),∀j̸=k成立,则权向量不变。
w
j
(
t
+
1
)
=
w
j
(
t
)
,
j
=
1
,
2
,
.
.
.
,
M
\mathbf w_{j}(t+1)=\mathbf w_{j}(t),j=1,2,...,M
wj(t+1)=wj(t),j=1,2,...,M
(2)若
∃
j
≠
k
\exists j\neq k
∃j̸=k,使得
d
k
(
x
)
≤
d
j
(
x
)
d_{k}(\mathbf x)\leq d_{j}(\mathbf x)
dk(x)≤dj(x),则有如下更新规则。
{
w
k
(
t
+
1
)
=
w
k
(
t
)
+
η
x
i
,
此
时
可
以
使
得
w
k
(
t
)
向
w
k
(
t
)
x
i
增
大
的
方
向
调
整
w
j
(
t
+
1
)
=
w
j
(
t
)
−
η
x
i
,
此
时
可
以
使
得
w
j
(
t
)
向
w
j
(
t
)
x
i
减
小
的
方
向
调
整
\left\{\begin{matrix} \mathbf w_{k}(t+1)=\mathbf w_{k}(t)+\eta\mathbf x_{i},此时可以使得\mathbf w_{k}(t)向\mathbf w_{k}(t)\mathbf x_{i}增大的方向调整\\ \mathbf w_{j}(t+1)=\mathbf w_{j}(t)-\eta\mathbf x_{i},此时可以使得\mathbf w_{j}(t)向\mathbf w_{j}(t)\mathbf x_{i}减小的方向调整 \end{matrix}\right.
{wk(t+1)=wk(t)+ηxi,此时可以使得wk(t)向wk(t)xi增大的方向调整wj(t+1)=wj(t)−ηxi,此时可以使得wj(t)向wj(t)xi减小的方向调整