感知算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/winycg/article/details/83064112

在线性分类任务中,我们通常使用线性分类函数来分类: 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}
增广权向量可表示为: w = ( w 1 , w 2 , . . . , w n , w 0 ) T \mathbf{w}=(w_{1},w_{2},...,w_{n},w_{0})^{T}
增广模式向量可表示为: x = ( x 1 , x 2 , . . . , x n , 1 ) T \mathbf{x}=(x_{1},x_{2},...,x_{n},1)^{T}
感知器算法就是对线性函数进行优化,找出权向量。

二分类的感知算法:

已知训练集可分为 ω 1 \omega_{1} ω 2 \omega_{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.
感知算法就是每次遍历所有的样本,在分类错误的样本下修正权向量 w \mathbf w ,直至所有样本都能被正确的分类。遍历时,对于某个样本,有如下3种操作:
(1)若 x ω 1 , w T x i 0 \mathbf{x \in}\omega_{1},\mathbf w^{T} \mathbf x_{i} \leqslant 0 ,此时权向量对 x i \mathbf x_{i} 做了错误分类,需要对权向量修正: w ( t + 1 ) = w ( t ) + η x i \mathbf{w}(t+1)=\mathbf w (t)+\eta\mathbf x_{i}
其中 η \eta 是一个常数,校正增量,相当于学习率。表现在向量空间中为 w ( t ) ( η x i ) = w ( t + 1 ) \mathbf w (t)-(-\eta\mathbf x_{i})=\mathbf{w}(t+1) ,此时分类边界向朝 x \mathbf x 方向修正,即由红线转到绿线。
在这里插入图片描述
(2)若 x ω 2 , w T x i > 0 \mathbf{x \in}\omega_{2},\mathbf w^{T} \mathbf x_{i} > 0 ,此时权向量对 x i \mathbf x_{i} 做了错误分类,需要对权向量修正: w ( t + 1 ) = w ( t ) η x i \mathbf{w}(t+1)=\mathbf w (t)-\eta\mathbf x_{i}
表现在向量空间中为 w ( t ) η x i = w ( t + 1 ) \mathbf w (t)-\eta\mathbf x_{i}=\mathbf{w}(t+1) ,此时分类边界向朝 x \mathbf x 方向修正,即由红线转到绿线。
在这里插入图片描述
(3)若未出现上述情况,表明分类正确, w ( t + 1 ) = w ( t ) \mathbf w(t+1)=\mathbf 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.
如果对 x i ω 2 \mathbf x_{i}\in \omega_{2} 的模式样本都乘 1 -1 ,此时 w T ( x i ) < 0 \mathbf w^{T} (-\mathbf x_{i}) <0 时需要调整权向量向 w T ( x i ) 0 \mathbf w^{T} (-\mathbf x_{i}) \geq 0 靠近。也可以写成 w T ( x i ) 0 \mathbf w^{T} (-\mathbf x_{i}) \leq 0 时需要调整权向量向 w T ( x i ) > 0 \mathbf w^{T} (-\mathbf x_{i}) >0 靠近。区别是前者要求 w T x 0 , x i \mathbf {w^{T}x}\leq0,\mathbf x_{i} 才属于 ω 2 \omega_{2} ;后者要求 w T x < 0 , x i \mathbf {w^{T}x}<0,\mathbf x_{i} 才属于 ω 2 \omega_{2} ,这两种都可以。我们采用后者,所以此时的更新规则的形式和(1)相同。
w ( t + 1 ) = w ( t ) + η ( x i ) \mathbf{w}(t+1)=\mathbf w (t)+\eta(-\mathbf x_{i})
求解之前,对所有 ω 2 \in \omega_{2} 的样本进行 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.
例子:
数据点 ( 0 , 0 ) , ( 0 , 1 ) ω 1 (0,0),(0,1)\in \omega_{1} , ( 1 , 0 ) , ( 1 , 1 ) ω 2 (1,0),(1,1)\in \omega_{2} 。求解一条线性分类函数的权向量过程。
可得到分类函数为 d ( x ) = 2 x 1 + 1 d(\mathbf x)=-2x_{1}+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 个判别函数 d k , k = 1 , 2 , . . . , M d_{k},k=1,2,...,M 。若 x ω k x\in \omega_{k} ,则有 d k ( x ) &gt; d j ( x ) , j k d_{k}(\mathbf x)&gt;d_{j}(\mathbf x), \forall j\neq k
在第 t t 次迭代中,对于某个属于 ω k \omega_{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
(1)若 d k ( x ) &gt; d j ( x ) , j k d_{k}(\mathbf x)&gt;d_{j}(\mathbf x), \forall j\neq 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
(2)若 j k \exists j\neq k ,使得 d k ( x ) d j ( x ) d_{k}(\mathbf x)\leq d_{j}(\mathbf 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.

猜你喜欢

转载自blog.csdn.net/winycg/article/details/83064112
今日推荐