wolffd@0: function [dPsi, M, SO_time] = cuttingPlaneRandom(k, X, W, Ypos, Yneg, batchSize, SAMPLES, ClassScores) wolffd@0: % wolffd@0: % [dPsi, M, SO_time] = cuttingPlaneRandom(k, X, W, Yp, Yn, batchSize, SAMPLES, ClassScores) wolffd@0: % wolffd@0: % k = k parameter for the SO wolffd@0: % X = d*n data matrix wolffd@0: % W = d*d PSD metric wolffd@0: % Yp = cell-array of relevant results for each point wolffd@0: % Yn = cell-array of irrelevant results for each point wolffd@0: % batchSize = number of points to use in the constraint batch wolffd@0: % SAMPLES = indices of valid points to include in the batch wolffd@0: % ClassScores = structure for synthetic constraints wolffd@0: % wolffd@0: % dPsi = dPsi vector for this batch wolffd@0: % M = mean loss on this batch wolffd@0: % SO_time = time spent in separation oracle wolffd@0: wolffd@0: global SO PSI SETDISTANCE CPGRADIENT; wolffd@0: wolffd@0: [d,n] = size(X); wolffd@0: wolffd@0: wolffd@0: if length(SAMPLES) == n wolffd@0: % All samples are fair game (full data) wolffd@0: Batch = randperm(n); wolffd@0: Batch = Batch(1:batchSize); wolffd@0: D = SETDISTANCE(X, W, Batch); wolffd@0: wolffd@0: else wolffd@0: Batch = randperm(length(SAMPLES)); wolffd@0: Batch = SAMPLES(Batch(1:batchSize)); wolffd@0: wolffd@0: Ito = sparse(n,1); wolffd@0: wolffd@0: if isempty(ClassScores) wolffd@0: for i = Batch wolffd@0: Ito(Ypos{i}) = 1; wolffd@0: Ito(Yneg{i}) = 1; wolffd@0: end wolffd@0: D = SETDISTANCE(X, W, Batch, find(Ito)); wolffd@0: else wolffd@0: D = SETDISTANCE(X, W, Batch, 1:n); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: M = 0; wolffd@0: S = zeros(n); wolffd@0: dIndex = sub2ind([n n], 1:n, 1:n); wolffd@0: wolffd@0: SO_time = 0; wolffd@0: wolffd@0: wolffd@0: if isempty(ClassScores) wolffd@0: TS = zeros(batchSize, n); wolffd@0: parfor j = 1:batchSize wolffd@0: i = Batch(j); wolffd@0: if isempty(Yneg) wolffd@0: Ynegative = setdiff((1:n)', [i ; Ypos{i}]); wolffd@0: else wolffd@0: Ynegative = Yneg{i}; wolffd@0: end wolffd@0: SO_start = tic(); wolffd@0: [yi, li] = SO(i, D, Ypos{i}, Ynegative, k); wolffd@0: SO_time = SO_time + toc(SO_start); wolffd@0: wolffd@0: M = M + li /batchSize; wolffd@0: TS(j,:) = PSI(i, yi', n, Ypos{i}, Ynegative); wolffd@0: end wolffd@0: S(Batch,:) = TS; wolffd@0: S(:,Batch) = S(:,Batch) + TS'; wolffd@0: S(dIndex) = S(dIndex) - sum(TS, 1); wolffd@0: else wolffd@0: for j = 1:length(ClassScores.classes) wolffd@0: c = ClassScores.classes(j); wolffd@0: points = find(ClassScores.Y(Batch) == c); wolffd@0: if ~any(points) wolffd@0: continue; wolffd@0: end wolffd@0: wolffd@0: Yneg = find(ClassScores.Yneg{j}); wolffd@0: yp = ClassScores.Ypos{j}; wolffd@0: wolffd@0: TS = zeros(length(points), n); wolffd@0: parfor x = 1:length(points) wolffd@0: i = Batch(points(x)); wolffd@0: yl = yp; wolffd@0: yl(i) = 0; wolffd@0: Ypos = find(yl); wolffd@0: SO_start = tic(); wolffd@0: [yi, li] = SO(i, D, Ypos, Yneg, k); wolffd@0: SO_time = SO_time + toc(SO_start); wolffd@0: wolffd@0: M = M + li /batchSize; wolffd@0: TS(x,:) = PSI(i, yi', n, Ypos, Yneg); wolffd@0: end wolffd@0: S(Batch(points),:) = S(Batch(points),:) + TS; wolffd@0: S(:,Batch(points)) = S(:,Batch(points)) + TS'; wolffd@0: S(dIndex) = S(dIndex) - sum(TS, 1); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: dPsi = CPGRADIENT(X, S, batchSize); wolffd@0: wolffd@0: end