Эх сурвалжийг харах

consistent change of dim of examples to row vectors

Alexander Freytag 9 жил өмнө
parent
commit
6f0da799db

+ 7 - 7
evaluateIncrementalLearning.m

@@ -39,7 +39,7 @@ function evaluateIncrementalLearning ( settings )
         settings.i_numSamplesPerClassTrain = 10;    
     end
     if ( ~isfield(  settings, 'i_numSamplesPerClassUnlabeled') || isempty(settings.i_numSamplesPerClassUnlabeled) )
-        settings.i_numSamplesPerClassUnlabeled = 500;
+        settings.i_numSamplesPerClassUnlabeled = 100;
     end
     if ( ~isfield(  settings, 'i_numSamplesPerClassTest') || isempty(settings.i_numSamplesPerClassTest) )
         settings.i_numSamplesPerClassTest = 10;
@@ -286,7 +286,7 @@ function data = sampleData( settings )
     
 
     % randomly compute some features
-    data.X = abs ( d_mean + d_var.*randn(i_numClasses*i_numSamplesPerClass,i_numDim) );
+    data.X = abs ( d_mean + d_var.*randn(i_numDim,i_numClasses*i_numSamplesPerClass) );
 
     % disturbe features for specific classes
     for i_clIdx = 1:i_numClasses
@@ -294,20 +294,20 @@ function data = sampleData( settings )
         i_idxStart = (i_clIdx-1)*i_numSamplesPerClass+1;
         i_idxEnd = i_clIdx*i_numSamplesPerClass;
 
-        data.X(  i_idxStart:i_idxEnd,  dimToChange(i_clIdx) ) = ...
-                    abs ( data.X(i_idxStart:i_idxEnd,1)  + ... 
+        data.X(  dimToChange(i_clIdx), i_idxStart:i_idxEnd  ) = ...
+                    abs ( data.X(1, i_idxStart:i_idxEnd)  + ... 
                     offsetSpecific( i_clIdx) + ... %add class offset
-                    varSpecific ( i_clIdx) .*randn(i_numSamplesPerClass,1) ... % add class variance
+                    varSpecific ( i_clIdx) .*randn(1,i_numSamplesPerClass) ... % add class variance
                  );
     end
 
 
     % normalize features, thereby simulate histograms, which commonly occure in 
     % computer vision applications
-    data.X = bsxfun(@times, data.X, 1./(sum(data.X, 2)));
+    data.X = bsxfun(@times, data.X, 1./(sum(data.X, 1)));
 
     % adapt class labels
-    data.y =  bsxfun(@times, ones(i_numSamplesPerClass,1), 1:i_numClasses );
+    data.y =  bsxfun(@times, ones(1,i_numSamplesPerClass)', 1:i_numClasses );
     data.y = data.y(:);
 
 end    

+ 4 - 4
gp-model/learn_multiclass_gp.m

@@ -3,9 +3,9 @@ function model = learn_multiclass_gp(K, labels, noise)
 % 
 % function model = learn_multiclass_gp(K, labels, noise)
 %
-% INPUT: K -- kernel matrix of all data points
-%        labels -- multi-class labels, e.g., 1, 3, 4, 5, ...
-%        noise -- variance of Gaussian noise model within GP framework
+% INPUT: K      -- kernel matrix of all data points
+%        labels -- 1 x m vector, multi-class labels, e.g., 1, 3, 4, 5, ...
+%        noise  -- variance of Gaussian noise model within GP framework
 % Copyright (c) by Alexander Freytag, 2013-11-13.
 
   % obtain unique class labels (length of this vector equals the number of classes)
@@ -26,7 +26,7 @@ function model = learn_multiclass_gp(K, labels, noise)
     current_labels = 2*(labels==unique_labels(k))-1;
 
     % precompute and store alpha vector for each binary task
-    model.alpha{k} = L'\(L\current_labels);
+    model.alpha{k} = L'\(L\current_labels');
 
   end
   

+ 10 - 10
gp-model/test_multiclass_gp.m

@@ -3,13 +3,13 @@ function [pred_mean, pred_labels, pred_var] = test_multiclass_gp(model, Ks, Kss)
 % 
 % function [pred_mean, pred_labels, pred_var] = test_multiclass_gp(model, Ks, Kss)
 %
-% INPUT: model -- model structure obtained from function "learn_multiclass_gp"
-%        Ks -- n x m matrix of kernel values between n training and m test samples
-%        Kss -- m x 1 vector of self-similarities of test samples
+% INPUT: model        -- model structure obtained from function "learn_multiclass_gp"
+%        Ks           -- n x m matrix of kernel values between n training and m test samples
+%        Kss          -- 1 x m vector of self-similarities of test samples
 %
-% OUTPUT: pred_mean -- m x c matrix of predicitive mean values for each of the m test samples and each of the c binary tasks 
-%         pred_labels -- m x 1 vector containing predicted multi-class label for each test sample
-%         pred_var -- m x 1 vector containing predicted variance for each test sample
+% OUTPUT: pred_mean   -- c x m matrix of predicitive mean values for each of the m test samples and each of the c binary tasks 
+%         pred_labels -- 1 x m vector containing predicted multi-class label for each test sample
+%         pred_var    -- 1 x m vector containing predicted variance for each test sample
 %
 % NOTE: to get the maximum mean value for each test sample out of the c binary tasks, use: max_mean = max(pred_mean,[],2);
 % Copyright (c) by Alexander Freytag, 2013-11-13.
@@ -18,17 +18,17 @@ function [pred_mean, pred_labels, pred_var] = test_multiclass_gp(model, Ks, Kss)
         Kss = [];
     end
 
-  pred_mean = zeros( size(Ks,2),length(model.unique_labels) );
+  pred_mean = zeros( length(model.unique_labels), size(Ks,2) );
 
   % loop over classes and thus over binary one-vs-all tasks
   for k=1:length(model.unique_labels)
 
-    pred_mean(:,k) = Ks'*model.alpha{k};
+    pred_mean(k,:) = Ks'*model.alpha{k};
   
   end
 
   % obtain predicted labels using max-pooling
-  [~, maxID] = max(pred_mean,[],2);
+  [~, maxID]  = max(pred_mean,[],1);
   pred_labels = model.unique_labels(maxID);
 
   % compute predictive variances if necessary
@@ -41,7 +41,7 @@ function [pred_mean, pred_labels, pred_var] = test_multiclass_gp(model, Ks, Kss)
         return;
     end
     
-    pred_var = Kss - sum(v .* v)' + model.noise;
+    pred_var = (Kss - sum(v .* v)' + model.noise)';
 
   end
 

+ 2 - 2
gp-model/update_multiclass_gp.m

@@ -16,7 +16,7 @@ function model = update_multiclass_gp(modelOld, kss, ksNewSample, newLabel)
   L = [modelOld.L, zeros(length(l),1); l', lss ];  
   
   %obtain new labels in a single vector
-  newLabels = [modelOld.y; newLabel];
+  newLabels = [modelOld.y, newLabel];
   
   % copy all relevant data
   model.noise = modelOld.noise;
@@ -36,7 +36,7 @@ function model = update_multiclass_gp(modelOld, kss, ksNewSample, newLabel)
     current_labels = 2*(newLabels==model.unique_labels(k))-1;
 
     % precompute and store alpha vector for each binary task
-    model.alpha{k} = L'\(L\current_labels);
+    model.alpha{k} = L'\(L\current_labels');
 
   end
   

+ 23 - 18
multiclass_gp_1D_example.m

@@ -6,7 +6,7 @@ function multiclass_gp_1D_example()
 
   % get some artificial 1D data
   train_data = [1;2.5;4.5]';
-  train_labels = [1; 2; 4];
+  train_labels = [1; 2; 4]';
   test_data = (0.1:0.01:5);
 
   % some default settings of hyperparameters
@@ -32,9 +32,9 @@ function multiclass_gp_1D_example()
   plot(train_data(2), 1, 'gx');
   plot(train_data(3), 1, 'rx');
 
-  plot(test_data, mu(:,1), 'b');
-  plot(test_data, mu(:,2), 'g');
-  plot(test_data, mu(:,3), 'r');
+  plot(test_data, mu(1,:), 'b');
+  plot(test_data, mu(2,:), 'g');
+  plot(test_data, mu(3,:), 'r');
   hold off
 
   hfigPred = figure(2);
@@ -45,17 +45,19 @@ function multiclass_gp_1D_example()
   title('Multi-class posterior mean and variance');
   colors = {'b', 'g', 'r'};
   hold on
-  max_mean = max(mu,[],2);
+  max_mean = max(mu,[],1);
   lower = max_mean-sqrt(variance);
   upper = max_mean+sqrt(variance);
-  p = [test_data', lower; flipdim(test_data',1),flipdim(upper,1)];
-  fill(p(:,1), p(:,2), 'y');
+  
+  px = [test_data, flipdim(test_data,2)];
+  py = [lower, flipdim(upper,2)];
+  fill(px, py, 'y');
   
   for k=1:length(model.unique_labels)
 
     tmp_ID = pred_labels == model.unique_labels(k);
-    plot( test_data(tmp_ID)', ...
-          mu(tmp_ID,k),...
+    plot( test_data(tmp_ID), ...
+          mu(k,tmp_ID),...
           colors{k}, ...
           'LineWidth', 2 ...
          );
@@ -98,29 +100,32 @@ function multiclass_gp_1D_example()
   plot(train_data(2), 1, 'gx');
   plot(train_data(3), 1, 'rx');
 
-  plot(test_data, mu(:,1), 'b');
-  plot(test_data, mu(:,2), 'g');
-  plot(test_data, mu(:,3), 'r');
+  plot(test_data, mu(1,:), 'b');
+  plot(test_data, mu(2,:), 'g');
+  plot(test_data, mu(3,:), 'r');
   hold off
 
   hfigPredUpd = figure(5);
-  plot(test_data', pred_labels, 'kx');
+  plot(test_data, pred_labels, 'kx');
   title('Predicted multi-class labels');
 
   hfigPosteriorUpd = figure(6);
   title('Multi-class posterior mean and variance');
   colors = {'b', 'g', 'r'};
   hold on
-  max_mean = max(mu,[],2);
+  max_mean = max(mu,[],1);
   lower = max_mean-sqrt(variance);
   upper = max_mean+sqrt(variance);
-  p = [test_data', lower; flipdim(test_data',1),flipdim(upper,1)];
-  fill(p(:,1), p(:,2), 'y');
+  
+  px = [test_data, flipdim(test_data,2)];
+  py = [lower, flipdim(upper,2)];
+  fill(px, py, 'y');
+  
   for k=1:length(model.unique_labels)
 
     tmp_ID = pred_labels == model.unique_labels(k);
-    plot( test_data(tmp_ID)', ...
-          mu(tmp_ID,k), ...
+    plot( test_data(tmp_ID), ...
+          mu(k,tmp_ID), ...
           colors{k}, ...
           'LineWidth', 2 ...
         );

+ 14 - 14
simulateIncrementalLearning.m

@@ -29,21 +29,21 @@ function results = simulateIncrementalLearning ( labeledData, unlabeledData, tes
     cov = {'covmin'};
     loghyper = [];
  
-    K = feval(cov{:},loghyper, labeled_X);
+    K = feval(cov{:},loghyper, labeled_X');
   
     timeStamp_trainStart = tic;
-    model = learn_multiclass_gp(K, labeled_y, gpnoise);
+    model = learn_multiclass_gp(K, labeled_y', gpnoise);
     times(1) = toc(timeStamp_trainStart);
   
     
     %% (2.2) test initial model
     %copute similarities to test samples  
-    Ks = feval(cov{:},loghyper, labeled_X, test_X);    
+    Ks = feval(cov{:},loghyper, labeled_X', test_X');    
  
     [~, pred_labels] = test_multiclass_gp(model, Ks);
     
     % multi-class accuracy: 1/(# classes) \sum accuracy_per_class
-    numCorrect = norm(double(pred_labels == test_y), 1);
+    numCorrect = norm(double(pred_labels == test_y'), 1);
     testSize = double(size(test_y,1)); 
     ARRscore(1) = double(numCorrect)/double(testSize);
     
@@ -59,32 +59,32 @@ function results = simulateIncrementalLearning ( labeledData, unlabeledData, tes
         %%%%%%%%%%%%%%
         
         % get new sample to add
-        xNew = unlabeled_X( idxToAdd(i_idxAdd), : );
+        xNew = unlabeled_X( :, idxToAdd(i_idxAdd) );
         % simulate asking of user
         yNew = unlabeled_y ( idxToAdd(i_idxAdd) );
         
         if ( b_efficientUpdates )
         
             % compute kernel values
-            kss = feval(cov{:},loghyper, xNew);        
-            ksNewSample = feval(cov{:},loghyper, labeled_X, xNew);
+            kss = feval(cov{:},loghyper, xNew');        
+            ksNewSample = feval(cov{:},loghyper, labeled_X', xNew');
             
 
             timeStamp_updateStart = tic;
             model = update_multiclass_gp(model, kss, ksNewSample, yNew);
             times(i_idxAdd+1) = toc(timeStamp_updateStart);
 
-            labeled_X = [ labeled_X; xNew];
+            labeled_X = [ labeled_X, xNew];
         else
-            kss = feval(cov{:},loghyper, xNew);        
-            ksNewSample = feval(cov{:},loghyper, labeled_X, xNew); 
+            kss = feval(cov{:},loghyper, xNew');        
+            ksNewSample = feval(cov{:},loghyper, labeled_X', xNew'); 
             K = [K, ksNewSample;ksNewSample',kss];
             
-            labeled_X = [ labeled_X; xNew];
+            labeled_X = [ labeled_X, xNew];
             labeled_y = [ labeled_y; yNew];         
 
             timeStamp_updateStart = tic;
-            model = learn_multiclass_gp(K, labeled_y, gpnoise);
+            model = learn_multiclass_gp(K, labeled_y', gpnoise);
             times(i_idxAdd+1) = toc(timeStamp_updateStart);
          
         end
@@ -94,12 +94,12 @@ function results = simulateIncrementalLearning ( labeledData, unlabeledData, tes
         %% test
         %%%%%%%%%%%%%
         
-        Ks = [Ks; feval(cov{:},loghyper, xNew, test_X)]; 
+        Ks = [Ks; feval(cov{:},loghyper, xNew', test_X')]; 
 
         [~, pred_labels] = test_multiclass_gp(model, Ks);
 
         % multi-class accuracy: 1/(# classes) \sum accuracy_per_class
-        numCorrect = norm(double(pred_labels == test_y), 1);
+        numCorrect = norm(double(pred_labels == test_y'), 1);
         testSize = double(size(test_y,1)); 
         ARRscore(i_idxAdd+1) = double(numCorrect)/double(testSize);        
     end