【Day 29】支援向量机(Support Vector Machine, SVM)(下)

昨天讲完Hinge Loss,今天要继续介绍SVM的第二个特色:Kernel Method。

Dual Representation

我们实际找出来可以最小化Loss function的weight https://chart.googleapis.com/chart?cht=tx&chl=w%5E*,其实是我们data points的linear combination。昨天提到我们可以用Gradient Descent来minimize SVM,更新参数的式子如下图所示,假设我们初始化的时候 https://chart.googleapis.com/chart?cht=tx&chl=w 是一个zero vector,而你每次在更新参数的时候,你都是加上data points的linear combination。

如果今天用的是Hinge Loss,他作用在 https://chart.googleapis.com/chart?cht=tx&chl=max%20%3D%200 的region的情况,它的 https://chart.googleapis.com/chart?cht=tx&chl=c%5En(w) 就会是0,所以当你在用Hinge Loss的时候,https://chart.googleapis.com/chart?cht=tx&chl=c%5En(w) 往往都会是0,也就是说不是所有的 https://chart.googleapis.com/chart?cht=tx&chl=x%5En 都会被拿来加到 https://chart.googleapis.com/chart?cht=tx&chl=w 里面去,所以最後解出来的 https://chart.googleapis.com/chart?cht=tx&chl=w%5E* 的linear combination的weight可能会是sparse,也就是可能有很多的data point对应的 https://chart.googleapis.com/chart?cht=tx&chl=%5Calpha%5E* 值等於0,对最後的结果不会有影响,而那些 https://chart.googleapis.com/chart?cht=tx&chl=%5Calpha%5E* 的值不等於0的 https://chart.googleapis.com/chart?cht=tx&chl=x%5En,就是support vector

SVM相较於其他方法可能是比较robust的,如果Loss function选的是Cross Entropy,就没有sparse的特性。

我们可以将 https://chart.googleapis.com/chart?cht=tx&chl=w 原本的式子改写成 https://chart.googleapis.com/chart?cht=tx&chl=w%20%3D%20X%20%5Cbf%20a,接着在步骤一我们就可以改一下function的样子,如下图所示,而 https://chart.googleapis.com/chart?cht=tx&chl=x%5ENhttps://chart.googleapis.com/chart?cht=tx&chl=x 做内积这件事,我们可以把它写成一个function https://chart.googleapis.com/chart?cht=tx&chl=K(x%5EN%2C%20x),这个function我们就称之为Kernel function

再来步骤二、三我们要找到一组最好的 https://chart.googleapis.com/chart?cht=tx&chl=%5Calpha%5EN 可以让我们的Total Loss最小,从下面的式子可以发现,我们不再需要知道 https://chart.googleapis.com/chart?cht=tx&chl=x 的vector是多少,我们只要知道 https://chart.googleapis.com/chart?cht=tx&chl=x 和另外一个vector https://chart.googleapis.com/chart?cht=tx&chl=z 之间的内积值,或是只要知道Kernel function就可以做所有的optimization,这件事我们就称为Kernel Trick

Kernel Trick

我们之前有说过如果是linear的model会有很多限制,你可能要对输入的feature做一个feature transform,它才能用linear model来处理,如果再Neural Network里面,就用好几个hidden layer来做feature transform。

假设有一笔data是二维的,我们想要先对他做feature transform,在feature transform以後再去apply linear SVM。假设feature transform的结果是 https://chart.googleapis.com/chart?cht=tx&chl=%5Cphi(x),如果要算 https://chart.googleapis.com/chart?cht=tx&chl=xhttps://chart.googleapis.com/chart?cht=tx&chl=z 的Kernel function,我们就可以把值带到feature transform的function里面再做内积,简化一下就会发现算出来的值会等同於 https://chart.googleapis.com/chart?cht=tx&chl=x%2C%20z 在做feature transform之前,先做内积以後再平方算出来的值。

Sigmoid Kernel

Sigmoid Kernel是 https://chart.googleapis.com/chart?cht=tx&chl=K(x%2C%20z)%20%3D%20tanh(x%20%5Ccdot%20z),如果我们使用的是Sigmoid Kernel,这个 https://chart.googleapis.com/chart?cht=tx&chl=f(x) 就可以想成它其实是一个只有一个hidden layer的Neural Network,因为你的 https://chart.googleapis.com/chart?cht=tx&chl=x 会跟所有的 https://chart.googleapis.com/chart?cht=tx&chl=x%5En 做内积,就好像是有一个Neuron的weight就是某一笔data,而Neuron的数目就是看有几个support vector。

https://chart.googleapis.com/chart?cht=tx&chl=x 是有structure的data,我们其实就可以直接设计一个Kernel function,例如 https://chart.googleapis.com/chart?cht=tx&chl=x 是一个sequence,我们就很难把它表示成vector,那我们只要知道怎麽计算两个sequence之间的similarity就有机会把这个similarity当作Kernel来使用,而我们还可以透过Mercer's theory来检查你设计的function可不可以拆成两个vector做内积以後的结果。

在语音上,假设现在要做的分类对象是Audio Segment,每一段声音讯号会用vector sequence来表示,但你不需要知道一段声音讯号变成vector之後长什麽样子,你只要直接定一个Kernel function,就可以直接用Kernel Trick在SVM。

Deep Learning vs SVM

我们之前说,Deep Learning的前几个layer可以看做是Feature Transformation,最後一个layer可以看做是Linear Classifier。而SVM做的也是很类似的事情,它前面先apply一个Kernel function,把feature转到high dimension上面,接着在high dimension space上面就可以apply Linear Classifier,只是在SVM里面一般Linear Classifier都会用Hinge Loss。


参考资料

李宏毅老师 - ML Lecture 20


<<:  Day 27 - Greedy

>>:  用 Python 畅玩 Line bot - 08:Audio message part2

【Day 11】- 再次创造 Ghost Process,这次找不到了吧哈哈(基於修改 PspCidTable 隐藏的 Rookit)

Agenda 资安宣言 测试环境与工具 学习目标 前情提要 技术原理与程序码 References ...

EP01 - 开始建置流程之前

英国面包、法国面包、德国面包通通都有, 就是没有属於日本的面包既然如此今後只好自己创造, 这故事就...

ASP.NET MVC 从入门到放弃 (Day5) -C# 判断式 回圈介绍

接着来讲讲常用的判断式写法.... 简单来说以下就是玩攻略游戏 在选择选项的逻辑.... 单项if写...

Day 28 JavaScript < 简单介绍>

1.JS是什麽? Java Script 是一种运行在客户端的脚本语言 (script就是脚本的意思...

Day8 - TextView(二)

上一篇把"Hello World!"更改成了了 但字体太小了,看不清楚到底打对还...