forked from tourmaline612/FSVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSVM_C_ForClass.m
77 lines (67 loc) · 2.15 KB
/
FSVM_C_ForClass.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
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
function [bestacc,bestc] = FSVM_C_ForClass(train_label, train, cmin, cmaC, v, cstep, opts)
% Find the optimal parameter C with v-inner fold CV for FSVM
% the semi-whitening initialization is adopted for parameter fine-tuning
%% about the parameters of SVMcg
if nargin < 6
cstep = 0.8;
end
if nargin < 5
v = 5;
end
if nargin < 4
cmaC = 8;
cmin = -8;
end
%% C:c Y:g cg:CVaccuracy
C = cmin:cstep:cmaC;
[m,n] = size(C);
cg = zeros(m,n);
eps = 1e-3;
%% record acc with different c ,and find the bestacc with the smallest c
bestc = 1;
bestacc = 0;
basenum = 2;
epsilon = opts.epsilon;
rho = 1;
%trainset: nxd
trainset = bsxfun(@minus, train, mean(train));
S = cov( trainset, 1 ); % equal to cov( trainset ) * (size(trainset,1)-1)/size(trainset,1);
[ U, E ] = eig( full(S) );
[ dummy, order ] = sort( diag(E), 'descend' );
U = U( : , order );
% S2: http://ufldl.stanford.edu/wiki/index.php/%E7%99%BD%E5%8C%96
dummy = dummy + epsilon;
Sigma = diag((dummy*rho).^(-0.5));
M = U * Sigma * U'; %M=L'L
L = Sigma^(0.5) * U';
% if L = Sigma * U', then Ztrain = trainset*L' is the PCA whitening,
% Ztrain = trainset*M is the ZCA whitening
% we define the L = Sigma^(0.5) * U', Ztrain = trainset*L' as the semi-whitening
Ztrain = trainset * L';
for i = 1:m
for j = 1:n
cmd = ['-v ',num2str(v),' -c ',num2str( basenum^C(i,j) ), ' -q'];
cg(i,j) = svmtrain( train_label, Ztrain, cmd);
if cg(i,j) <= 55
continue;
end
if cg(i,j) > bestacc
bestacc = cg(i,j);
bestc = basenum^C(i,j);
end
if abs( cg(i,j)-bestacc )<=eps && bestc > basenum^C(i,j)
bestacc = cg(i,j);
bestc = basenum^C(i,j);
end
end
end
% %% to draw the acc with diffesrent c & g
% gcf = plot(cg);
% xlabel('log2c','FontSize',10);
% ylabel('Accuracy (%)','FontSize',10);
% secondline = ['Best c=',num2str(bestc), ...
% ' CVAccuracy=',num2str(bestacc),'%'];
% firstline = 'Parameter evaluation:';
% title({firstline;secondline},'Fontsize',12);
% close all;
end