1 ماشین بردار پشتیبان author : saeed shiry with little change by: keyvanrad 1393...
TRANSCRIPT
1
ماشین بردار پشتیبان
Author : Saeed Shiry
With little change by: Keyvanrad
1393- 1394( Spring)
2
مقدمه
SVM شاخه جزو که است ای کننده بندی دستهKernel Methods می محسوب ماشین دریادگیری
شود. SVM سال و Vapnikتوسط 1992در شده معرفی
پایه گردیده statistical learning theoryبر بنااست.
شهرتSVM تشخیص در آن موفقیت بخاطرعصبی های شبکه با که است نویس دست حروف
: کند می برابری شده تنظیم خطا% 1.1بدقت
3
ایده اصلی
جداپذیر خطی بصورت ها دسته اینکه فرض باحاشیه حداکثر با هائی ابرصفحه باشند،
(maximum margin )دسته که آورد می بدست را. کنند جدا را ها
جداپذیر خطی بصورت ها داده که مسایلی درپیدا نگاشت بیشتر ابعاد با فضای به ها داده نباشندبصورت جدید فضای این در را آنها بتوان تا کنند می
. نمود جدا خطی
4
تعریف
Support Vector Machines are a system for efficiently training linear learning machines in kernel-induced feature spaces, while respecting the insights of generalisation theory and exploiting optimisation theory. Cristianini & Shawe-Taylor )2000(
5
مسئله جداسازی خطی: Linear Discrimination
خطی بصورت که باشند داشته وجود دسته دو اگردو این کننده جدا بهترین باشند، جداپذیر هم از
چیست؟ دستهمیتوانند الگوریتم پرسپترون جمله از مختلفی های
. دهند انجام را جداسازی این بر اینکار عهده از بخوبی الگوریتمها این همه آیا
میآیند؟
Separating Surface:
A+A-
6
Intuitions
X
X
O
OO
O
OOX
X
X
X
X
XO
O
7
Intuitions
X
X
O
OO
O
OOX
X
X
X
X
XO
O
8
Intuitions
X
X
O
OO
O
OOX
X
X
X
X
XO
O
9
Intuitions
X
X
O
OO
O
OOX
X
X
X
X
XO
O
10
A “Good” Separator
X
X
O
OO
O
OOX
X
X
X
X
XO
O
11
Noise in the Observations
X
X
O
OO
O
OOX
X
X
X
X
XO
O
12
Ruling Out Some Separators
X
X
O
OO
O
OOX
X
X
X
X
XO
O
13
Lots of Noise
X
X
O
OO
O
OOX
X
X
X
X
XO
O
14
Maximizing the Margin
X
X
O
OO
O
OOX
X
X
X
X
XO
O
15
خط یا ابر صفحه جدا کننده
0.1
bxiiwn
i0. bxwT
02211 bxwxw
هدف: پیدا کردن بهترین خط ) ابر صفحه( که دودسته را از هم جدا کند. در حالت دو بعدی معادله
این خط بصورت زیر است: در حالتn:بعدی خواهیم داشت
Class 1Class -1
𝑥2
𝑥1
16
برای جدا سازی SVMایده دسته ها
: بسازید مرزی صفحه دو کرده رسم بندی دسته صفحه با موازی مرزی صفحه دو
برخورد ها داده به که میکنیم دور هم از آنقدر را آندو وکنند.
صفحات از را فاصله بیشترین که بندی دسته صفحه. بود خواهد کننده جدا بهترین باشد، داشته .0مرزی bxw
Class 1Class -1
Cbxw
. Cbxw
.
17
حداکثر حاشیه
مثالهای اگر یادگیری تئوری در ای قضیه طبق بربین از باشند، شده بندی دسته بدرستی آموزشیداده حاشیه که جداسازی آن خطی، جداسازهایرا تعمیم خطای کند می حداکثر را آموزشی های
. کرد خواهد .0حداقل bxw
Class 1Class -1
Cbxw
. Cbxw
.
18
چرا حداکثر حاشیه؟
. باشد راه ترین مطمئن که میرسد نظر به برمبنای هائی که VC dimensionتئوری دارد وجود
. کند می اثبات آنرا بودن مفید داده جواب خوب خیلی روش این تجربی بطور
است.
19
بردار پشتیبان
های صفحه ابر به آموزشی های داده نزدیکترینکننده پشتیبان جدا شوند بردار می نامیده
Class 1Class -1
SV
SV
SV
𝑥2
𝑥1
20
SVMتعمیم و
از مناسب استفاده صورت الگوریتم SVMدر این: داشت خواهد خوبی تعمیم قدرت
( زیاد ابعاد داشتن از( high dimensionalityعلیرغمoverfitting . از ناشی خاصیت این کند می پرهیز
optimization. است الگوریتم این: اطالعات سازی فشرده
می استفاده پشتیبان بردارهای از آموزشی های داده بجایکند.
21
SVMحل مسئله آموزشی های نمونه
x n
y {-1, 1} گیری تصمیم تابع
f)x( = sign)<w,x> + b(w n
b صفحه ابر
>w, x> + b = 0w1x1 + w2x2 … + wnxn + b = 0
مقادیر خواهیم : W, bمی که کنیم پیدا ای بگونه را کند بندی دسته دقت به را آموزشی های نمونه
باشند پذیر جدا خطی بصورت ها داده که فرض این با نماید حداکثر را حاشیه
22
Linear SVM Mathematically Let training set be separated by a hyperplane with
margin ρ. Then for each training example :
For every support vector the above inequality is an equality. After rescaling w and b by ρ/2 in the equality, we obtain that distance between each and the hyperplane is
Then the margin can be expressed through )rescaled( w and b as:
wTxi + b ≤ - ρ/2 if yi = -1wTxi + b ≥ ρ/2 if yi = 1
w
22 r
ww
xw 1||
br s
T
yi(wTxi + b) ≥ ρ/2
23
SVMحل مسئله
با براست برا مبدا از جداکننده خط فاصله مثل ای نمونه با xفاصله است برابر کننده جدا خط از
w
bwx
w
xf
)(
X2
X1
f)x(=0 f)x(>0f)x(<0
w
xw
b
w
b
w
bwx
w
xf
)(
بر هر دو صفحه مثبت wبردار ومنفی عمود خواهد بود.
24
روش دوم برای تعیین حاشیه بین خطوط جدا کننده
Plus-plane = { x : w . x + b = +1 }
Minus-plane = { x : w . x + b = -1 }
Classify as..
-1 if w . x + b <= -1
+1 if w . x + b >= 1
25
محاسبه پهنای حاشیه: میگیریم نظر در زیر بصورت را منفی و مثبت صفحه
Plus-plane = { x : w . x + b = +1 } Minus-plane = { x : w . x + b = -1 }
بردارw. بود خواهد عمود ومنفی مثبت صفحه بر کنید و- Xفرض بوده منفی صفحه در ای + Xنقطه
به مثبت صفحه در نقطه . Xنزدیکترین باشد-
26
محاسبه پهنای حاشیه
که صفحه+ Xرابه- Xخطی دو هر بر میکند وصل . مضربی صفحه دو بین فاصله لذا بود خواهد عمود
.Wاز بود خواهد: داشت خواهیم اینصورت در
x+= x-+ λ w for some value of λ.
27
محاسبه پهنای حاشیه
که : میدانیم w . x+ + b = +1 w . x- + b = -1 X+= x-+ λ w | x+- x-| = M
میتوان برحسب Mلذا .bو Wرا کرد محاسبه
28
محاسبه پهنای حاشیه
w . x+ + b = +1 w . x- + b = -1 X+= x-+ λ w | x+- x-| = M
w.) x-+ λ w) + b = +1
w.x-+ λ w.w + b = +1
-1+ λ w.w = +1
λ=2/ w.w
29
محاسبه پهنای حاشیه
30
محدودیت
با را ها دسته مقدار فوق بعدی دو مثال برای 1اگر:1و داریم- کنیم مشخص
<w,xi> + b ≥ +1 for y=1
<w,xi> + b £ -1 for y= -1 نوشت زیر صورت به آنرا توان می که
yi )<w,xi> + b( ≥ 1 for all i
31
جمع بندی حل مسئله درSVM: هستیم زیر معادالت همزمان حل بدنبال که آموزشی مثالهای داشتن با
Minimise ||w||2
Subject to : yi )<w,xi> + b( ≥ 1 for all i Note that ||w||2 = wTw
مسئله یک محدودیت quadratic programmingاین با . شناخته روشهای است خطی نامعادالت بصورت هائی
. اند آمده بوجود هائی مسئله چنین برای ای شده
32
Quadratic Programming
33
Recap of Constrained Optimization
Suppose we want to: minimize f)x( subject to g)x( = 0 A necessary condition for x0 to be a solution:
a: the Lagrange multiplier For multiple constraints gi)x( = 0, i=1, …, m, we need a
Lagrange multiplier ai for each of the constraints
34
Recap of Constrained Optimization The case for inequality constraint gi)x( £ 0 is similar, except that the
Lagrange multiplier ai should be positive If x0 is a solution to the constrained optimization problem
There must exist ai ≥ 0 for i=1, …, m such that x0 satisfy
The function is also known as the Lagrangrian;
we want to set its gradient to 0
35
معادله حل راه Construct & minimise the Lagrangian
Take derivatives wrt. w and b, equate them to 0
The Lagrange multipliers ai are called ‘dual variables’
Each training point has an associated dual variable.
Ni
bybL
i
N
iiii
,...1,0 constraint wrt.
]1)([||||2
1),,(
1
2
wxww
0]1)([:
0),,(
0),,(
1
1
bycondKKT
yb
bL
ybL
iii
N
iii
N
iiii
wx
w
xww
w
parameters are expressed as a linear combination of training points
only SVs will have non-zero ai
36
راه حل معادله
a6=1.4
Class 1
Class 2
a1=0.8
a2=0
a3=0
a4=0
a5=0a7=0
a8=0.6
a9=0
a10=0
37
The Dual Problem If we substitute to Lagrangian , we have
Note that
This is a function of ai only
38
The Dual Problem
Properties of ai when we introduce the Lagrange multipliers
The result when we differentiate the original Lagrangian w.r.t. b
The new objective function is in terms of ai only It is known as the dual problem: if we know w, we know all ai;
if we know all ai, we know w The original problem is known as the primal problem The objective function of the dual problem needs to be
maximized! The dual problem is therefore:
39
The Dual Problem
This is a quadratic programming )QP( problem A global maximum of ai can always be found
w can be recovered by
40
راه حل معادله
So, Plug this back into the Lagrangian to obtain the dual formulation The resulting dual that is solved for by using a QP solver:
The b does not appear in the dual so it is determined separatelyfrom the initial constraints
SVi iii
N
i iii yyw xx 1
Niy
yyW
ii
N
i i
N
i ijiji
N
ji ji
,...1,0,0 :subject to
2
1)( :maximise
1
11,
xx
Data enters only in the form of dot products!
41
دسته بندی داده های جدید
پس از آنکه مقادیر(*, b* با حل معادالت )quadratic بر اساس را برای دسته بندی SVMداده های ورودی بدست آمد، می توان
نمونه های جدید بکار برد. اگرx یک نمونه جدید باشد، دسته بندی آن بصورت زیر مشخص
می شود:
sign[f)x, *, b*(], where ***
1
***** ),,( bybybbfSVi iii
N
i iii xxxxxwx
Data enters only in the form of dot products!
42
ویژگی های راه حل
The solution of the SVM, i.e. of the quadratic programming problem with linear inequality constraints has the nice property that the data enters only in the form of dot products!
Dot product )notation & memory refreshing(: given x=)x1,x2,…xn( and y=)y1,y2,…yn(, then the dot product of x and y is xy=)x1y1, x2y2,…, xnyn(.
This is nice because it allows us to make SVMs non-linear without complicating the algorithm
43
The Quadratic Programming Problem Many approaches have been proposed
Loqo, cplex, etc. Most are “interior-point” methods
Start with an initial solution that can violate the constraints Improve this solution by optimizing the objective function and/or
reducing the amount of constraint violation For SVM, sequential minimal optimization )SMO( seems to be
the most popular A QP with two variables is trivial to solve Each iteration of SMO picks a pair of )ai,aj( and solve the QP
with these two variables; repeat until convergence In practice, we can just regard the QP solver as a “black-box”
without bothering how it works
44
داده هائی که بصورت خطی جدا پذیر نیستند
در قوی بسیار فرض ها SVMیک داده که بود این . عمل در حالیکه در باشند جداپذیر خطی بصورت
. نیست صحیح فرض این مواقع بسیاری در
+
+
+
+ + +
+
45
slackافزودن متغیر های
خطا مقداری و آمده کوتاه اندکی که است این حل راه یک! بپذیریم را بندی دسته در
متغیر معرفی با کار نشانگر xi این که شود می انجامتابع توسط که است هائی نمونه ارزیابی wTx+bتعداد غلط
. شوند می
Class 1
Class 2
46
slackافزودن متغیر های
متغیر معرفی های, xi, i=1, 2, …, Nبا محدودیترابطه و شده تر ساده قبلی
yi )<w,xi> + b( ≥1
: میکند تغییر زیر بصورتyi )<w,xi> + b( ≥1- xi , xi ≥ 0
صفر باید ها متغیر این همه آل ایده حالت درباشند.
47
به شود می تبدیل سازی بهینه مسئله اینصورت در:wیافتن شود مینیمم زیر معادله که نحوی به
آن در . C > 0که سعی شده اضافه جمله میباشدمتغیرهای همه امکان حد تا کوچک slackدارد را
نماید.
iby
Tosubject
C
iiiT
i
ii
,0,1)(
21 22
xw
w
48
خواهد زیر بصورت جدید حالت در دوگان رابطهبود.
مناسب مسئله Cمقدار های داده اساس بر. میشود انتخاب
i j
jTijiji
ii yy xx 2
1find ai that maximizes
subject to iC
y
i
N
i ii
,0
,01
49
Soft Margin Hyperplane If we minimize wi xi, xi can be computed by
xi are “slack variables” in optimization Note that i=0 if there is no error for xi i is an upper bound of the number of errors
We want to minimize
C : tradeoff parameter between error and margin The optimization problem becomes
50
The Optimization Problem The dual of this new constrained optimization problem is
w is recovered as
This is very similar to the optimization problem in the linear separable case, except that there is an upper bound C on ai now
Once again, a QP solver can be used to find ai
51
غیر خطی :مسئله جداسازی یادگیری در فضای ویژگی
را آنها ویژگی فضای یک به داده نگاشت با میتوان: نمود جداپذیر خطی بصورت
52
تبدیل داده به فضای ویژگی
ابعاد اینکه برای باشد پرهزینه میتواند ویژگی فضای در محاسبات انجام. دارد بیشتری
. است نهایت بی فضا این ابعاد کلی حالت در از مشکل این بر غلبه .kernel trickبرای شود می استفاده
f( )
f( )
f( )f( )f( )
f( )
f( )f( )
f(.) f( )
f( )
f( )
f( )f( )
f( )
f( )
f( )f( )
f( )
Feature spaceInput spaceNote: feature space is of higher dimension than the input space in practice
53
مشکالت فضای ویژگی
است مشکل باال ابعاد با ویژگی فضای با کردن کار ممکن محاسباتی هزینه رفتن باال مسئله بر عالوه
بواسطه نیز تعمیم مشکل curse ofاستdimensionality. آید بوجود
54
نگاشت غیر مستقیم به فضای ویژگی
We will introduce Kernels: Solve the computational problem of working with
many dimensions Can make it possible to use infinite dimensions efficiently in time / space Other advantages, both practical and conceptual
55
کرنل
Transform x )x( The linear algorithm depends only on xxi,
hence transformed algorithm depends only on )x()xi(
Use kernel function K)xi,xj( such that K)xi,xj(= )x()xi(
56
Suppose f).( is given as follows
An inner product in the feature space is
So, if we define the kernel function as follows, there is no need to carry out f).( explicitly
This use of kernel function to avoid carrying out f).( explicitly is known as the kernel trick
An Example for f(.) and K(.,.)
57
کرنل های نمونه:
)2',1tanh()',( :network Neural
)/||'||exp()',(:basis Radial
))',(()',( :polynomial Degreeth -2
kxxkxxK
cxxxxK
xxcxxKm m
2
22
21
21
)(2)(
zx,zx,x K
x
xx
x
Homogeneous Polynomial Kernel
)C=0(
58
مثال: کرنل چند جمله ای
59
Modification Due to Kernel Function Change all inner products to kernel functions For training,
Original
With kernel function
60
Modification Due to Kernel Function For testing, the new data z is classified as
class 1 if f³0, and as class 2 if f <0
Original
With kernel function
61
Modularity
Any kernel-based learning algorithm composed of two modules: A general purpose learning machine A problem specific kernel function
Any K-B algorithm can be fitted with any kernel Kernels themselves can be constructed in a modular
way Great for software engineering )and for analysis(
62
ساخت کرنل ها
: است صادق ها کرنل مورد در زیر قوانین مجموعه If K, K’ are kernels, then:
K+K’ is a kernel cK is a kernel, if c>0 aK+bK’ is a kernel, for a,b >0 Etc etc etc……
روی از را پیچیده های کرنل میتوان ترتیب این به. ساخت تر ساده های کرنل
63
Example Suppose we have 5 1D data points
x1=1, x2=2, x3=4, x4=5, x5=6, with 1, 2, 6 as class 1 and 4, 5 as class 2 y1=1, y2=1, y3=-1, y4=-1, y5=1
We use the polynomial kernel of degree 2 K)x,z( = )xz+1(2
C is set to 100 We first find ai )i=1, …, 5( by
64
Example By using a QP solver, we get
1=0, 2=2.5, 3=0, 4=7.333, 5=4.833 Note that the constraints are indeed satisfied The support vectors are {x2=2, x4=5, x5=6}
The discriminant function is
b is recovered by solving f)2(=1 or by f)5(=-1 or by f)6(=1, as x2 and x5 lie on the line and x4 lies on the line
All three give b=9
65
Example
Value of discriminant function
1 2 4 5 6
class 2 class 1class 1
66
مثالی از کاربرد
نویس دست حروف تشخیص آ پست اداره اند مدر توانسته روش این از استفاده با ریکا
حدود در خطائی .4به برسند%
67
از استفاده برای SVMمراحلبندی دسته
Prepare the data matrix Select the kernel function to use Execute the training algorithm using a QP
solver to obtain the i values
Unseen data can be classified using the i values and the support vectors
68
انتخاب تابع کرنل
روش در مسئله ترین . SVMجدی است کرنل تابع انتخاب: اسـت شده معرفی کار این برای متعددی اصول و روشها
diffusion kernel, Fisher kernel, string kernel, … روی از کرنل ماتریس آوردن بدست برای نیز تحقیقاتی و
. است انجام حال در موجود های داده عمل در
In practice, a low degree polynomial kernel or RBF kernel with a reasonable width is a good initial try
Note that SVM with RBF kernel is closely related to RBF neural networks, with the centers of the radial basis functions automatically chosen for SVM
69
SVM applications SVMs were originally proposed by Boser, Guyon and Vapnik in
1992 and gained increasing popularity in late 1990s. SVMs are currently among the best performers for a number of
classification tasks ranging from text to genomic data. SVMs can be applied to complex data types beyond feature
vectors )e.g. graphs, sequences, relational data( by designing kernel functions for such data.
SVM techniques have been extended to a number of tasks such as regression [Vapnik et al. ’97], principal component analysis [Schölkopf et al. ’99], etc.
Most popular optimization algorithms for SVMs use decomposition to hill-climb over a subset of αi’s at a time, e.g. SMO [Platt ’99] and [Joachims ’99]
Tuning SVMs remains a black art: selecting a specific kernel and parameters is usually done in a try-and-see manner.
70
SVMنقاط قوت و ضعف Strengths
Training is relatively easy Good generalization in theory and practice Work well with few training instances Find globally best model, No local optimal, unlike in neural
networks It scales relatively well to high dimensional data Tradeoff between classifier complexity and error can be
controlled explicitly Weaknesses
Need to choose a “good” kernel function.
71
نتیجه گیری
SVMs find optimal linear separator They pick the hyperplane that maximises the margin The optimal hyperplane turns out to be a linear
combination of support vectors The kernel trick makes SVMs non-linear learning
algorithms Transform nonlinear problems to higher dimensional space
using kernel functions; then there is more chance that in the transformed space the classes will be linearly separable.
72
SVMسایر جنبه های
How to use SVM for multi-class classification? One can change the QP formulation to become multi-class More often, multiple binary classifiers are combined One can train multiple one-versus-all classifiers, or
combine multiple pairwise classifiers “intelligently” How to interpret the SVM discriminant function value
as probability? By performing logistic regression on the SVM output of a
set of data )validation set( that is not used for training Some SVM software )like libsvm( have these
features built-in
73
نرم افزار
در میتوانید را مختلف های افزار نرم از لیستی: بیابید زیر آدرس
http://www.kernel-machines.org/software.html نظیر افزارها نرم بصورت LIBSVMبرخی میتوانند
. کنند کار ای دسته چند افزار رفته SVMLightنرم بکار مختلفی مراجع در
است. چندینtoolbox درMatlab برایSVM معرفی
. اند شده
74
مراجع
[1] b.E. Boser et al. A training algorithm for optimal margin classifiers. Proceedings of the fifth annual workshop on computational learning theory 5 144-152, Pittsburgh, 1992.
[2] l. Bottou et al. Comparison of classifier methods: a case study in handwritten digit recognition. Proceedings of the 12th IAPR international conference on pattern recognition, vol. 2, pp. 77-82.
[3] v. Vapnik. The nature of statistical learning theory. 2nd edition, Springer, 1999.