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