annotate toolboxes/distance_learning/mlr/cuttingPlane/cuttingPlaneRandom.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] = cuttingPlaneRandom(k, X, W, Ypos, Yneg, batchSize, SAMPLES, ClassScores)
wolffd@0 2 %
wolffd@0 3 % [dPsi, M, SO_time] = cuttingPlaneRandom(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 SETDISTANCE CPGRADIENT;
wolffd@0 19
wolffd@0 20 [d,n] = size(X);
wolffd@0 21
wolffd@0 22
wolffd@0 23 if length(SAMPLES) == n
wolffd@0 24 % All samples are fair game (full data)
wolffd@0 25 Batch = randperm(n);
wolffd@0 26 Batch = Batch(1:batchSize);
wolffd@0 27 D = SETDISTANCE(X, W, Batch);
wolffd@0 28
wolffd@0 29 else
wolffd@0 30 Batch = randperm(length(SAMPLES));
wolffd@0 31 Batch = SAMPLES(Batch(1:batchSize));
wolffd@0 32
wolffd@0 33 Ito = sparse(n,1);
wolffd@0 34
wolffd@0 35 if isempty(ClassScores)
wolffd@0 36 for i = Batch
wolffd@0 37 Ito(Ypos{i}) = 1;
wolffd@0 38 Ito(Yneg{i}) = 1;
wolffd@0 39 end
wolffd@0 40 D = SETDISTANCE(X, W, Batch, find(Ito));
wolffd@0 41 else
wolffd@0 42 D = SETDISTANCE(X, W, Batch, 1:n);
wolffd@0 43 end
wolffd@0 44 end
wolffd@0 45
wolffd@0 46
wolffd@0 47 M = 0;
wolffd@0 48 S = zeros(n);
wolffd@0 49 dIndex = sub2ind([n n], 1:n, 1:n);
wolffd@0 50
wolffd@0 51 SO_time = 0;
wolffd@0 52
wolffd@0 53
wolffd@0 54 if isempty(ClassScores)
wolffd@0 55 TS = zeros(batchSize, n);
wolffd@0 56 parfor j = 1:batchSize
wolffd@0 57 i = Batch(j);
wolffd@0 58 if isempty(Yneg)
wolffd@0 59 Ynegative = setdiff((1:n)', [i ; Ypos{i}]);
wolffd@0 60 else
wolffd@0 61 Ynegative = Yneg{i};
wolffd@0 62 end
wolffd@0 63 SO_start = tic();
wolffd@0 64 [yi, li] = SO(i, D, Ypos{i}, Ynegative, k);
wolffd@0 65 SO_time = SO_time + toc(SO_start);
wolffd@0 66
wolffd@0 67 M = M + li /batchSize;
wolffd@0 68 TS(j,:) = PSI(i, yi', n, Ypos{i}, Ynegative);
wolffd@0 69 end
wolffd@0 70 S(Batch,:) = TS;
wolffd@0 71 S(:,Batch) = S(:,Batch) + TS';
wolffd@0 72 S(dIndex) = S(dIndex) - sum(TS, 1);
wolffd@0 73 else
wolffd@0 74 for j = 1:length(ClassScores.classes)
wolffd@0 75 c = ClassScores.classes(j);
wolffd@0 76 points = find(ClassScores.Y(Batch) == c);
wolffd@0 77 if ~any(points)
wolffd@0 78 continue;
wolffd@0 79 end
wolffd@0 80
wolffd@0 81 Yneg = find(ClassScores.Yneg{j});
wolffd@0 82 yp = ClassScores.Ypos{j};
wolffd@0 83
wolffd@0 84 TS = zeros(length(points), n);
wolffd@0 85 parfor x = 1:length(points)
wolffd@0 86 i = Batch(points(x));
wolffd@0 87 yl = yp;
wolffd@0 88 yl(i) = 0;
wolffd@0 89 Ypos = find(yl);
wolffd@0 90 SO_start = tic();
wolffd@0 91 [yi, li] = SO(i, D, Ypos, Yneg, k);
wolffd@0 92 SO_time = SO_time + toc(SO_start);
wolffd@0 93
wolffd@0 94 M = M + li /batchSize;
wolffd@0 95 TS(x,:) = PSI(i, yi', n, Ypos, Yneg);
wolffd@0 96 end
wolffd@0 97 S(Batch(points),:) = S(Batch(points),:) + TS;
wolffd@0 98 S(:,Batch(points)) = S(:,Batch(points)) + TS';
wolffd@0 99 S(dIndex) = S(dIndex) - sum(TS, 1);
wolffd@0 100 end
wolffd@0 101 end
wolffd@0 102
wolffd@0 103 dPsi = CPGRADIENT(X, S, batchSize);
wolffd@0 104
wolffd@0 105 end