EM_GMM.m 2.96 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 36 37 38 39

%Parameters of the EM algorithm
nbMinSteps = 5; %Minimum number of iterations allowed
nbMaxSteps = 100; %Maximum number of iterations allowed
maxDiffLL = 1E-4; %Likelihood increase threshold to stop the algorithm
nbData = size(Data,2);

40 41
%diagRegularizationFactor = 1E-6; %Regularization term is optional
diagRegularizationFactor = 1E-4; %Regularization term is optional
Sylvain Calinon's avatar
Sylvain Calinon committed
42 43

for nbIter=1:nbMaxSteps
44 45 46 47 48 49 50 51
	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
52
		%Update Priors
53 54
		model.Priors(i) = sum(GAMMA(i,:)) / nbData;
		
55
		%Update Mu
Sylvain Calinon's avatar
Sylvain Calinon committed
56
		model.Mu(:,i) = Data * GAMMA2(i,:)';
57
		
58
		%Update Sigma
Sylvain Calinon's avatar
Sylvain Calinon committed
59
		DataTmp = Data - repmat(model.Mu(:,i),1,nbData);
Sylvain Calinon's avatar
Sylvain Calinon committed
60
		model.Sigma(:,:,i) = DataTmp * diag(GAMMA2(i,:)) * DataTmp' + eye(size(Data,1)) * diagRegularizationFactor;
61 62 63 64 65 66 67 68 69 70 71
	end
	
	%Compute average log-likelihood
	LL(nbIter) = sum(log(sum(L,1))) / nbData;
	%Stop the algorithm if EM converged (small change of LL)
	if nbIter>nbMinSteps
		if LL(nbIter)-LL(nbIter-1)<maxDiffLL || nbIter==nbMaxSteps-1
			disp(['EM converged after ' num2str(nbIter) ' iterations.']);
			return;
		end
	end
Sylvain Calinon's avatar
Sylvain Calinon committed
72
end
73
disp(['The maximum number of ' num2str(nbMaxSteps) ' EM iterations has been reached.']);
Sylvain Calinon's avatar
Sylvain Calinon committed
74 75
end

76

Sylvain Calinon's avatar
Sylvain Calinon committed
77 78
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [L, GAMMA] = computeGamma(Data, model)
79 80 81 82 83
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
84 85
end