-
Notifications
You must be signed in to change notification settings - Fork 2
/
k_pegasos_train.m
121 lines (104 loc) · 3.83 KB
/
k_pegasos_train.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
111
112
113
114
115
116
117
118
119
120
121
function model = k_pegasos_train(X,Y,model)
% K_PEGASOS_TRAIN Kernel Pegasos Algorithm
%
% MODEL = K_PEGASOS_TRAIN(X,Y,MODEL) trains an classifier according to
% Pegasos algorithm, using kernels.
%
% Additional parameters:
% - model.k is the number of samples used to estimate the gradient at
% each step.
% Default value is 1.
% - model.T is the numer of epochs, as a fraction of the number of
% training points.
% Default value is 5.
% - model.lambda is the regularization weight.
% Default value is 1/numel(Y).
%
% Note that the projection step is missing in this implementation.
%
% References:
% - Shalev-Shwartz, S., Singer, Y., & Srebro, N. (2007)
% Pegasos: Primal Estimated sub-GrAdient SOlver for SVM.
% Proceedings of the 24th International Conference on Machine
% Learning.
% - Shalev-Shwartz, S., & Srebro, N. (2008)
% SVM Optimization: Inverse Dependence on Training Set Size.
% Proceedings of the 25th International Conference on Machine
% Learning.
% This file is part of the DOGMA library for MATLAB.
% Copyright (C) 2009-2011, Francesco Orabona
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
% Contact the author: francesco [at] orabona.com
n = length(Y); % number of training samples
if isfield(model,'iter')==0
model.iter=0;
model.beta=spalloc(1,n,n);
model.errTot=0;
model.aer=[];
model.num_ker_eval=0;
model.X=[];
model.Y=[];
model.epoch=0;
end
if isfield(model,'k')==0
model.k=1;
end
if isfield(model,'T')==0
model.T=10;
end
if isfield(model,'lambda')==0
model.lambda=1/numel(Y);
end
for epoch=1:model.T
model.epoch=model.epoch+1;
idx_rand=randperm(n);
for i=1:model.k:n
model.iter=model.iter+1;
idxs_for_subgrad=idx_rand(i:i+model.k-1);
if numel(model.S)>0
if isempty(model.ker)
K_f=X(model.S,idxs_for_subgrad);
else
K_f=feval(model.ker,model.SV,X(:,idxs_for_subgrad),model.kerparam);
end
val_f=model.beta(model.S)*K_f;
else
val_f=zeros(1,model.k);
end
tmpY=Y(idxs_for_subgrad);
model.errTot=model.errTot+sum(sign(val_f)~=tmpY);
model.aer(model.iter)=model.errTot/(model.iter*model.k);
eta=1/(model.lambda*model.iter);
model.beta=model.beta*(1-model.lambda*eta);
idx_to_update=find(val_f.*tmpY<1);
if numel(idx_to_update)>0
model.beta(idxs_for_subgrad(idx_to_update))=...
model.beta(idxs_for_subgrad(idx_to_update))+eta*tmpY(idx_to_update)/model.k;
model.S=find(model.beta);
if ~isempty(model.ker)
model.SV=X(:,model.S);
end
end
if mod(model.iter,model.step)==0
fprintf('#%.0f(epoch %.0f)\tSV:%5.2f(%d)\tAER:%5.2f\n', ...
ceil(model.iter/1000),epoch,numel(model.S)/n*100,numel(model.S),model.aer(end)*100);
end
end
end
model.X=X;
model.S=find(model.beta);
model.SV=X(:,model.S);
model.beta=full(model.beta(model.S));