1
پشتیبان بردار ماشین
Instructor : Saeed Shiry
2
مقدمه
SVM شاخه جزو که است ای کننده بندی دستهKernel Methods محسوب ماشین دریادگیری
میشود. SVM سال و Vapnikتوسط 1992در شده معرفی
پایه گردیده statistical learning theoryبر بنااست.
شهرتSVM تشخیص در آن موفقیت بخاطرعصبی های شبکه با که است نویس دست حروف
: میکند برابری شده تنظیم خطا% 1.1بدقت
3
مقدمه
متمایز و تشخیص ها الگوریتم دسته این هدفطریق ) از هاست داده در پیچیده الگوهای کردن
) غیره و پاکسازی رنکینگ، بندی، دسته کالسترینگ، : مطرح مسایل
دهیم نمایش چگونه را پیچیده الگوهای مسئله از کنیم overfittingچگونه پرهیز
4
اصلی ایده
جداپذیر خطی بصورت ها دسته اینکه فرض باحاشیه حداکثر با هائی ابرصفحه باشند،
(maximum margin ) دسته که آورد می بدست را. کنند جدا را ها
جداپذیر خطی بصورت ها داده که مسایلی درپیدا نگاشت بیشتر ابعاد با فضای به ها داده نباشندبصورت جدید فضای این در را آنها بتوان تا میکنند
. نمود جدا خطی
5
تعریف
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(
6
: خطی جداسازی مسئله Linear Discrimination
خطی بصورت که باشند داشته وجود دسته دو اگردو این کننده جدا بهترین باشند، جداپذیر هم از
چیست؟ دستهمیتوانند الگوریتم پرسپترون جمله از مختلفی های
. دهند انجام را جداسازی این بر اینکار عهده از بخوبی الگوریتمها این همه آیا
میآیند؟
w
x0w = í + 1
x0w = í à 1x0w = íSeparating Surface:
A+A-
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
Intuitions
X
X
O
OO
O
OOX
X
X
X
X
XO
O
11
A “Good” Separator
X
X
O
OO
O
OOX
X
X
X
X
XO
O
12
Noise in the Observations
X
X
O
OO
O
OOX
X
X
X
X
XO
O
13
Ruling Out Some Separators
X
X
O
OO
O
OOX
X
X
X
X
XO
O
14
Lots of Noise
X
X
O
OO
O
OOX
X
X
X
X
XO
O
15
Maximizing the Margin
X
X
O
OO
O
OOX
X
X
X
X
XO
O
16
داخلی ضرب
معیاری میتوان را داخلی تشابه ضرب دانست از
حالت نمایش nدر زیر بصورت آنرا میتوان بعدیداد.
a
cosbaba
b
n
iii
n ba1
,, baRba
17
کننده جدا صفحه ابر یا خط
0.0
bxiiwn
i
0. bxwT
02211 bXwXw
) دو: ) که صفحه ابر خط بهترین کردن پیدا هدف . معادله بعدی دو حالت در کند جدا هم از را دسته
: است زیر بصورت خط این حالت خواهیم nدر داشت: بعدی
Class 1Class -1
X2
X1
18
دسته SVMایده سازی جدا برایها
: بسازید مرزی صفحه دو و کرده رسم بندی دسته صفحه با موازی مرزی صفحه دو
. کنند برخورد ها داده به که میکنیم دور هم از آنقدر را آندو مرزی صفحات از را فاصله بیشترین که بندی دسته صفحه
. بود خواهد کننده جدا بهترین باشد، داشته0. bxw
Class 1Class -1
Cbxw
. Cbxw
.
19
حاشیه حداکثر
مثالهای اگر یادگیری تئوری در ای قضیه طبق بربین از باشند، شده بندی دسته بدرستی آموزشیداده حاشیه که جداسازی آن خطی، جداسازهای
را تعمیم خطای میکند حداکثر را آموزشی های . کرد خواهد .0حداقل bxw
Class 1Class -1
Cbxw
. Cbxw
.
20
حاشیه؟ حداکثر چرا
. باشد راه ترین مطمئن که میرسد نظر به برمبنای هائی که VC dimensionتئوری دارد وجود
. میکند اثبات آنرا بودن مفید داده جواب خوب خیلی روش این تجربی بطور
است.
21
پشتیبان بردار
های صفحه ابر به آموزشی های داده نزدیکترینکننده پشتیبان جدا میشوند بردار نامیده
Class 1Class -1
X2
X1
SV
SV
SV
22
و SVMتعمیم
از مناسب استفاده صورت الگوریتم SVMدر این: داشت خواهد خوبی تعمیم قدرت
زیاد ابعاد داشتن از (high dimensionality)علیرغمoverfitting . از ناشی خاصیت این میکند پرهیز
optimization است الگوریتم این: اطالعات سازی فشرده
استفاده پشتیبان بردارهای از آموزشی های داده بجایمیکند.
23
بعدی دو حالت برای مسئله حل آموزشی های نمونه
x n
y {-1, 1} گیری تصمیم تابع
f)x( = sign)<w,x> + b(w n
b صفحه ابر
>w, x> + b = 0w1x1 + w2x2 … + wnxn + b = 0
مقادیر :W, bمیخواهیم که کنیم پیدا ای رابگونه کند بندی دسته بدقت را آموزشی های نمونه
باشند پذیر جدا خطی بصورت ها داده که فرض این با نماید حداکثر را حاشیه
24
Linear SVM Mathematically Let training set {)xi, yi(}i=1..n, xiRd, yi {-1, 1} be
separated by a hyperplane with margin ρ. Then for each training example )xi, yi(:
For every support vector xs the above inequality is an equality. After rescaling w and b by ρ/2 in the equality, we obtain that distance between each xs 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)(y
br s
Ts
yi(wTxi + b) ≥ ρ/2
25
بعدی دو حالت برای مسئله حل
با براست برا مبدا از جداکننده خط فاصله مثل ای نمونه با 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بردار صفحه دو هر بر. بود خواهد عمود ومنفی
26
کننده جدا خطوط بین حاشیه تعیین
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
27
حاشیه پهنای محاسبه: میگیریم نظر در زیر بصورت را منفی و مثبت صفحه
Plus-plane = { x : w . x + b = +1 } Minus-plane = { x : w . x + b = -1 }
بردارw. بود خواهد عمود ومنفی مثبت صفحه بر کنید و -Xفرض بوده منفی صفحه در ای +Xنقطه
به مثبت صفحه در نقطه . -Xنزدیکترین باشد
28
حاشیه پهنای محاسبه
که صفحه +Xرابه -Xخطی دو هر بر میکند وصل . مضربی صفحه دو بین فاصله لذا بود خواهد عمود
.Wاز بود خواهد: داشت خواهیم اینصورت در
x+= x-+ λ w for some value of λ.
29
حاشیه پهنای محاسبه
که :میدانیم w . x+ + b = +1 w . x- + b = -1 X+= x-+ λ w | x+- x-| = M
میتوان برحسب Mلذا .b و Wرا کرد محاسبه
30
حاشیه پهنای محاسبه
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
31
حاشیه پهنای محاسبه
32
محدودیت
با را ها دسته مقدار فوق بعدی دو مثال برای 1اگر:1و داریم- کنیم مشخص
<w,xi> + b ≥ 1 for y=1
<w,xi> + b -1 for y= -1 نوشت زیر آنرابصورت میتوان که
yi )<w,xi> + b( ≥ 1 for all i
33
مسئله حل بندی جمع درSVM : هستیم زیر معادالت همزمان حل بدنبال آموزشی مثالهای داشتن ;i=1,2,…Nکه ( xi, yi)با
yi{+1,-1}
Minimise ||w||2
Subject to : yi )<w,xi> + b( ≥ 1 for all i Note that ||w||2 = wTw
مسئله یک محدودیت quadratic programmingاین با . شناخته روشهای است خطی نامعادالت بصورت هائی
. اند آمده بوجود هائی مسئله چنین برای ای شده
34
Quadratic Programming
35
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:
: the Lagrange multiplier For multiple constraints gi)x( = 0, i=1, …, m, we need a
Lagrange multiplier i for each of the constraints
36
Recap of Constrained Optimization The case for inequality constraint gi)x( 0 is similar, except that the
Lagrange multiplier i should be positive If x0 is a solution to the constrained optimization problem
There must exist i ≥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
37
معادله حل راه Construct & minimise the Lagrangian
Take derivatives wrt. w and b, equate them to 0
The Lagrange multipliers i 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 i
38
معادله حل راه
6=1.4
Class 1
Class 2
1=0.8
2=0
3=0
4=0
5=07=0
8=0.6
9=0
10=0
39
The Dual Problem If we substitute to Lagrangian , we have
Note that
This is a function of i only
40
The Dual Problem
Properties of i 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 i only It is known as the dual problem: if we know w, we know all i;
if we know all i, 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:
41
The Dual Problem
This is a quadratic programming )QP( problem A global maximum of i can always be found
w can be recovered by
42
معادله حل راه
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!
43
جدید های داده بندی دسته
مقادیر آنکه از معادالت (**, 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!
44
حل راه های ویژگی
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
45
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 )i,j( 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
46
بصورتخطی که هائی دادهنیستند پذیر جدا
در قوی بسیار فرض ها SVMیک داده که بود این . عمل در حالیکه در باشند جداپذیر خطی بصورت
. نیست صحیح فرض این مواقع بسیاری در
+
+
+
+ + +
+
47
های متغیر slackافزودن در خطا مقداری و آمده کوتاه اندکی که است این حل راه یک
! بپذیریم را بندی دسته متغیر معرفی با کار نمونه i این تعداد نشانگر که میشود انجام
تابع توسط که است .wTx+bهائی میشوند ارزیابی غلط
Class 1
Class 2
48
های متغیر slackافزودن
متغیر معرفی های , i, i=1, 2, …, Nبا محدودیترابطه و شده تر ساده قبلی
yi )<w,xi> + b( ≥1
: میکند تغییر زیر بصورتyi )<w,xi> + b( ≥1- i , i ≥ 0
. باشند صفر باید ها متغیر این همه آل ایده حالت در
49
به میشود تبدیل سازی بهینه مسئله اینصورت در: wیافتن شود مینیمم زیر معادله که نحوی به
آن در . C > 0که سعی شدن اضافه جمله میباشدمتغیرهای همه امکان حد تا کوچک slackدارد را
نماید.
iby
Tosubject
C
iiiT
i
ii
,0,1)(
21 22
xw
w
50
خواهد زیر بصورت جدید حالت در دوگان رابطهبود.
مناسب مسئله Cمقدار های داده اساس بر. میشود انتخاب
i j
jTijiji
ii yy xx 2
1find i that maximizes
subject to iC
y
i
N
i ii
,0
,01
51
Soft Margin Hyperplane If we minimize wi i, i can be computed by
i 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
52
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 i now
Once again, a QP solver can be used to find i
53
جداسازی خطی : مسئله غیرویژگی فضای در یادگیری
را آنها ویژگی فضای یک به داده نگاشت با میتوان: نمود جداپذیر خطی بصورت
54
ویژگی فضای به داده تبدل
ابعاد اینکه برای باشد پرهزینه میتواند ویژگی فضای در محاسبات انجام. دارد بیشتری
. است نهایت بی فضا این ابعاد کلی حالت در از مشکل این بر غلبه .kernel trickبرای میشود استفاده
( )
( )
( )( )( )
( )
( )( )
(.)( )
( )
( )
( )( )
( )
( )
( )( )
( )
Feature spaceInput spaceNote: feature space is of higher dimension than the input space in practice
55
ویژگی فضای مشکالت
است مشکل باال ابعاد با ویژگی فضای با کردن کار ممکن محاسباتی هزینه رفتن باال مسئله بر عالوه
بواسطه نیز تعمیم مشکل curse ofاستdimensionality. آید بوجود
56
فضای به مستقیم غیر نگاشتویژگی
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
57
کرنل
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(
58
Suppose ).( 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 ).( explicitly
This use of kernel function to avoid carrying out ).( explicitly is known as the kernel trick
An Example for (.) and K(.,.)
59
: نمونه های کرنل
)2',1tanh()',( :network Neural
)/||'||exp()',(:basis Radial
))',(1()',( :polynomial Degreeth -2
kxxkxxK
cxxxxK
xxxxKm m
2
22
21
21
)(),(2)( jijiK
x
xx
x
xxxxx
60
ای: جمله چند کرنل مثال
61
Modification Due to Kernel Function Change all inner products to kernel functions For training,
Original
With kernel function
62
Modification Due to Kernel Function For testing, the new data z is classified as
class 1 if f0, and as class 2 if f <0
Original
With kernel function
63
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(
64
ها کرنل ساخت
: است صادق ها کرنل مورد در زیر قوانین مجموعه 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……
روی از را پیچیده های کرنل میتوان ترتیب این به. ساخت تر ساده های کرنل
65
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,y( = )xy+1(2
C is set to 100 We first find i )i=1, …, 5( by
66
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
67
Example
Value of discriminant function
1 2 4 5 6
class 2 class 1class 1
68
کاربرد از مثالی
حروف تشخیصنویس دست
پست اداره دراستفاده مآ با ریکا
روش این ازبه اند توانسته
حدود در خطائی4. برسند%
69
از استفاده برای 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
70
کرنل تابع انتخاب
روش در مسئله ترین . 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
71
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.
72
ضعف و قوت 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 Non-traditional data like strings and trees can be used as
input to SVM, instead of feature vectors Weaknesses
Need to choose a “good” kernel function.
73
گیری نتیجه
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.
74
های جنبه 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
75
Multi-class Classification
SVM is basically a two-class classifier One can change the QP formulation to allow multi-
class classification More commonly, the data set is divided into two
parts “intelligently” in different ways and a separate SVM is trained for each way of division
Multi-class classification is done by combining the output of all the SVM classifiers Majority rule Error correcting code Directed acyclic graph
76
افزار نرم
در میتوانید را مختلف های افزار نرم از لیستی: بیابید زیر آدرس
http://www.kernel-machines.org/software.html نظیر افزارها نرم بصورت LIBSVMبرخی میتوانند
. کنند کار ای دسته چند افزار رفته SVMLightنرم بکار مختلفی مراجع در
است. چندینtoolbox درMatlab برایSVM معرفی
. اند شده
77
مراجع
[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.