init_tensorGMM_kmeans.m 2.05 KB
Newer Older
1
function model = init_tensorGMM_kmeans(Data, model)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
% Initialization of TP-GMM with k-means.
%
% 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.
%
% @article{Calinon15,
%   author="Calinon, S.",
%   title="A Tutorial on Task-Parameterized Movement Learning and Retrieval",
%   journal="Intelligent Service Robotics",
%   year="2015"
% }
%
% 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/>.


nbVar = size(Data,1); 
diagRegularizationFactor = 1E-4; %Optional regularization term
36

37
DataAll = reshape(Data, size(Data,1)*size(Data,2), size(Data,3)); %Matricization/flattening of tensor
38

39 40
%k-means clustering
[Data_id, Mu] = kmeansClustering(DataAll, model.nbStates);
41

42
for i=1:model.nbStates
Milad Malekzadeh's avatar
Milad Malekzadeh committed
43 44 45
	idtmp = find(Data_id==i);
	model.Priors(i) = length(idtmp);
	Sigma(:,:,i) = cov([DataAll(:,idtmp) DataAll(:,idtmp)]') + eye(size(DataAll,1))*diagRegularizationFactor;
46 47 48
end
model.Priors = model.Priors / sum(model.Priors);

49
%Reshape GMM parameters into tensor data
50
for m=1:model.nbFrames
Milad Malekzadeh's avatar
Milad Malekzadeh committed
51
	for i=1:model.nbStates
Sylvain Calinon's avatar
Sylvain Calinon committed
52 53
		model.Mu(:,m,i) = Mu((m-1)*nbVar+1:m*nbVar,i);
		model.Sigma(:,:,m,i) = Sigma((m-1)*nbVar+1:m*nbVar,(m-1)*nbVar+1:m*nbVar,i);
Milad Malekzadeh's avatar
Milad Malekzadeh committed
54
	end
55
end