-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysvr.m
110 lines (93 loc) · 3.06 KB
/
mysvr.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
% ---------------------------------------------------------------------------------------------------
%
% mysvr.m
%
% This function trains a $\varepsilon$-Huber SVR model with a fixed set of free parameters.
%
% INPUTS:
% x ............. Training set (features in columns, samples in rows).
% y ............. Actual value to be estimated/predicted in the training set.
% e ............. Best epsilon-insensitivity zone.
% C ............. Best penalization factor.
% gam ............. Best gamma parameter in the $\varepsilon$-Huber cost function.
% sigmay ............. Best RBF kernel width.
% ker ............. Label for the kernel type (at this moment only RBF kernel, yker = 'rbf').
%
% OUTPUTS:
% nsv ............. Number of support vectors.
% w ............. (Alpha-Alpha^*).
% bias ............. Bias of the best model (b).
% ypred ............. Predictions for desired y
%
% José L. Rojo-Álvarez & Gustavo Camps-Valls
% 2004(c)
%
% ---------------------------------------------------------------------------------------------------
function [nsv,w,bias, ypred] = mysvr(x,y,e,C,gam,sigmay,ker)
% Initialization
N = length(y);
% Construct kernel matrix and alpha vector
H = mysvkernel2(ker,x,x,sigmay);
alfa=zeros(N,1); % Incognitus
alfabis=zeros(N,1);
w = zeros(N,1);
bias=0;
maxiter=10;
t=.95; % Update lambda^(*)
pp=zeros(maxiter,1);
for i=1:maxiter
% Compute errors
ypred=H*w;
er=y-ypred;
pp(i)=norm(er);
% Compute alphas
[alfa,alfabis]=misalfas(er,e,C,gam);
% Compute lambdas
[landa_new,landabis_new]=mislandas(alfa,alfabis,er,e);
if (i==1)
landa=landa_new;
landabis=landabis_new;
landa_old=landa;
landabis_old=landabis;
else
landa=t*landa_new+(1-t)*landa_old;
landabis=t*landabis_new+(1-t)*landabis_old;
landa_old=landa;
landabis_old=landabis;
end
aux=find( (landa+landabis)~=0 );
An= eye(N) + diag(landa+landabis)*H;
Sin = diag(landa+landabis)*y - ...
diag(landa-landabis)*(e*ones(N,1));
w=inv(An)*(Sin);
end
aux=find(abs(w)>1e-7);
nsv=length(aux);
% ==========================================
% ========= Auxiliary functions ============
% ==========================================
function [alfa,alfabis]=misalfas(er,e,C,gam)
N0=length(er);
alfa=zeros(N0,1);
alfabis=zeros(N0,1);
ec=e+gam*C;
S1 = find( (er>=e)&(er<=ec) );
S1b= find( (er<=-e)&(er>=-ec) );
S2 = find( (er>ec) );
S2b= find( (er<-ec) );
alfa(S1) = 1/gam*(er(S1)-e);
alfabis(S1b)= 1/gam*(-er(S1b)-e);
alfa(S2) = C;
alfabis(S2b)= C;
function [landa,landabis]=mislandas(alfa,alfabis,er,e);
N0=length(er);
erb=er;
er=er-e;
erb=-erb-e;
landa=zeros(1,N0);
landabis=zeros(1,N0);
V1=find(abs(alfa-alfabis)<1e-10);
V1=setdiff(1:N0,V1);
landa(V1) = 2*alfa(V1)./er(V1);
landabis(V1) = 2*alfabis(V1)./erb(V1);