EM_GMM.m 3.42 KB
Newer Older
Sylvain Calinon's avatar
Sylvain Calinon committed
1
function [model, GAMMA2, LL] = EM_GMM(Data, model)
2
% Training of a Gaussian mixture model (GMM) with an expectation-maximization (EM) algorithm.
Sylvain Calinon's avatar
Sylvain Calinon committed
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
% 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/>.

Sylvain Calinon's avatar
Sylvain Calinon committed
33 34 35

%Parameters of the EM algorithm
nbData = size(Data,2);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
if ~isfield(model,'params_nbMinSteps')
	model.params_nbMinSteps = 5; %Minimum number of iterations allowed
end
if ~isfield(model,'params_nbMaxSteps')
	model.params_nbMaxSteps = 100; %Maximum number of iterations allowed
end
if ~isfield(model,'params_maxDiffLL')
	model.params_maxDiffLL = 1E-4; %Likelihood increase threshold to stop the algorithm
end
if ~isfield(model,'params_diagRegFact')
	%model.params.diagRegFact = 1E-8; %Regularization term is optional
	model.params_diagRegFact = 1E-4; %Regularization term is optional
end
if ~isfield(model,'params_updateComp')
	model.params_updateComp = ones(3,1);
end	
Sylvain Calinon's avatar
Sylvain Calinon committed
52

53
for nbIter=1:model.params_nbMaxSteps
54 55 56 57 58 59 60 61
	fprintf('.');
	
	%E-step
	[L, GAMMA] = computeGamma(Data, model); %See 'computeGamma' function below
	GAMMA2 = GAMMA ./ repmat(sum(GAMMA,2),1,nbData);
	
	%M-step
	for i=1:model.nbStates
62
		%Update Priors
63 64 65
		if model.params_updateComp(1)
			model.Priors(i) = sum(GAMMA(i,:)) / nbData;
		end
66
		%Update Mu
67 68 69
		if model.params_updateComp(2)
			model.Mu(:,i) = Data * GAMMA2(i,:)';
		end
70
		%Update Sigma
71 72 73 74
		if model.params_updateComp(3)
			DataTmp = Data - repmat(model.Mu(:,i),1,nbData);
			model.Sigma(:,:,i) = DataTmp * diag(GAMMA2(i,:)) * DataTmp' + eye(size(Data,1)) * model.params_diagRegFact;
		end
75 76 77 78 79
	end
	
	%Compute average log-likelihood
	LL(nbIter) = sum(log(sum(L,1))) / nbData;
	%Stop the algorithm if EM converged (small change of LL)
80 81
	if nbIter>model.params_nbMinSteps
		if LL(nbIter)-LL(nbIter-1)<model.params_maxDiffLL || nbIter==model.params_nbMaxSteps-1
82 83 84 85
			disp(['EM converged after ' num2str(nbIter) ' iterations.']);
			return;
		end
	end
Sylvain Calinon's avatar
Sylvain Calinon committed
86
end
87
disp(['The maximum number of ' num2str(model.params_nbMaxSteps) ' EM iterations has been reached.']);
Sylvain Calinon's avatar
Sylvain Calinon committed
88 89
end

90

Sylvain Calinon's avatar
Sylvain Calinon committed
91 92
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [L, GAMMA] = computeGamma(Data, model)
93 94 95 96 97
L = zeros(model.nbStates,size(Data,2));
for i=1:model.nbStates
	L(i,:) = model.Priors(i) * gaussPDF(Data, model.Mu(:,i), model.Sigma(:,:,i));
end
GAMMA = L ./ repmat(sum(L,1)+realmin, model.nbStates, 1);
Sylvain Calinon's avatar
Sylvain Calinon committed
98 99
end

100 101