支持向量机原理 线性支持向量机
Posted 远里歌声_why
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了支持向量机原理 线性支持向量机相关的知识,希望对你有一定的参考价值。
支持向量机(Support Vecor Machine,以下简称SVM)虽然诞生只有短短的二十多年,但是自一诞生便由于它良好的分类性能席卷了机器学习领域,并牢牢压制了神经网络领域好多年。如果不考虑集成学习的算法,不考虑特定的训练数据集,在分类算法中的表现SVM说是排第一估计是没有什么异议的。
SVM是一个二元分类算法,线性分类和非线性分类都支持。经过演进,现在也可以支持多元分类,同时经过扩展,也能应用于回归问题。本系列文章就对SVM的原理做一个总结。本篇的重点是SVM用于线性分类时模型和损失函数优化的一个总结。
1. 回顾感知机模型
在感知机原理小结中,我们讲到了感知机的分类原理,感知机的模型就是尝试找到一条直线,能够把二元数据隔离开。放到三维空间或者更高维的空间,感知机的模型就是尝试找到一个超平面,能够把所有的二元类别隔离开。对于这个分离的超平面,我们定义为wTx+b=0,如下图。在超平面
wTx+b=0上方的我们定义为
y=1,在超平面
wTx+b=0下方的我们定义为
y=−1。可以看出满足这个条件的超平面并不止一个。那么我们可能会尝试思考,这么多的可以分类的超平面,哪个是最好的呢?或者说哪个是泛化能力最强的呢?
接着我们看感知机模型的损失函数优化,它的思想是让所有误分类的点(定义为M)到超平面的距离和最小,即最小化下式:
![](https://image.cha138.com/20210607/953ff855c28d45879a092f14bc13a050.jpg)
![](https://image.cha138.com/20210607/cf934f3127c74039a02a595151b4c0c7.jpg)
![](https://image.cha138.com/20210607/f029af6ed6494e9a9851b9ea444363c0.jpg)
![](https://image.cha138.com/20210607/1c683a08c3934a5393b853f10a95528e.jpg)
![](https://image.cha138.com/20210607/a0aba4158ecd42b88091f3a7cb8a309c.jpg)
![](https://image.cha138.com/20210607/0a0dce44fac949e29a2f38a4f48697a5.jpg)
![](https://image.cha138.com/20210607/1ffdca70551147348cf9b2852c039025.jpg)
![](https://image.cha138.com/20210607/61376e918de5432eac3f91fa6ff26743.jpg)
![](https://image.cha138.com/20210607/dc2e9d632be947bfbc8ab485a4e86b89.jpg)
![](https://image.cha138.com/20210607/e99061ce31d346968c76bc7f44d3fb42.jpg)
![](https://image.cha138.com/20210607/621b581be1d34c57960d63c3d37abf71.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/199942080e4b43f2b668da1dfafb8c06.jpg)
![](https://image.cha138.com/20210607/3cecdf1275a7465ebb5ea7c419491d44.jpg)
![](https://image.cha138.com/20210607/61376e918de5432eac3f91fa6ff26743.jpg)
![](https://image.cha138.com/20210607/dc2e9d632be947bfbc8ab485a4e86b89.jpg)
![](https://image.cha138.com/20210607/e99061ce31d346968c76bc7f44d3fb42.jpg)
![](https://image.cha138.com/20210607/0c32057df0784f89b357910db65f717f.jpg)
![](https://image.cha138.com/20210607/9dc6142e8dee41e6bb84be9c907f6ebd.jpg)
![](https://image.cha138.com/20210607/1fbf2ff3081a41b2827b0f90857531d0.jpg)
![](https://image.cha138.com/20210607/91d4cdf62f09442f882681f90c1622b2.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/1123e50f97384504aca28ff2da878a59.jpg)
当和
w和b成比例的增加,比如,当分子的
和
w和b扩大N倍时,分母的L2范数也会扩大N倍。也就是说,分子和分母有固定的倍数关系。那么我们可以固定分子或者分母为1,然后求另一个即分子自己或者分母的倒数的最小化作为损失函数,这样可以简化我们的损失函数。在感知机模型中,我们采用的是保留分子,固定分母
||w||2=1,即最终感知机模型的损失函数为:
![](https://image.cha138.com/20210607/953ff855c28d45879a092f14bc13a050.jpg)
![](https://image.cha138.com/20210607/cf934f3127c74039a02a595151b4c0c7.jpg)
![](https://image.cha138.com/20210607/f029af6ed6494e9a9851b9ea444363c0.jpg)
![](https://image.cha138.com/20210607/1c683a08c3934a5393b853f10a95528e.jpg)
![](https://image.cha138.com/20210607/a0aba4158ecd42b88091f3a7cb8a309c.jpg)
![](https://image.cha138.com/20210607/0a0dce44fac949e29a2f38a4f48697a5.jpg)
![](https://image.cha138.com/20210607/1ffdca70551147348cf9b2852c039025.jpg)
![](https://image.cha138.com/20210607/61376e918de5432eac3f91fa6ff26743.jpg)
![](https://image.cha138.com/20210607/dc2e9d632be947bfbc8ab485a4e86b89.jpg)
![](https://image.cha138.com/20210607/e99061ce31d346968c76bc7f44d3fb42.jpg)
![](https://image.cha138.com/20210607/621b581be1d34c57960d63c3d37abf71.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/199942080e4b43f2b668da1dfafb8c06.jpg)
![](https://image.cha138.com/20210607/3cecdf1275a7465ebb5ea7c419491d44.jpg)
![](https://image.cha138.com/20210607/61376e918de5432eac3f91fa6ff26743.jpg)
![](https://image.cha138.com/20210607/dc2e9d632be947bfbc8ab485a4e86b89.jpg)
![](https://image.cha138.com/20210607/e99061ce31d346968c76bc7f44d3fb42.jpg)
![](https://image.cha138.com/20210607/0c32057df0784f89b357910db65f717f.jpg)
![](https://image.cha138.com/20210607/9dc6142e8dee41e6bb84be9c907f6ebd.jpg)
![](https://image.cha138.com/20210607/1fbf2ff3081a41b2827b0f90857531d0.jpg)
如果我们不是固定分母,改为固定分子,作为分类模型有没有改进呢?
这些问题在我们引入SVM后会详细解释。
2. 函数间隔与几何间隔
在正式介绍SVM的模型和损失函数之前,我们还需要先了解下函数间隔和几何间隔的知识。
在分离超平面固定为wTx+b=0的时候,
|wTx+b|表示点x到超平面的距离。通过观察
wTx+b和y是否同号,我们判断分类是否正确,这些知识我们在感知机模型里都有讲到。这里我们引入函数间隔的概念,定义函数间隔
γ′为:
![](https://image.cha138.com/20210607/6fded41fe6a544b8b5538b025d2bdd3c.jpg)
![](https://image.cha138.com/20210607/b7c7a83beebe4889ac67d2348195ba9b.jpg)
![](https://image.cha138.com/20210607/6168555b17da4f02824e41dfb099f5b5.jpg)
![](https://image.cha138.com/20210607/1ffdca70551147348cf9b2852c039025.jpg)
![](https://image.cha138.com/20210607/621b581be1d34c57960d63c3d37abf71.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/199942080e4b43f2b668da1dfafb8c06.jpg)
![](https://image.cha138.com/20210607/3cecdf1275a7465ebb5ea7c419491d44.jpg)
![](https://image.cha138.com/20210607/0c32057df0784f89b357910db65f717f.jpg)
![](https://image.cha138.com/20210607/9dc6142e8dee41e6bb84be9c907f6ebd.jpg)
![](https://image.cha138.com/20210607/1fbf2ff3081a41b2827b0f90857531d0.jpg)
可以看到,它就是感知机模型里面的误分类点到超平面距离的分子。对于训练集中m个样本点对应的m个函数间隔的最小值,就是整个训练集的函数间隔。
函数间隔并不能正常反应点到超平面的距离,在感知机模型里我们也提到,当分子成比例的增长时,分母也是成倍增长。为了统一度量,我们需要对法向量w加上约束条件,这样我们就得到了几何间隔
γ,定义为:
![](https://image.cha138.com/20210607/6fded41fe6a544b8b5538b025d2bdd3c.jpg)
![](https://image.cha138.com/20210607/6168555b17da4f02824e41dfb099f5b5.jpg)
![](https://image.cha138.com/20210607/1ffdca70551147348cf9b2852c039025.jpg)
![](https://image.cha138.com/20210607/621b581be1d34c57960d63c3d37abf71.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/199942080e4b43f2b668da1dfafb8c06.jpg)
![](https://image.cha138.com/20210607/3cecdf1275a7465ebb5ea7c419491d44.jpg)
![](https://image.cha138.com/20210607/0c32057df0784f89b357910db65f717f.jpg)
![](https://image.cha138.com/20210607/9dc6142e8dee41e6bb84be9c907f6ebd.jpg)
![](https://image.cha138.com/20210607/1fbf2ff3081a41b2827b0f90857531d0.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/1123e50f97384504aca28ff2da878a59.jpg)
![](https://image.cha138.com/20210607/6168555b17da4f02824e41dfb099f5b5.jpg)
![](https://image.cha138.com/20210607/6fded41fe6a544b8b5538b025d2bdd3c.jpg)
![](https://image.cha138.com/20210607/b7c7a83beebe4889ac67d2348195ba9b.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/1123e50f97384504aca28ff2da878a59.jpg)
几何间隔才是点到超平面的真正距离,感知机模型里用到的距离就是几何距离。
3. 支持向量
在感知机模型中,我们可以找到多个可以分类的超平面将数据分开,并且优化时希望所有的点都离超平面远。但是实际上离超平面很远的点已经被正确分类,我们让它离超平面更远并没有意义。反而我们最关心是那些离超平面很近的点,这些点很容易被误分类。如果我们可以让离超平面比较近的点尽可能的远离超平面,那么我们的分类效果会好有一些。SVM的思想起源正起于此。
如下图所示,分离超平面为wTx+b=0,如果所有的样本不光可以被超平面分开,还和超平面保持一定的函数距离(下图函数距离为1),那么这样的分类超平面是比感知机的分类超平面优的。可以证明,这样的超平面只有一个。和超平面平行的保持一定的函数距离的这两个超平面对应的向量,我们定义为支持向量,如下图虚线所示。
支持向量到超平面的距离为1/||w||2,两个支持向量之间的距离为
2/||w||2。
4. SVM模型目标函数与优化
SVM的模型是让所有点到超平面的距离大于一定的距离,也就是所有的分类点要在各自类别的支持向量两边。用数学式子表示为:
![](https://image.cha138.com/20210607/6298719573e64dd9ac67ce50ab777e10.jpg)
![](https://image.cha138.com/20210607/329fa1e0f9ea4646b422a9668376dbd2.jpg)
![](https://image.cha138.com/20210607/3cecdf1275a7465ebb5ea7c419491d44.jpg)
![](https://image.cha138.com/20210607/6fded41fe6a544b8b5538b025d2bdd3c.jpg)
![](https://image.cha138.com/20210607/6168555b17da4f02824e41dfb099f5b5.jpg)
![](https://image.cha138.com/20210607/1ffdca70551147348cf9b2852c039025.jpg)
![](https://image.cha138.com/20210607/621b581be1d34c57960d63c3d37abf71.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/199942080e4b43f2b668da1dfafb8c06.jpg)
![](https://image.cha138.com/20210607/3cecdf1275a7465ebb5ea7c419491d44.jpg)
![](https://image.cha138.com/20210607/0c32057df0784f89b357910db65f717f.jpg)
![](https://image.cha138.com/20210607/9dc6142e8dee41e6bb84be9c907f6ebd.jpg)
![](https://image.cha138.com/20210607/1fbf2ff3081a41b2827b0f90857531d0.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/ea2c9b801d944873b58f7c72695172f6.jpg)
![](https://image.cha138.com/20210607/1123e50f97384504aca28ff2da878a59.jpg)
![](https://image.cha138.com/20210607/9c3335e9aa274e37b1bb37d7cf23c6de.jpg)
![](https://image.cha138.com/20210607/f8a4a4a9f80643a3b20ae4a0d5ce349f.jpg)
![](https://image.cha138.com/20210607/5e39dcde8e4c45369d173acd3b3dc3b0.jpg)
![](https://image.cha138.com/20210607/1ffdca70551147348cf9b2852c039025.jpg)
![](https://image.cha138.com/20210607/dc2e9d632be947bfbc8ab485a4e86b89.jpg)
![](https://image.cha138.com/20210607/621b581be1d34c57960d63c3d37abf71.jpg)
![](https://image.cha138.com/20210607/2dd6267a14cb445d916e6383b8ec3313.jpg)
![](https://image.cha138.com/20210607/199942080e4b43f2b668da1dfafb8c06.jpg)
![](https://image.cha138.com/20210607/3cecdf1275a7465ebb5ea7c419491d44.jpg)
![](https://image.cha138.com/20210607/dc2e9d632be947bfbc8ab485a4e86b89.jpg)
![](https://image.cha138.com/20210607/0c32057df0784f89b357910db65f717f.jpg)
![](https://image.cha138.com/20210607/9dc6142e8dee41e6bb84be9c907f6ebd.jpg)
![](https://image.cha138.com/20210607/1fbf2ff3081a41b2827b0f90857531d0.jpg)
![](https://image.cha138.com/20210607/6168555b17da4f02824e41dfb099f5b5.jpg)
![](https://image.cha138.com/20210607/6fded41fe6a544b8b5538b025d2bdd3c.jpg)
![](https://image.cha138.com/20210607/b7c7a83beebe4889ac67d2348195ba9b.jpg)
![](https://image.cha138.com/20210607/61376e918de5432eac3f91fa6ff26743.jpg)
![](https://image.cha138.com/20210607/dc2e9d632be947bfbc8ab485a4e86b89.jpg)
![](https://image.cha138.com/20210607/e99061ce31d346968c76bc7f44d3fb42.jpg)
![](https://image.cha138.com/20210607/868174122a264e3d8c30689f52561220.jpg)
![](https://image.cha138.com/20210607/6fded41fe6a544b8b5538b025d2bdd3c.jpg)
![](https://image.cha138.com/20210607/b7c7a83beebe4889ac67d2348195ba9b.jpg)
![](https://image.cha138.com/20210607/621b581be1d34c57960d63c3d37abf71.jpg)
![](https://image.cha138.com/20210607/d17a818910794a29b8b4463610bc0265.jpg)
![](https://image.cha138.com/20210607/6168555b17da4f02824e41dfb099f5b5.jpg)
![](https://image.cha138.com/20210607/27e0b3773d6f47fdb64a20846c81f6d9.jpg)
以上是关于支持向量机原理 线性支持向量机的主要内容,如果未能解决你的问题,请参考以下文章