Commit edcab51a authored by Sylvain Calinon's avatar Sylvain Calinon

Merge branch 'master' of gitlab.idiap.ch:rli/task-parameterized-tensor-gmm-with-lqr

parents 878c14e4 b2adef22
function model = init_tensorGMM_kmeans(Data, model)
% Author: Leonel Rozo, 2014
% http://programming-by-demonstration.org/LeonelRozo
%
diagRegularizationFactor = 1E-4;
%Matricization/flattening of tensor
DataAll = reshape(Data, size(Data,1)*size(Data,2), size(Data,3));
%The function 'kmeans' below is from the Matlab Statistics Toolbox (see note above)
[Data_id, Centers] = kmeans(DataAll', model.nbStates);
% Setting means and covariance matrices
Mu = Centers';
Sigma = zeros(model.nbFrames*model.nbVar, model.nbFrames*model.nbVar, model.nbStates);
for i = 1 : model.nbStates
idtmp = find(Data_id==i);
model.Priors(i) = length(idtmp);
Sigma(:,:,i) = cov(DataAll(:,idtmp)') + eye(size(DataAll,1))*diagRegularizationFactor;
end
model.Priors = model.Priors / sum(model.Priors);
%Reshape GMM parameters into a tensor
for m = 1 : model.nbFrames
for i = 1 : model.nbStates
model.Mu(:,m,i) = Mu((m-1)*model.nbVar+1:m*model.nbVar,i);
model.Sigma(:,:,m,i) = Sigma((m-1)*model.nbVar+1:m*model.nbVar,(m-1)*model.nbVar+1:m*model.nbVar,i);
end
end
function [idList, Mu] = kmeansClustering(Data, nbStates)
% Initialization of the model with k-means.
% Author: Sylvain Calinon, Tohid Alizadeh, 2013
% http://programming-by-demonstration.org/
%Criterion to stop the EM iterative update
cumdist_threshold = 1e-10;
maxIter = 100;
%Initialization of the parameters
[nbVar, nbData] = size(Data);
cumdist_old = -realmax;
nbStep = 0;
idTmp = randperm(nbData);
Mu = Data(:,idTmp(1:nbStates));
%k-means iterations
while 1
%E-step %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:nbStates
%Compute distances
distTmp(:,i) = sum((Data-repmat(Mu(:,i),1,nbData)).^2);
end
[vTmp,idList] = min(distTmp,[],2);
cumdist = sum(vTmp);
%M-step %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:nbStates
%Update the centers
Mu(:,i) = mean(Data(:,idList==i),2);
end
%Stopping criterion %%%%%%%%%%%%%%%%%%%%
if abs(cumdist-cumdist_old) < cumdist_threshold
break;
end
cumdist_old = cumdist;
nbStep = nbStep+1;
% if nbStep>maxIter
% disp(['Maximum number of iterations, ' num2str(maxIter) 'is reached']);
% break;
% end
end
%disp(['Kmeans stopped after ' num2str(nbStep) ' steps.']);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment