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

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