-
Notifications
You must be signed in to change notification settings - Fork 3
/
mlr_test_largescale.m
291 lines (231 loc) · 7.72 KB
/
mlr_test_largescale.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
284
285
286
287
288
289
290
291
function Perf = mlr_test_largescale(L, test_k, Xtrain, Ytrain, Xtest_all, Ytest_all)
% Original code by Brian McFee (brian.mcfee@nyu.edu)
% Modified by Daryl Lim (dklim@ucsd.edu)
% Three major changes from mlr_test:
%
% 1) Distance matrix is computed in batches to conserve memory for large data
% 2) It supports only input given in factored form (i.e L s.t. W = L'L)
% 3) Fixed MAP/NDCG so it returns 0 instead of NaN if there are no relevant
% points for a given query point.
%
% L = m x d matrix such that L'L is the metric of interest
% 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-1 cell array where
% Y{q,1} contains relevant indices (in 1..n) for point q
% 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 = Test data in training format
% Ytest = similar format as Ytrain for test data.
%
% The output structure Perf contains the mean score for:
% AUC, KNN, Prec@k, Prec@1, Prec@10, MAP, MRR, NDCG
%
Perf = struct( ...
'AUC', [], ...
'KNN', [], ...
'PrecAtK', [], ...
'PrecAt1', [], ...
'PrecAt10', [], ...
'MAP', [], ...
'MRR', [], ...
'MFR', [], ...
'NDCG', [], ...
'KNNk', [], ...
'PrecAtKk', [], ...
'NDCGk', [] ...
);
[d, nTrain, nKernel] = size(Xtrain);
test_k = min(test_k, nTrain);
batchsize = 4000;
nTest_all = length(Ytest_all);
startIndices = 1:batchsize:nTest_all;
numBatches = length(startIndices);
batchWeights = zeros(numBatches,1);
batchWeights(1:numBatches-1) = batchsize;
batchWeights(end) = nTest_all - startIndices(end)+1;
PerfArray = repmat(Perf,numBatches,1);
for batch = 1:numBatches
startIndex = startIndices(batch);
endIndex = min(nTest_all,startIndex+batchsize-1);
tic
disp(sprintf('Processing indices %d to %d', startIndex, endIndex));
Xtest = Xtest_all(:,startIndex:endIndex);
Ytest = Ytest_all(startIndex:endIndex);
if nargin > 5
% Knock out the points with no labels
if ~iscell(Ytest)
Ibad = find(isnan(Ytrain));
Xtrain(:,Ibad,:) = inf;
end
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);
end
% Build the distance matrix
[D, I] = mlr_test_distance(L, Xtrain, Xtest);
if nargin == 5
% 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
[PerfArray(batch).KNN, PerfArray(batch).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
PerfArray(batch).AUC = mlr_test_auc(Agree);
% Compute MAP score
PerfArray(batch).MAP = mlr_test_map(Agree);
% Compute MRR score
PerfArray(batch).MRR = mlr_test_mrr(Agree);
PerfArray(batch).MFR = mlr_test_mfr(Agree);
% Compute prec@k
[PerfArray(batch).PrecAtK, PerfArray(batch).PrecAtKk] = mlr_test_preck(Agree, test_k);
[PerfArray(batch).PrecAt1, ~] = mlr_test_preck(Agree, 1);
[PerfArray(batch).PrecAt10, ~] = mlr_test_preck(Agree, 10);
% Compute NDCG score
[PerfArray(batch).NDCG, PerfArray(batch).NDCGk] = mlr_test_ndcg(Agree, test_k);
toc
end
SNames = fieldnames(Perf);
for loopIndex = 1:numel(SNames)
if ~isempty([PerfArray.(SNames{loopIndex})])
Perf.(SNames{loopIndex}) = ([PerfArray.(SNames{loopIndex})]*batchWeights)/sum(batchWeights);
end
end
end
function [D,I] = mlr_test_distance(L, Xtrain, Xtest)
% CASES:
% Raw: L = []
% Low rank L = d-by-m
% Linear, diagonal: L = d-by-1
[d, nTrain, nKernel] = size(Xtrain);
nTest = size(Xtest, 2);
if isempty(L)
% L = [] => native euclidean distances
D = (bsxfun(@plus, dot(Xtest,Xtest)', dot(Xtrain,Xtrain)) - 2*Xtest'*Xtrain)';
elseif size(L,2) == d && size(L,1) < d
% Low rank L!
Xtrt = L * Xtrain;
Xtet = L * Xtest;
D = (bsxfun(@plus, dot(Xtet,Xtet)', dot(Xtrt,Xtrt)) - 2 * Xtet'*Xtrt)';
elseif size(L,1) == d && size(L,2) == 1
%diagonal
Xtrt = bsxfun(@times,Xtrain,L);
Xtet = bsxfun(@times,Xtest,L);
D = (bsxfun(@plus, dot(Xtrain,Xtrt)', dot(Xtest,Xtet)) - 2 * Xtrain'*Xtet);
elseif size(L,1) == d && size(L,2) == d
disp('treating matrix as factored form L')
Xtrt = L * Xtrain;
Xtet = L * Xtest;
D = (bsxfun(@plus, dot(Xtet,Xtet)', dot(Xtrt,Xtrt)) - 2 * Xtet'*Xtrt)';
else
% Error?
error('Cannot determine metric mode.');
end
[v,I] = sort(D, 1);
end
function [ndcg, ndcgk] = mlr_test_ndcg(Agree_tags,test_k);
trunc = 1;
nTrain = size(Agree_tags,1);
topk_scores = Agree_tags(1:test_k,:);
disc = 1./[log2(2:nTrain+1)];
if trunc == 1
sort_norel = sort(Agree_tags,1, 'descend');
else
sort_norel = sort(topk_scores,1, 'descend');
end
dcg = disc(1:test_k)*topk_scores;
nor = disc(1:test_k)*sort_norel(1:test_k,:);
dcgall = disc * Agree_tags;
norall = disc * sort_norel;
ndcgk = mean(dcg./(nor+eps));
ndcg = mean(dcgall./(norall+eps));
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 <bmcfee@cs.ucsd.edu>
% 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)+eps));
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 MFR = mlr_test_mfr(Agree);
nTest = size(Agree, 2);
MFR = 0;
for i = 1:nTest
MFR = MFR + (find(Agree(:,i), 1));
end
MFR = MFR / 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+eps),2);
FPR = mean(bsxfun(@rdivide, FPR, numNeg+eps),2);
AUC = diff([0 FPR']) * TPR;
end
function A = reduceAgreement(Agree)
nPos = sum(Agree,1);
nNeg = sum(~Agree,1);
goodI = find(nPos > 0 & nNeg > 0);
A = Agree(:,goodI);
end