-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_SVM_SMO.m
More file actions
81 lines (74 loc) · 1.86 KB
/
function_SVM_SMO.m
File metadata and controls
81 lines (74 loc) · 1.86 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
%{
SVM implemented by SMO(sequential minization optimization?
%}
function [w,b] = function_SVM_SMO(x1,x2,C,mini)
x = [x1,x2];
n = size(x,2);
y = zeros(1,n);
y(1,1:size(x1,2)) = 1;
y(1,size(x1,2)+1:n) = -1;
alp = zeros(1,n);
% yi * yj * xi' * xj
x_y = zeros(n,n);
for i = 1 : n
for j = 1 : n
x_y(i,j) = y(1,i) * y(1,j) * x(:,i)' * x(:,j);
end
end
%init w,b
w = function_SVM_w(alpha, x, y);
b = function_SVM_b(alpha, y, x, C, x_y);
%the distance from satisfied kkt
delta = zeros(1,n);
% satisfied kkt
while 1 = 1
k = 0;
type = 0;% type 1: 0<alp<C,type 2: 0=alp,type 3: alp=C,
%0<alp<C
for i = 1 : n
sum = y(1,i) * (w' * x(:,i) + b);
if 0 < alp(1,i) && alp(1,i) < C
if abs(sum - 1) >= min
type = 1;
k = i;
break;
end
end
end
% alpha = 0
if type == 0
for i = 1 : n
sum = y(1,i) * (w' * x(:,i) + b);
if alp(1,i) == 0
if sum < 1
type = 2;
k = i;
break;
end
end
end
end
% alpha = C
if type == 0
for i = 1 : n
sum = y(1,i) * (w' * x(:,i) + b);
if abs(alp(1,i) - C) < mini
if sum >= 1
type = 3;
k = i;
break;
end
end
end
end
% all satisfied kkt
if type == 0
break;
end
% chose k,k+1
k_1 = mod(k,n) + 1;
if type == 1
alpha(1,k)
end
end
end