Daniel@0: function [means, covs, weights, ll] = gmmem_multi_restart(K, data, varargin) Daniel@0: % GMMEM_MULTI_RESTART Multiple restart wrapper for gmmem_kpm Daniel@0: % function [means, covs, weights, ll] = gmmem_multi_restart(K, data, varargin) Daniel@0: % Daniel@0: % Input: Daniel@0: % K = number of mixture components Daniel@0: % data(i,:) is the i'th example (feature vector) Daniel@0: % Daniel@0: % Output: Daniel@0: % The parameters for the k'th mixture component, k=1:K, are Daniel@0: % means(k,:), covs(:,:,k) and weights(k) Daniel@0: % Daniel@0: % [ ... ] = gmmem_multi_restart(..., 'param1',val1, 'param2',val2, ...) Daniel@0: % allows you to specify optional parameter name/value pairs. Daniel@0: % Parameters are below [default value in brackets] Daniel@0: % Daniel@0: % 'nrestarts' - number of EM restarts [2] Daniel@0: % 'cov_type' - 'full', 'diag' or 'spherical' ['full'] Daniel@0: % 'init_cov' - the initial covariance matrix [0.1*cov(data) for each k] Daniel@0: % 'init_means' - [] means sample from randn(); otherwise, use Daniel@0: % init_means(k,:,r) for the k'th comp. on the r'th restart [ [] ] Daniel@0: % 'restartfn' - this function, if non-empty, will be called before/after every restart Daniel@0: % (e.g., to display the parameters as they evolve) [ [] ] Daniel@0: % The fn is called as fn(mix{r}, data, restart_num, niter, outerfnargs) Daniel@0: % where niter is the number of iterations performed (0 initially) Daniel@0: % 'restartfnargs' - additional arguments to be passed to restartfn [ {} ] Daniel@0: % Daniel@0: % Optional arguments for gmmem_kpm are passed through. Daniel@0: % Daniel@0: % Written by Kevin P Murphy, 30 Dec 2002 Daniel@0: Daniel@0: [ndata nfeatures] = size(data); Daniel@0: Daniel@0: %Cinit = repmat(0.1*diag(diag(cov(data))), [1 1 K]); Daniel@0: Cinit = repmat(0.1*cov(data), [1 1 K]); Daniel@0: Daniel@0: [nrestarts, init_cov, init_means, cov_type, ... Daniel@0: restartfn, restartfnargs, unused_args] = ... Daniel@0: process_options(varargin, ... Daniel@0: 'nrestarts', 2, 'init_cov', Cinit, 'init_means', [], ... Daniel@0: 'cov_type', 'full', 'restartfn', [], 'restartfnargs', {}); Daniel@0: Daniel@0: mix = cell(1, nrestarts); Daniel@0: cost = inf*ones(1,nrestarts); Daniel@0: Daniel@0: for r=1:nrestarts Daniel@0: mix{r} = gmm(nfeatures, K, cov_type); % random centers Daniel@0: if ~isempty(init_means), mix{r}.centres = init_means(:,:,r); end Daniel@0: mix{r}.covars = init_cov; Daniel@0: if ~isempty(restartfn) Daniel@0: feval(restartfn, mix{r}, data, r, 0, restartfnargs{:}); Daniel@0: end Daniel@0: [mix{r}, niter, ll] = gmmem_kpm(mix{r}, data, unused_args{:}); Daniel@0: cost(r) = -ll; %-sum(log(gmmprob(mix{r}, data))); Daniel@0: if ~isempty(restartfn) Daniel@0: feval(restartfn, mix{r}, data, r, niter, restartfnargs{:}); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: [nll, bestr] = min(cost); Daniel@0: fprintf('best r = %d\n', bestr); Daniel@0: ll = -nll; Daniel@0: means = mix{bestr}.centres; Daniel@0: covs = mix{bestr}.covars; Daniel@0: weights = mix{bestr}.priors;