wolffd@0
|
1 function mlr_demo()
|
wolffd@0
|
2
|
wolffd@0
|
3 display('Loading Wine data');
|
wolffd@0
|
4 load Wine;
|
wolffd@0
|
5
|
wolffd@0
|
6 % z-score the input dimensions
|
wolffd@0
|
7 display('z-scoring features');
|
wolffd@0
|
8 X = zscore(X')';
|
wolffd@0
|
9
|
wolffd@0
|
10 [d,n] = size(X);
|
wolffd@0
|
11
|
wolffd@0
|
12 % Generate a random training/test split
|
wolffd@0
|
13 display('Generating a 80/20 training/test split');
|
wolffd@0
|
14 P = randperm(n);
|
wolffd@0
|
15 Xtrain = X(:,P(1:floor(0.8 * n)));
|
wolffd@0
|
16 Ytrain = Y(P(1:floor(0.8*n)));
|
wolffd@0
|
17 Xtest = X(:,P((1+floor(0.8*n)):end));
|
wolffd@0
|
18 Ytest = Y(P((1+floor(0.8*n)):end));
|
wolffd@0
|
19
|
wolffd@0
|
20
|
wolffd@0
|
21 % Optimize W for AUC
|
wolffd@0
|
22 C = 1e-2;
|
wolffd@0
|
23 display(sprintf('Training with C=%.2e, Delta=mAP', C));
|
wolffd@0
|
24 [W, Xi, Diagnostics] = mlr_train(Xtrain, Ytrain, C, 'map');
|
wolffd@0
|
25 % [W, Xi, Diagnostics] = mlr_train_primal(Xtrain, Ytrain, C, 'map');
|
wolffd@0
|
26
|
wolffd@0
|
27 display('Test performance in the native (normalized) metric');
|
wolffd@0
|
28 mlr_test(eye(d), 3, Xtrain, Ytrain, Xtest, Ytest)
|
wolffd@0
|
29
|
wolffd@0
|
30 display('Test performance with MLR metric');
|
wolffd@0
|
31 mlr_test(W, 3, Xtrain, Ytrain, Xtest, Ytest)
|
wolffd@0
|
32
|
wolffd@0
|
33 % Scatter-plot
|
wolffd@0
|
34 figure;
|
wolffd@0
|
35 subplot(1,2,1), drawData(eye(d), Xtrain, Ytrain, Xtest, Ytest), title('Native metric (z-scored)');
|
wolffd@0
|
36 subplot(1,2,2), drawData(W, Xtrain, Ytrain, Xtest, Ytest), title('Learned metric (MLR-mAP)');
|
wolffd@0
|
37
|
wolffd@0
|
38 Diagnostics
|
wolffd@0
|
39
|
wolffd@0
|
40 end
|
wolffd@0
|
41
|
wolffd@0
|
42
|
wolffd@0
|
43 function drawData(W, Xtrain, Ytrain, Xtest, Ytest);
|
wolffd@0
|
44
|
wolffd@0
|
45 n = length(Ytrain);
|
wolffd@0
|
46 m = length(Ytest);
|
wolffd@0
|
47
|
wolffd@0
|
48 if size(W,2) == 1
|
wolffd@0
|
49 W = diag(W);
|
wolffd@0
|
50 end
|
wolffd@0
|
51 % PCA the learned metric
|
wolffd@0
|
52 Z = [Xtrain Xtest];
|
wolffd@0
|
53 A = Z' * W * Z;
|
wolffd@0
|
54 [v,d] = eig(A);
|
wolffd@0
|
55
|
wolffd@0
|
56 L = (d.^0.5) * v';
|
wolffd@0
|
57 L = L(1:2,:);
|
wolffd@0
|
58
|
wolffd@0
|
59 % Draw training points
|
wolffd@0
|
60 hold on;
|
wolffd@0
|
61 trmarkers = {'b+', 'r+', 'g+'};
|
wolffd@0
|
62 tsmarkers = {'bo', 'ro', 'go'};
|
wolffd@0
|
63 for i = min(Ytrain):max(Ytrain)
|
wolffd@0
|
64 points = find(Ytrain == i);
|
wolffd@0
|
65 scatter(L(1,points), L(2,points), trmarkers{i});
|
wolffd@0
|
66 points = n + find(Ytest == i);
|
wolffd@0
|
67 scatter(L(1,points), L(2,points), tsmarkers{i});
|
wolffd@0
|
68 end
|
wolffd@0
|
69 legend({'Training', 'Test'});
|
wolffd@0
|
70 end
|