-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdsl_triplet_away.m
executable file
·280 lines (241 loc) · 8.86 KB
/
hdsl_triplet_away.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
% Copyright 2015 Kuan Liu & Aurelien Bellet
%
% This file is part of HDSL.
%
% HDSL 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.
%
% HDSL 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 HDSL. If not, see <http://www.gnu.org/licenses/>.
function [M, Stats] = hdsl_triplet_away(data, label, Cons, scale, nbIter, dataVa, labelVa, dataTe, labelTe)
% hyper-parameters
debug = 0;
MINIMUM_ITER = 20;
%
n_Cons = zeros(nbIter,1);
n_act_feat = zeros(nbIter,1);
n_relate =zeros(nbIter,1);
[~,d] = size(data);
nbCons = size(Cons,2);
Cons = uint64(Cons);
% for speed up: do not involve all 0 features
u = max(data,[],1);
nzf = find(u~=0);
length(nzf);
% allocate enough space in sparse matrix, maintain the coeficient of selected bases
M = spalloc(d,d,nbIter*4);
cons_related = spalloc(size(Cons,2),nbIter,nbIter * d);
n_feat = 0;
feat = zeros(2, nbIter);
feat_sign = zeros(nbIter,1);
coef_feat = zeros(nbIter,1);
% initialize with random basis
f = randsample(d,2);
if (f(1) > f(2))
tt = f(1);
f(1) = f(2);
f(2) = tt;
end
direction = 1;
alpha = 1;
signF = randsample([-1 1],1);
M(f,f) = scale;
M(f(1),f(2)) = M(f(1),f(2)) * signF;
M(f(2),f(1)) = M(f(2),f(1)) * signF;
% value of Frobenius inner product between each constraint and the current metric matrix
AtM = zeros(nbCons,1);
[AtM, violation, loss] = update_AtM(AtM,Cons,data(:,f),signF,alpha,scale);
[~, ~, ~, ~, relate] = line_search(AtM,Cons,data(:,f),signF,scale);
n_relate(n_feat+1) = sum(relate);
cons_related(:,n_feat+1) = sparse(relate);
% for evaluations
err3nn = []; % 1: train 2: train balanced 3: valid 4; valid balanced
err1nn = [];
act_fea = [];
obj = [];
direction_record = [];
picked_feat = [];
picked_step = [];
best_err1 = inf;
best_err3 = inf;
update_type = zeros(nbIter,1); % 0: add new 1: add old 2: away
update_type(1) = 0;
features = containers.Map; % maintain whether one basis has been selected.
idxF = []; % selected basis in away step
hit = 0; % successfully picking one pair of feature
delete = 0; % delete one useless basis
% main loop
for i=2:nbIter+1
% update coefficients
[feat, coef_feat, feat_sign, n_feat, features] = update_feat...
(direction, alpha, f, signF, n_feat, coef_feat, feat, ...
feat_sign, features, idxF);
% output
if mod(i-1,50) == 0 || i == 2
fprintf('iter %i obj:%f, #0-loss: %d, #correct: %d, #active features: %i\n',i-1,mean(loss),sum(loss==0),sum(violation<1),full(sum(sum(M~=0,2)~=0)));
[predTr1, predVa1] = knn_classify_bilin(data,label,dataVa,M,1);
[e1] = print_error(predTr1, label, predVa1, labelVa, '1-NN');
[predTr3, predVa3] = knn_classify_bilin(data,label,dataVa,M,3);
[e3] = print_error(predTr3, label, predVa3, labelVa, '3-NN');
err1nn = [err1nn;e1];
err3nn = [err3nn;e3];
if e1(3) < best_err1
best_err1 = e1(3);
best_M1 = M;
end
if e3(3) < best_err3
best_err3 = e3(3);
best_M3 = M;
end
af = full(sum(sum(M~=0,2)~=0));
act_fea = [act_fea; af];
obj = [obj; mean(loss)];
end
% converge test: if objective does not change or bases does not change.
if (length(obj) > MINIMUM_ITER && obj(end) == obj(end-MINIMUM_ITER) && act_fea(end) == act_fea(end-MINIMUM_ITER))
fprintf('Converge \n');
break;
end
% search for constraints with non zero loss
idxL = find(loss ~= 0);
constraint = cumsum(loss ~= 0);
% constraint(loss == 0) = 0;
constraint = int32(constraint);
if length(idxL) < 1
display('could not find one instance');
break;
end
n_Cons(i) = length(idxL);
A = data(Cons(1,idxL),:)';
B = (data(Cons(2,idxL),:)-data(Cons(3,idxL),:))';
AtMtmp = AtM(idxL);
while (hit~=1)
% search for basis via fast heuristic: run find_feat_all twice
ii = randsample(nzf,1);
[f, ~, ~, ~, ~] = find_feat_all(A,B,d,AtMtmp,numel(idxL),ii);
if f(1) == ii
jj = f(2);
else
jj = f(1);
end
[f, signF, value, ~, ~] = find_feat_all(A,B,d,AtMtmp,numel(idxL),jj);
% search away step basis: among all selected basis, take one away
% or forward step
[idx_af,~] = find(coef_feat(1:n_feat)~=0); % active features
n_act_feat(i) = length(idx_af);
[f_a, signF_a, value_a, Mgrad, idxF, ~] = away(A,B,cons_related(:,idx_af),feat(:,idx_af),feat_sign(idx_af),AtM,coef_feat(idx_af),constraint,debug);
if idxF ~= 0
idxF = idx_af(idxF);
end
% compare the three and decide
if value_a(2) > value
value_f = value_a(2);
f = f_a(3:4);
signF = signF_a(2);
update_type(i) = 1;
else
value_f = value;
end
direction = 1;
if (value_f + value_a(1) - 2 * Mgrad < 0)
direction = -1; % away
f = f_a(1:2);
signF = signF_a(1);
update_type(i) = 2;
end
direction_record = [direction_record; direction];
if (value ~= -1)
hit = 1;
end
end
hit = 0;
% do line-search and update AtM, violation and loss
% update matrix M
if direction > 0
[AtM, alpha, violation, loss, relate] = line_search(AtM,Cons,data(:,f),signF,scale);
if alpha == 0
else
n_relate(n_feat+1) = sum(relate);
cons_related(:,n_feat+1) = sparse(relate);
end
elseif direction == -1;
[AtM, alpha, violation, loss] = line_search_away(AtM,Cons,data(:,f),signF,scale, coef_feat(idxF),debug);
if alpha == coef_feat(idxF) / (1 - coef_feat(idxF))
delete = delete + 1;
end
end
picked_feat = [picked_feat; reshape(f,1,2)];
picked_step = [picked_step; alpha];
M = (1 - alpha * direction) * M;
M(f(1),f(1)) = M(f(1),f(1)) + scale * alpha * direction ;
M(f(2),f(2)) = M(f(2),f(2)) + scale * alpha * direction;
M(f(1),f(2)) = M(f(1),f(2)) + scale * alpha * direction * signF;
M(f(2),f(1)) = M(f(2),f(1)) + scale * alpha * direction * signF;
end
fprintf('\n----------------------------\nResults with HDSL similarity\n----------------------------\n');
fprintf('%i obj:%f, #0-loss: %d, #correct: %d, #active features: %i\n',i-1,mean(loss),sum(loss==0),sum(violation<1),full(sum(sum(M~=0,2)~=0)));
[predTr1, predVa1] = knn_classify_bilin(data,label,dataVa,M,1);
print_error(predTr1, label, predVa1, labelVa, '1-NN');
[predTr3, predVa3] = knn_classify_bilin(data,label,dataVa,M,3);
print_error(predTr3, label, predVa3, labelVa, '3-NN');
% Save results
Stats.err1 = err1nn;
Stats.err3 = err3nn;
Stats.af = act_fea;
Stats.obj = obj;
Stats.best_M1 = best_M1;
Stats.best_M3 = best_M3;
Stats.best_e3 = best_err3;
Stats.best_e1 = best_err1;
Stats.feat = feat;
Stats.n_feat = n_feat;
Stats.feat_sign = feat_sign;
Stats.coef_feat = coef_feat;
Stats.dir = direction_record;
Stats.delete = delete;
Stats.update_type = update_type;
Stats.no_cons_related = n_relate;
Stats.picked_feat = picked_feat;
Stats.picked_step = picked_step;
end
% update selected bases and maintain their coefficients
function [feat, coef_feat, feat_sign, n_feat, features] = update_feat...
(direction, alpha, f, signF, n_feat, coef_feat, feat, ...
feat_sign, features, idxF)
if alpha == 0
else
%write a function
if f(1) > f(2)
k1 = num2str(f(2));
k2 = num2str(f(1));
else
k2 = num2str(f(2));
k1 = num2str(f(1));
end
key_f = [k1 ',' k2 ',' num2str(signF)];
if direction == 1
if features.isKey(key_f)
coef_feat(1:n_feat) = coef_feat(1:n_feat) * (1-alpha);
idx = features(key_f);
coef_feat(idx) = coef_feat(idx) + alpha;
else
coef_feat(1:n_feat) = coef_feat(1:n_feat) * (1-alpha);
n_feat = n_feat+1;
features(key_f) = n_feat;
coef_feat(n_feat) = alpha;
feat(:,n_feat) = f';
feat_sign(n_feat) = signF;
end
else
coef_feat(1:n_feat) = coef_feat(1:n_feat) * (1+alpha);
coef_feat(idxF) = coef_feat(idxF) - alpha;
end
end
end