-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSVMtrain_QP.m
More file actions
32 lines (25 loc) · 831 Bytes
/
SVMtrain_QP.m
File metadata and controls
32 lines (25 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [ alpha, b ] = SVMtrain_QP( X, Y, C, sigma)
% SVM-QP Solves de dual quadratic program of the L1-regularized SVM problem
% It solves the QP using CVX. The return variables are the dual variables
% of the SVM problem, which are then used for testing.
m = size(X,1);
K = zeros(m);
for i = 1:m
for j = 1:m
K(i,j) = RBF(X(i,:), X(j,:), sigma);
end
end
one = ones(m,1);
alpha = zeros(m,1);
cvx_begin
variables alpha(m)
maximize(-1/2 * (alpha.*Y)'*K*(alpha.*Y) + one'*alpha)
subject to
0 <= alpha <= C
alpha'*Y == 0
cvx_end
% To calculate b we pick an arbitrary support vector
ind = find(alpha>=C*0.00001 & alpha<=C*(1-0.00001));
ind = ind(1);
b = Y(ind) - alpha'*(Y.*K(:,ind));
end