-
Notifications
You must be signed in to change notification settings - Fork 2
/
k_om2_mp_multi_train.m
177 lines (147 loc) · 5.81 KB
/
k_om2_mp_multi_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function model = k_om2_mp_multi_train(K, Y, model)
% K_OM2_MP_MULTI_TRAIN OM-2 Algorithm (Multiple Passes)
%
% MODEL = K_OM2_MULTI_TRAIN(K,Y,MODEL) trains an p-norm MKL classifier
% by cyclying on the same training set multiple times using a fast
% online method.
%
% Inputs:
% K - 3-D N*N*F Kernel Matrices, each kernel K(:, :, i) is a N*N matrix
% Y - Training label, 1*N Vector
%
% Additional parameters:
% - model.p is 'p' of the p-norm used in the regularization
% Default value is 1/(1-1/(2*log(numbers_of_cue))).
% - model.T is maximum numer of training epochs. It will stop earlier if
% it converges.
% Default value is 5.
%
% References:
% - Jie, L., Orabona, F., Fornoni, M., Caputo, B., and Cesa-Bianchi, N. (2010).
% "OM-2: An Online Mutli-class Multi-kernel Learning Algorithm".
% Proceedings of the 23rd IEEE Conference on Computer Vision and
% Pattern Recognition - Workshops.
% 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 authors: jluo [at] idiap.ch
% francesco [at] orabona.com
timerstart = cputime;
n = length(Y); % number of training samples
n_kernel = size(K,3); % number of kernels
if isfield(model,'stopCondition')==0
model.stopCondition = 0; % #. of update threshold
end
if isfield(model,'step')==0
model.step = 100*numel(Y);
end
if isfield(model,'n_cla')==0
model.n_cla = max(Y); % number of classes
end
if isfield(model,'iter')==0
model.iter = 0;
model.beta = spalloc(model.n_cla, n, n*model.n_cla);
model.errTot = 0;
model.lossTot = 0;
model.S = [];
model.epoch = 0;
model.time = []; % training time on each step
model.test = []; % iteration when testing happens
model.weights = zeros(n_kernel,1);
end
if isfield(model,'p')==0
model.q = 2*log(n_kernel);
model.p = 1/(1-1/model.q);
else
model.q = 1/(1-1/model.p);
end
if isfield(model,'T')==0
model.T = 5; % maximum number of iterations
end
if isfield(model, 'L1')==0
model.L1 = cell(n_kernel, 1);
end
preds = zeros(model.n_cla, n_kernel);
isSV = zeros(1,n);
sqnorms = zeros(n_kernel, 1)+eps;
val_f = zeros(model.n_cla, 1);
for epoch=1:model.T
model.epoch = model.epoch+1;
idx_rand = randperm(n);
n_update=0;
for i=1:n
model.iter = model.iter+1;
idxs_subgrad = idx_rand(i);
if numel(model.S)>0
K_f = double(K(:, idxs_subgrad, :));
preds = model.beta*K_f;
val_f = preds*model.weights;
end
yi = Y(idxs_subgrad);
margin_true = val_f(yi);
val_f(yi) = -Inf;
[margin_pred, yhat] = max(val_f);
model.errTot = model.errTot+(margin_true<=margin_pred);
model.lossTot = model.lossTot+max(1-margin_true+margin_pred,0);
% update
if margin_true<=margin_pred+1
eta = min(1, 1-2*(margin_true-margin_pred)/(2*n_kernel^(2/model.q)));
model.beta(yi,idxs_subgrad) = model.beta(yi,idxs_subgrad)+eta;
model.beta(yhat,idxs_subgrad) = model.beta(yhat,idxs_subgrad)-eta;
Kii = double(K(idxs_subgrad,idxs_subgrad, :));
sqnorms = sqnorms+2*eta*(preds(yi, :)-preds(yhat, :))'+(2*eta^2*Kii(:));
isSV(idxs_subgrad) = any(model.beta(:, idxs_subgrad));
model.S = find(isSV);
n_update = n_update+1;
norms = sqrt(sqnorms);
norm_theta = norm(norms+eps,model.q);
model.weights = (norms/norm_theta).^(model.q-2)/model.q;
end
if mod(model.iter,model.step)==0
model.test(end+1) = model.iter;
model.time(end+1) = cputime-timerstart;
if isfield(model,'eachRound')~=0
if isfield(model, 'outputER')==0
model.outputER = [];
model.outputER = feval(model.eachRound, model);
else
model.outputER(end+1) = feval(model.eachRound, model);
end
end
timerstart = cputime;
end
end
fprintf('#%.0f(epoch %.0f)\tSV:%5.2f(%d)\tAER:%5.2f\tAEL:%5.2f\tUpdates:%5.2f\n', ...
ceil(model.iter/1000), epoch, numel(model.S)/n*100, numel(model.S), ...
model.errTot/model.iter*100, model.lossTot/model.iter, n_update);
if n_update<=model.stopCondition || epoch==model.T
model.test(end+1) = model.iter;
model.time(end+1) = cputime-timerstart;
if isfield(model,'eachRound')~=0
if isfield(model, 'outputER')==0
model.outputER = [];
model.outputER = feval(model.eachRound, model);
else
model.outputER(end+1) = feval(model.eachRound, model);
end
end
break
end
end
for i=1:numel(model.L1)
model.L1{i}.S=model.S;
model.L1{i}.beta=model.beta(:,model.S);
end