Bladeren bron

clean-up, consistent dimensionality, flexible initialization

Alexander Freytag 9 jaren geleden
bovenliggende
commit
0a8f34cb1c

+ 1 - 1
README

@@ -1,6 +1,6 @@
 (1) 'Installing everything'
 This package is self-contained and independent from other external sources. You only need to add the path accordingly:
-     Setup path: init
+     Setup path: initWorkspaceGPMulticlass
 
 
 (2) GP models

+ 31 - 10
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 = 50;
+        settings.i_numSamplesPerClassUnlabeled = 500;
     end
     if ( ~isfield(  settings, 'i_numSamplesPerClassTest') || isempty(settings.i_numSamplesPerClassTest) )
         settings.i_numSamplesPerClassTest = 10;
@@ -160,10 +160,20 @@ function evaluateIncrementalLearning ( settings )
     set ( fig_timeComp, 'name', 'Computation Times for Model Updates');
     hold on;
 
-    plot ( timesEfficient, 'Color', c{ 1 }, 'LineStyle', lineStyle{ 1 },  'Marker', marker{ 1 }, ...
-                'LineWidth', linewidth, 'MarkerSize',8 );
-    plot ( timesBatch, 'Color', c{ 2 }, 'LineStyle', lineStyle{ 2 },  'Marker', marker{ 2 }, ...
-                'LineWidth', linewidth, 'MarkerSize',8);
+    plot ( timesEfficient, ...
+           'Color',      c{ 1 }, ...
+           'LineStyle',  lineStyle{ 1 },  ...
+           'Marker',     marker{ 1 }, ...
+           'LineWidth',  linewidth, ...
+           'MarkerSize', 8 ...
+         );
+    plot ( timesBatch, ...
+           'Color',      c{ 2 }, ...
+           'LineStyle',  lineStyle{ 2 },  ...
+           'Marker',     marker{ 2 }, ...
+           'LineWidth',  linewidth, ...
+           'MarkerSize', 8 ...
+       );
     
     leg=legend( {'Efficient', 'Batch'}, 'Location', s_legendLocation,'fontSize', i_fontSize,'LineWidth', 3);
     xlabel('Number of samples added');
@@ -183,13 +193,24 @@ function evaluateIncrementalLearning ( settings )
     set ( fig_accuracyComp, 'name', 'Accuracy over time');
     hold on;
 
-    plot ( arrEfficient, 'Color', c{ 1 }, 'LineStyle', lineStyle{ 1 },  'Marker', marker{ 1 }, ...
-                'LineWidth', linewidth, 'MarkerSize',8 );
-    plot ( arrBatch, 'Color', c{ 2 }, 'LineStyle', lineStyle{ 2 },  'Marker', marker{ 2 }, ...
-                'LineWidth', linewidth, 'MarkerSize',8);
+    plot ( 100*arrEfficient,  ...
+           'Color',      c{ 1 },  ...
+           'LineStyle',  lineStyle{ 1 },  ...
+           'Marker',     marker{ 1 }, ...
+           'LineWidth',  linewidth, ...
+           'MarkerSize', 8  ...
+         );
+    plot ( 100*arrBatch, ...
+           'Color',      c{ 2 }, ...
+           'LineStyle',  lineStyle{ 2 }, ...
+           'Marker',     marker{ 2 }, ...
+           'LineWidth',  linewidth, ...
+           'MarkerSize', 8 ...
+         );
+     
     leg=legend( {'Efficient', 'Batch'}, 'Location', s_legendLocation,'fontSize', i_fontSize,'LineWidth', 3);
     xlabel('Number of samples added');
-    ylabel({'Time spent for model update [s]'});    
+    ylabel({'Accuracy [%]'});    
     
     text_h=findobj(gca,'type','text');  
     set(text_h,'FontSize',i_fontSize);

+ 0 - 0
learn_multiclass_gp.m → gp-model/learn_multiclass_gp.m


+ 1 - 1
test_multiclass_gp.m → gp-model/test_multiclass_gp.m

@@ -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
 

+ 0 - 0
update_multiclass_gp.m → gp-model/update_multiclass_gp.m


+ 0 - 1
init.m

@@ -1 +0,0 @@
-addpath('misc');

+ 71 - 0
initWorkspaceGPMulticlass.m

@@ -0,0 +1,71 @@
+function initWorkspaceGPMulticlass
+% function initWorkspaceGPMulticlass
+% 
+% BRIEF
+%   Add local subfolders and 3rd party libraries to Matlabs work space.
+%
+%   Exemplary call from external position:
+%        GPMULTIDIR = '/place/to/this/repository/';
+%        currentDir = pwd;
+%        cd ( GPMULTIDIR );
+%        initWorkspaceGPMulticlass;
+%        cd ( currentDir );
+%    
+% 
+% Author: Alexander Freytag
+
+    %% setup paths in user-specific manner
+
+
+    % currently we do not have any dependencies, but if we would have some,
+    % we would add them here  
+    
+    %% add paths which come with this repository
+    
+    % add main path
+    b_recursive             = false; 
+    b_overwrite             = true;
+    s_pathMain              = fullfile(pwd);
+    addPathSafely ( s_pathMain, b_recursive, b_overwrite )
+    clear ( 's_pathMain' );      
+    
+    % some useful tiny scripts
+    b_recursive             = true; 
+    b_overwrite             = true;
+    s_pathModel             = fullfile(pwd, 'gp-model');
+    addPathSafely ( s_pathModel, b_recursive, b_overwrite )
+    clear ( 's_pathModel' );    
+    
+    % some useful tiny scripts, mainly covariance functions. see the
+    % gpml-toolbox for further alternatives.
+    b_recursive              = true; 
+    b_overwrite              = true;
+    s_pathMisc               = fullfile(pwd, 'misc');
+    addPathSafely ( s_pathMisc, b_recursive, b_overwrite )
+    clear ( 's_pathMisc' );
+    
+    %% 3rd party, untouched
+    % nothing to add here
+end
+
+
+function addPathSafely ( s_path, b_recursive, b_overwrite )
+    if ( ~isempty(strfind(path, [s_path , pathsep])) )
+        if ( b_overwrite )
+            if ( b_recursive )
+                rmpath( genpath( s_path ) );
+            else
+                rmpath( s_path );
+            end
+        else
+            fprintf('initWSImRet - %s already in your path but overwriting de-activated.\n', s_path);
+            return;
+        end
+    end
+    
+    if ( b_recursive )
+        addpath( genpath( s_path ) );
+    else
+        addpath( s_path );
+    end
+end

+ 92 - 11
multiclass_gp_1D_example.m

@@ -5,9 +5,9 @@ function multiclass_gp_1D_example()
 % Copyright (c) by Alexander Freytag, 2013-11-13.
 
   % get some artificial 1D data
-  train_data = [1;2.5;4.5];
+  train_data = [1;2.5;4.5]';
   train_labels = [1; 2; 4];
-  test_data = (0.1:0.01:5)';
+  test_data = (0.1:0.01:5);
 
   % some default settings of hyperparameters
   loghyper = [-1 0];
@@ -15,16 +15,16 @@ function multiclass_gp_1D_example()
 
   
   % learn multi-class GP model
-  K = feval('covSEiso', loghyper, train_data);
+  K = feval('covSEiso', loghyper, train_data');
   model = learn_multiclass_gp(K, train_labels, gpnoise);
 
   % evaluate model on test data
-  Ks = feval('covSEiso', loghyper, train_data, test_data);
-  Kss = feval('covSEiso', loghyper, test_data, 'diag');  
-  [mu, pred_labels, variance] = test_multiclass_gp(model, Ks, Kss');
+  Ks = feval('covSEiso', loghyper, train_data', test_data');
+  Kss = feval('covSEiso', loghyper, test_data', 'diag');  
+  [mu, pred_labels, variance] = test_multiclass_gp(model, Ks, Kss);
 
   % visualize everything nicely
-  f1 = figure(1);
+  hfigMean = figure(1);
   plot(train_data(1), 1, 'bx');
   title('Mean curve of each binary task');
 
@@ -37,25 +37,106 @@ function multiclass_gp_1D_example()
   plot(test_data, mu(:,3), 'r');
   hold off
 
-  f2 = figure(2);
+  hfigPred = figure(2);
   plot(test_data, pred_labels, 'kx');
   title('Predicted multi-class labels');
 
-  f3 = figure(3);
+  hfigPosterior = figure(3);
   title('Multi-class posterior mean and variance');
   colors = {'b', 'g', 'r'};
   hold on
   max_mean = max(mu,[],2);
   lower = max_mean-sqrt(variance);
   upper = max_mean+sqrt(variance);
-  p = [test_data, lower; flipdim(test_data,1),flipdim(upper,1)];
+  p = [test_data', lower; flipdim(test_data',1),flipdim(upper,1)];
   fill(p(:,1), p(:,2), '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),colors{k}, 'LineWidth', 2);
+    plot( test_data(tmp_ID)', ...
+          mu(tmp_ID,k),...
+          colors{k}, ...
+          'LineWidth', 2 ...
+         );
 
   end
   hold off
+  
+  
+  
+  %% update
+  
+  disp( 'Now add one example...' )
+  
+  pause
+  
+  
+  train_data_new = [2.0];
+  train_labels_new = [1];
+  
+  Ks = feval('covSEiso', loghyper, train_data', train_data_new');
+  Kss = feval('covSEiso', loghyper, train_data_new', 'diag');    
+  model = update_multiclass_gp(model, Kss, Ks, train_labels_new);
+  
+  
+  train_data = [train_data, train_data_new];
+  
+  % evaluate model on test data
+  Ks = feval('covSEiso', loghyper, train_data', test_data');
+  Kss = feval('covSEiso', loghyper, test_data', 'diag');  
+  [mu, pred_labels, variance] = test_multiclass_gp(model, Ks, Kss);
+
+  % visualize everything nicely
+  hfigMeanUpd = figure(4);
+  hold on
+  plot(train_data(1), 1, 'bx');
+  plot(train_data(4), 1, 'bx');
+  title('Mean curve of each binary task');
+
+  
+  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');
+  hold off
+
+  hfigPredUpd = figure(5);
+  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);
+  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');
+  for k=1:length(model.unique_labels)
+
+    tmp_ID = pred_labels == model.unique_labels(k);
+    plot( test_data(tmp_ID)', ...
+          mu(tmp_ID,k), ...
+          colors{k}, ...
+          'LineWidth', 2 ...
+        );
+
+  end
+  hold off  
+  
+  
+  pause
+  
+  close ( hfigMean );
+  close ( hfigPred );
+  close ( hfigPosterior );
+  %
+  close ( hfigMeanUpd );
+  close ( hfigPredUpd );
+  close ( hfigPosteriorUpd );
 
 end