demo_GPR01.m 4.23 KB
Newer Older
1
function demo_GPR01
2 3
% Gaussian process regression (GPR) 
%
4 5 6 7
% Writing code takes time. Polishing it and making it available to others takes longer! 
% If some parts of the code were useful for your research of for a better understanding 
% of the algorithms, please reward the authors by citing the related publications, 
% and consider making your own research available in this way.
Sylvain Calinon's avatar
Sylvain Calinon committed
8
%
9
% @article{Calinon16JIST,
Sylvain Calinon's avatar
Sylvain Calinon committed
10
%   author="Calinon, S.",
11 12
%   title="A Tutorial on Task-Parameterized Movement Learning and Retrieval",
%   journal="Intelligent Service Robotics",
13 14 15 16 17 18
%		publisher="Springer Berlin Heidelberg",
%		doi="10.1007/s11370-015-0187-9",
%		year="2016",
%		volume="9",
%		number="1",
%		pages="1--29"
Sylvain Calinon's avatar
Sylvain Calinon committed
19
% }
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
% 
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
% 
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
% 
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
% 
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
37 38 39

addpath('./m_fcts/');

Sylvain Calinon's avatar
Sylvain Calinon committed
40

41 42
%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 44 45 46 47
nbVar = 3; %Dimension of datapoint (t,x1,x2)
nbData = 20; %Number of datapoints
nbDataRepro = 100; %Number of datapoints for reproduction
nbSamples = 1; %Number of demonstrations
p(1)=1E0; p(2)=1E1; p(3)=1E-2; %GPR parameters
48 49


50
%% Load handwriting data
51
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 53 54
demos=[];
load('data/2Dletters/G.mat');
Data=[];
55
for n=1:nbSamples
56 57 58 59 60
	s(n).Data = spline(1:size(demos{n}.pos,2), demos{n}.pos, linspace(1,size(demos{n}.pos,2),nbData)); %Resampling
	tt = [1:nbData/2,3*nbData/4:nbData];  %Simulate missing data
	%tt = 1:nbData;
	s(n).Data = [tt; s(n).Data(:,tt)];
	Data = [Data s(n).Data]; 
61
end
62 63 64 65 66 67
%GPR precomputation
xIn = Data(1,:);
xOut = Data(2:end,:);
M = pdist2(xIn', xIn');
K = p(1) * exp(-p(2)^-1 * M.^2);
invK = pinv(K + p(3) * eye(size(K))); 
68 69


70
%% Reproduction with GPR
71
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 73 74 75 76 77 78 79 80 81 82
xInHat = linspace(1,nbData,nbDataRepro);
Md = pdist2(xInHat', xIn');
Kd = p(1) * exp(-p(2)^-1 * Md.^2);
r(1).Data = [xInHat; (Kd * invK * xOut')']; 
%Covariance computation
Mdd = pdist2(xInHat',xInHat');
Kdd = p(1) * exp(-p(2)^-1 * Mdd.^2);
S = Kdd - Kd * invK * Kd';
r(1).SigmaOut = zeros(nbVar-1,nbVar-1,nbData);
for t=1:nbDataRepro
	r(1).SigmaOut(:,:,t) = eye(nbVar-1) * S(t,t); 
83 84 85 86 87
end


%% Plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 89 90 91 92 93 94 95 96 97 98
figure('position',[10 10 1300 600]);
%Plots 1D
for m=2:nbVar
	limAxes = [1, nbData, min(Data(m,:))-1E0, max(Data(m,:))+1E0];
	subplot(nbVar-1,2,(m-2)*2+1); hold on;
	patch([r(1).Data(1,:), r(1).Data(1,end:-1:1)], ...
		[r(1).Data(m,:)+squeeze(r(1).SigmaOut(m-1,m-1,:).^.5)'*2E1, r(1).Data(m,end:-1:1)-squeeze(r(1).SigmaOut(m-1,m-1,end:-1:1).^.5)'*2E1], ...
		[1 .8 .8],'edgecolor','none');
	plot(r(1).Data(1,:), r(1).Data(m,:), '-','lineWidth',3.5,'color',[.8 0 0]);
	for n=1:nbSamples
		plot(s(n).Data(1,:), s(n).Data(m,:), '.','markersize',20,'color',[.2 .2 .2]);
99
	end
100 101 102 103
	set(gca,'xtick',[],'ytick',[]);
	xlabel('$t$','interpreter','latex','fontsize',18);
	ylabel(['$x_' num2str(m) '$'],'interpreter','latex','fontsize',18);
	axis(limAxes);
104
end
105 106 107 108
%Plot 2D
subplot(nbVar-1,2,[2:2:(nbVar-1)*2]); hold on;
plotGMM(r(1).Data(2:3,:),r(1).SigmaOut*1E1,[1 .2 .2],.2);
plot(r(1).Data(2,:), r(1).Data(3,:), '-','lineWidth',3.5,'color',[.8 0 0]);
109
for n=1:nbSamples
110
	plot(s(n).Data(2,:), s(n).Data(3,:), '.','markersize',20,'color',[.2 .2 .2]); 
111
end
112 113 114
set(gca,'xtick',[],'ytick',[]); axis equal; axis square;
xlabel(['$x_1$'],'interpreter','latex','fontsize',18);
ylabel(['$x_2$'],'interpreter','latex','fontsize',18);
115 116 117 118 119

%print('-dpng','graphs/demo_GPR01.png');
%pause;
%close all;