diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toolboxes/distance_learning/mlr/cuttingPlane/cuttingPlaneRandom.m	Fri Aug 19 13:07:06 2016 +0200
@@ -0,0 +1,97 @@
+function [dPsi, M, SO_time] = cuttingPlaneRandom(k, X, W, Ypos, Yneg, batchSize, SAMPLES, ClassScores)
+%
+% [dPsi, M, SO_time] = cuttingPlaneRandom(k, X, W, Yp, Yn, batchSize, SAMPLES, ClassScores)
+%
+%   k           = k parameter for the SO
+%   X           = d*n data matrix
+%   W           = d*d PSD metric
+%   Yp          = cell-array of relevant results for each point
+%   Yn          = cell-array of irrelevant results for each point
+%   batchSize   = number of points to use in the constraint batch
+%   SAMPLES     = indices of valid points to include in the batch
+%   ClassScores = structure for synthetic constraints
+%
+%   dPsi        = dPsi vector for this batch
+%   M           = mean loss on this batch
+%   SO_time     = time spent in separation oracle
+
+    global SO PSI DISTANCE SETDISTANCE CPGRADIENT;
+
+    [d,n]   = size(X);
+
+
+    if length(SAMPLES) == n
+        % All samples are fair game (full data)
+        Batch   = randperm(n);
+        Batch   = Batch(1:batchSize);
+        D       = SETDISTANCE(X, W, Batch);
+
+    else
+        Batch   = randperm(length(SAMPLES));
+        Batch   = SAMPLES(Batch(1:batchSize));
+
+        Ito     = sparse(n,1);
+
+        if isempty(ClassScores)
+            for i = Batch
+                Ito(Ypos{i}) = 1;
+                Ito(Yneg{i}) = 1;
+            end
+            D       = SETDISTANCE(X, W, Batch, find(Ito));
+        else
+            D       = SETDISTANCE(X, W, Batch, 1:n);
+        end
+    end
+
+
+    M       = 0;
+    S       = zeros(n);
+    dIndex  = sub2ind([n n], 1:n, 1:n);
+
+    SO_time = 0;
+
+    if isempty(ClassScores)
+        for i = Batch
+            SO_start        = tic();
+                [yi, li]    =   SO(i, D, Ypos{i}, Yneg{i}, k);
+            SO_time         = SO_time + toc(SO_start);
+    
+            M               = M + li /batchSize;
+            snew            = PSI(i, yi', n, Ypos{i}, Yneg{i});
+            S(i,:)          = S(i,:) + snew';
+            S(:,i)          = S(:,i) + snew;
+            S(dIndex)       = S(dIndex) - snew';
+        end
+    else
+        for j = 1:length(ClassScores.classes)
+            c       = ClassScores.classes(j);
+            points  = find(ClassScores.Y(Batch) == c);
+            if ~any(points)
+                continue;
+            end
+
+            Yneg    = find(ClassScores.Yneg{j});
+            yp      = ClassScores.Ypos{j};
+
+            for x = 1:length(points)
+                i               = Batch(points(x));
+                yp(i)           = 0;
+                Ypos            = find(yp);
+                SO_start        = tic();
+                    [yi, li]    =   SO(i, D, Ypos, Yneg, k);
+                SO_time         = SO_time + toc(SO_start);
+    
+                M               = M + li /batchSize;
+                snew            = PSI(i, yi', n, Ypos, Yneg);
+                S(i,:)          = S(i,:) + snew';
+                S(:,i)          = S(:,i) + snew;
+                S(dIndex)       = S(dIndex) - snew';
+
+                yp(i) = 1;
+            end
+        end
+    end
+
+    dPsi    = CPGRADIENT(X, S) / batchSize;
+
+end