-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVMcgForClass.m
40 lines (34 loc) · 1023 Bytes
/
SVMcgForClass.m
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
function [bestacc,bestc,bestg] = SVMcgForClass(label,train)
%% X:c Y:g cg:CVaccuracy
[X,Y] = meshgrid(-8:1:8,-8:1:8);
[m,n] = size(X);
cg = zeros(m,n);
eps = 10^(-4);
%% record acc with different c & g, and find the bestacc with the smallest c
bestc = 1;
bestg = 0.1;
bestacc = 0;
basenum = 2;
for i = 1:m
% Parallel training
parfor j = 1:n
cmd = ['-v ',num2str(10),' -c ',num2str( basenum^X(i,j) ),' -g ',num2str( basenum^Y(i,j) )];
cg(i,j) = svmtrain(label,train,cmd);
end
% Find the bestacc with the smallest c
for j = 1:n
if cg(i,j) <= 55
continue;
end
if cg(i,j) > bestacc
bestacc = cg(i,j);
bestc = basenum^X(i,j);
bestg = basenum^Y(i,j);
end
if abs( cg(i,j) - bestacc )<=eps && bestc > basenum^X(i,j)
bestacc = cg(i,j);
bestc = basenum^X(i,j);
bestg = basenum^Y(i,j);
end
end
end