annotate toolboxes/distance_learning/mlr/cuttingPlane/cuttingPlaneFull.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [dPsi, M, SO_time] = cuttingPlaneFull(k, X, W, Ypos, Yneg, batchSize, SAMPLES, ClassScores)
wolffd@0 2 %
wolffd@0 3 % [dPsi, M, SO_time] = cuttingPlaneFull(k, X, W, Yp, Yn, batchSize, SAMPLES, ClassScores)
wolffd@0 4 %
wolffd@0 5 % k = k parameter for the SO
wolffd@0 6 % X = d*n data matrix
wolffd@0 7 % W = d*d PSD metric
wolffd@0 8 % Yp = cell-array of relevant results for each point
wolffd@0 9 % Yn = cell-array of irrelevant results for each point
wolffd@0 10 % batchSize = number of points to use in the constraint batch
wolffd@0 11 % SAMPLES = indices of valid points to include in the batch
wolffd@0 12 % ClassScores = structure for synthetic constraints
wolffd@0 13 %
wolffd@0 14 % dPsi = dPsi vector for this batch
wolffd@0 15 % M = mean loss on this batch
wolffd@0 16 % SO_time = time spent in separation oracle
wolffd@0 17
wolffd@0 18 global SO PSI DISTANCE CPGRADIENT;
wolffd@0 19
wolffd@0 20 [d,n,m] = size(X);
wolffd@0 21 D = DISTANCE(W, X);
wolffd@0 22
wolffd@0 23 M = 0;
wolffd@0 24 S = zeros(n);
wolffd@0 25 dIndex = sub2ind([n n], 1:n, 1:n);
wolffd@0 26
wolffd@0 27 SO_time = 0;
wolffd@0 28
wolffd@0 29 if isempty(ClassScores)
wolffd@0 30 TS = zeros(batchSize, n);
wolffd@0 31 parfor i = 1:batchSize
wolffd@0 32 if i <= length(SAMPLES)
wolffd@0 33 j = SAMPLES(i);
wolffd@0 34
wolffd@0 35 if isempty(Ypos{j})
wolffd@0 36 continue;
wolffd@0 37 end
wolffd@0 38 if isempty(Yneg)
wolffd@0 39 % Construct a negative set
wolffd@0 40 Ynegative = setdiff((1:n)', [j ; Ypos{j}]);
wolffd@0 41 else
wolffd@0 42 Ynegative = Yneg{j};
wolffd@0 43 end
wolffd@0 44 SO_start = tic();
wolffd@0 45 [yi, li] = SO(j, D, Ypos{j}, Ynegative, k);
wolffd@0 46 SO_time = SO_time + toc(SO_start);
wolffd@0 47
wolffd@0 48 M = M + li /batchSize;
wolffd@0 49 TS(i,:) = PSI(j, yi', n, Ypos{j}, Ynegative);
wolffd@0 50 end
wolffd@0 51 end
wolffd@0 52
wolffd@0 53 % Reconstruct the S matrix from TS
wolffd@0 54 S(SAMPLES,:) = TS;
wolffd@0 55 S(:,SAMPLES) = S(:,SAMPLES) + TS';
wolffd@0 56 S(dIndex) = S(dIndex) - sum(TS, 1);
wolffd@0 57 else
wolffd@0 58
wolffd@0 59 % Do it class-wise for efficiency
wolffd@0 60 batchSize = 0;
wolffd@0 61 for j = 1:length(ClassScores.classes)
wolffd@0 62 c = ClassScores.classes(j);
wolffd@0 63 points = find(ClassScores.Y == c);
wolffd@0 64
wolffd@0 65 Yneg = find(ClassScores.Yneg{j});
wolffd@0 66 yp = ClassScores.Ypos{j};
wolffd@0 67
wolffd@0 68 if length(points) <= 1
wolffd@0 69 continue;
wolffd@0 70 end
wolffd@0 71
wolffd@0 72 batchSize = batchSize + length(points);
wolffd@0 73 TS = zeros(length(points), n);
wolffd@0 74 parfor x = 1:length(points)
wolffd@0 75 i = points(x);
wolffd@0 76 yl = yp;
wolffd@0 77 yl(i) = 0;
wolffd@0 78 Ypos = find(yl);
wolffd@0 79 SO_start = tic();
wolffd@0 80 [yi, li] = SO(i, D, Ypos, Yneg, k);
wolffd@0 81 SO_time = SO_time + toc(SO_start);
wolffd@0 82
wolffd@0 83 M = M + li;
wolffd@0 84 TS(x,:) = PSI(i, yi', n, Ypos, Yneg);
wolffd@0 85 end
wolffd@0 86
wolffd@0 87 S(points,:) = S(points,:) + TS;
wolffd@0 88 S(:,points) = S(:,points) + TS';
wolffd@0 89 S(dIndex) = S(dIndex) - sum(TS, 1);
wolffd@0 90 end
wolffd@0 91 M = M / batchSize;
wolffd@0 92 end
wolffd@0 93
wolffd@0 94 dPsi = CPGRADIENT(X, S, batchSize);
wolffd@0 95
wolffd@0 96 end