This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
mlr_test.m
283 lines (221 loc) · 7.79 KB
/
mlr_test.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
281
282
283
function Perf = mlr_test(W, test_k, Xtrain, Ytrain, Xtest, Ytest)
% Perf = mlr_test(W, test_k, Xtrain, Ytrain, Xtest, Ytest)
%
% W = d-by-d positive semi-definite matrix
% test_k = vector of k-values to use for KNN/Prec@k/NDCG
% Xtrain = d-by-n matrix of training data
% Ytrain = n-by-1 vector of training labels
% OR
% n-by-2 cell array where
% Y{q,1} contains relevant indices (in 1..n) for point q
% Y{q,2} contains irrelevant indices (in 1..n) for point q
% Xtest = d-by-m matrix of testing data
% Ytest = m-by-1 vector of training labels, or m-by-2 cell array
%
%
% The output structure Perf contains the mean score for:
% AUC, KNN, Prec@k, MAP, MRR, NDCG,
% as well as the effective dimensionality of W, and
% the best-performing k-value for KNN, Prec@k, and NDCG.
%
Perf = struct( ...
'AUC', [], ...
'KNN', [], ...
'PrecAtK', [], ...
'MAP', [], ...
'MRR', [], ...
'NDCG', [], ...
'dimensionality', [], ...
'KNNk', [], ...
'PrecAtKk', [], ...
'NDCGk', [] ...
);
[d, nTrain, nKernel] = size(Xtrain);
% Compute dimensionality of the learned metric
Perf.dimensionality = mlr_test_dimension(W, nTrain, nKernel);
test_k = min(test_k, nTrain);
if nargin > 5
% Knock out the points with no labels
if ~iscell(Ytest)
Ibad = find(isnan(Ytrain));
Xtrain(:,Ibad,:) = inf;
end
% Build the distance matrix
[D, I] = mlr_test_distance(W, Xtrain, Xtest);
else
% Leave-one-out validation
if nargin > 4
% In this case, Xtest is a subset of training indices to test on
testRange = Xtest;
else
testRange = 1:nTrain;
end
Xtest = Xtrain(:,testRange,:);
Ytest = Ytrain(testRange);
% compute self-distance
[D, I] = mlr_test_distance(W, Xtrain, Xtest);
% clear out the self-link (distance = 0)
I = I(2:end,:);
D = D(2:end,:);
end
nTest = length(Ytest);
% Compute label agreement
if ~iscell(Ytest)
% First, knock out the points with no label
Labels = Ytrain(I);
Agree = bsxfun(@eq, Ytest', Labels);
% We only compute KNN error if Y are labels
[Perf.KNN, Perf.KNNk] = mlr_test_knn(Labels, Ytest, test_k);
else
if nargin > 5
Agree = zeros(nTrain, nTest);
else
Agree = zeros(nTrain-1, nTest);
end
for i = 1:nTest
Agree(:,i) = ismember(I(:,i), Ytest{i,1});
end
Agree = reduceAgreement(Agree);
end
% Compute AUC score
Perf.AUC = mlr_test_auc(Agree);
% Compute MAP score
Perf.MAP = mlr_test_map(Agree);
% Compute MRR score
Perf.MRR = mlr_test_mrr(Agree);
% Compute prec@k
[Perf.PrecAtK, Perf.PrecAtKk] = mlr_test_preck(Agree, test_k);
% Compute NDCG score
[Perf.NDCG, Perf.NDCGk] = mlr_test_ndcg(Agree, test_k);
end
function [D,I] = mlr_test_distance(W, Xtrain, Xtest)
% CASES:
% Raw: W = []
% Linear, full: W = d-by-d
% Single Kernel, full: W = n-by-n
% MKL, full: W = n-by-n-by-m
% Linear, diagonal: W = d-by-1
% Single Kernel, diagonal: W = n-by-1
% MKL, diag: W = n-by-m
% MKL, diag-off-diag: W = m-by-m-by-n
[d, nTrain, nKernel] = size(Xtrain);
nTest = size(Xtest, 2);
if isempty(W)
% W = [] => native euclidean distances
D = mlr_test_distance_raw(Xtrain, Xtest);
elseif size(W,1) == d && size(W,2) == d
% We're in a full-projection case
D = setDistanceFullMKL([Xtrain Xtest], W, nTrain + (1:nTest), 1:nTrain);
elseif size(W,1) == d && size(W,2) == nKernel
% We're in a simple diagonal case
D = setDistanceDiagMKL([Xtrain Xtest], W, nTrain + (1:nTest), 1:nTrain);
else
% Error?
error('Cannot determine metric mode.');
end
D = full(D(1:nTrain, nTrain + (1:nTest)));
[v,I] = sort(D, 1);
end
function dimension = mlr_test_dimension(W, nTrain, nKernel)
% CASES:
% Raw: W = []
% Linear, full: W = d-by-d
% Single Kernel, full: W = n-by-n
% MKL, full: W = n-by-n-by-m
% Linear, diagonal: W = d-by-1
% Single Kernel, diagonal: W = n-by-1
% MKL, diag: W = n-by-m
% MKL, diag-off-diag: W = m-by-m-by-n
if size(W,1) == size(W,2)
dim = [];
for i = 1:nKernel
[v,d] = eig(0.5 * (W(:,:,i) + W(:,:,i)'));
dim = [dim ; abs(real(diag(d)))];
end
else
dim = W(:);
end
cd = cumsum(dim) / sum(dim);
dimension = find(cd >= 0.95, 1);
if isempty(dimension)
dimension = 0;
end
end
function [NDCG, NDCGk] = mlr_test_ndcg(Agree, test_k)
nTrain = size(Agree, 1);
Discount = zeros(1, nTrain);
Discount(1:2) = 1;
NDCG = -Inf;
NDCGk = 0;
for k = test_k
Discount(3:k) = 1 ./ log2(3:k);
Discount = Discount / sum(Discount);
b = mean(Discount * Agree);
if b > NDCG
NDCG = b;
NDCGk = k;
end
end
end
function [PrecAtK, PrecAtKk] = mlr_test_preck(Agree, test_k)
PrecAtK = -Inf;
PrecAtKk = 0;
for k = test_k
b = mean( mean( Agree(1:k, :), 1 ) );
if b > PrecAtK
PrecAtK = b;
PrecAtKk = k;
end
end
end
function [KNN, KNNk] = mlr_test_knn(Labels, Ytest, test_k)
KNN = -Inf;
KNNk = 0;
for k = test_k
% FIXME: 2012-02-07 16:51:59 by Brian McFee <[email protected]>
% fix these to discount nans
b = mean( mode( Labels(1:k,:), 1 ) == Ytest');
if b > KNN
KNN = b;
KNNk = k;
end
end
end
function MAP = mlr_test_map(Agree);
nTrain = size(Agree, 1);
MAP = bsxfun(@ldivide, (1:nTrain)', cumsum(Agree, 1));
MAP = mean(sum(MAP .* Agree, 1)./ sum(Agree, 1));
end
function MRR = mlr_test_mrr(Agree);
nTest = size(Agree, 2);
MRR = 0;
for i = 1:nTest
MRR = MRR + (1 / find(Agree(:,i), 1));
end
MRR = MRR / nTest;
end
function AUC = mlr_test_auc(Agree)
TPR = cumsum(Agree, 1);
FPR = cumsum(~Agree, 1);
numPos = TPR(end,:);
numNeg = FPR(end,:);
TPR = mean(bsxfun(@rdivide, TPR, numPos),2);
FPR = mean(bsxfun(@rdivide, FPR, numNeg),2);
AUC = diff([0 FPR']) * TPR;
end
function D = mlr_test_distance_raw(Xtrain, Xtest)
[d, nTrain, nKernel] = size(Xtrain);
nTest = size(Xtest, 2);
% Not in kernel mode, compute distances directly
D = 0;
for i = 1:nKernel
D = D + setDistanceDiag([Xtrain(:,:,i) Xtest(:,:,i)], ones(d,1), ...
nTrain + (1:nTest), 1:nTrain);
end
end
function A = reduceAgreement(Agree)
nPos = sum(Agree,1);
nNeg = sum(~Agree,1);
goodI = find(nPos > 0 & nNeg > 0);
A = Agree(:,goodI);
end