annotate toolboxes/FullBNT-1.0.7/netlabKPM/gmmem_multi_restart.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [means, covs, weights, ll] = gmmem_multi_restart(K, data, varargin)
Daniel@0 2 % GMMEM_MULTI_RESTART Multiple restart wrapper for gmmem_kpm
Daniel@0 3 % function [means, covs, weights, ll] = gmmem_multi_restart(K, data, varargin)
Daniel@0 4 %
Daniel@0 5 % Input:
Daniel@0 6 % K = number of mixture components
Daniel@0 7 % data(i,:) is the i'th example (feature vector)
Daniel@0 8 %
Daniel@0 9 % Output:
Daniel@0 10 % The parameters for the k'th mixture component, k=1:K, are
Daniel@0 11 % means(k,:), covs(:,:,k) and weights(k)
Daniel@0 12 %
Daniel@0 13 % [ ... ] = gmmem_multi_restart(..., 'param1',val1, 'param2',val2, ...)
Daniel@0 14 % allows you to specify optional parameter name/value pairs.
Daniel@0 15 % Parameters are below [default value in brackets]
Daniel@0 16 %
Daniel@0 17 % 'nrestarts' - number of EM restarts [2]
Daniel@0 18 % 'cov_type' - 'full', 'diag' or 'spherical' ['full']
Daniel@0 19 % 'init_cov' - the initial covariance matrix [0.1*cov(data) for each k]
Daniel@0 20 % 'init_means' - [] means sample from randn(); otherwise, use
Daniel@0 21 % init_means(k,:,r) for the k'th comp. on the r'th restart [ [] ]
Daniel@0 22 % 'restartfn' - this function, if non-empty, will be called before/after every restart
Daniel@0 23 % (e.g., to display the parameters as they evolve) [ [] ]
Daniel@0 24 % The fn is called as fn(mix{r}, data, restart_num, niter, outerfnargs)
Daniel@0 25 % where niter is the number of iterations performed (0 initially)
Daniel@0 26 % 'restartfnargs' - additional arguments to be passed to restartfn [ {} ]
Daniel@0 27 %
Daniel@0 28 % Optional arguments for gmmem_kpm are passed through.
Daniel@0 29 %
Daniel@0 30 % Written by Kevin P Murphy, 30 Dec 2002
Daniel@0 31
Daniel@0 32 [ndata nfeatures] = size(data);
Daniel@0 33
Daniel@0 34 %Cinit = repmat(0.1*diag(diag(cov(data))), [1 1 K]);
Daniel@0 35 Cinit = repmat(0.1*cov(data), [1 1 K]);
Daniel@0 36
Daniel@0 37 [nrestarts, init_cov, init_means, cov_type, ...
Daniel@0 38 restartfn, restartfnargs, unused_args] = ...
Daniel@0 39 process_options(varargin, ...
Daniel@0 40 'nrestarts', 2, 'init_cov', Cinit, 'init_means', [], ...
Daniel@0 41 'cov_type', 'full', 'restartfn', [], 'restartfnargs', {});
Daniel@0 42
Daniel@0 43 mix = cell(1, nrestarts);
Daniel@0 44 cost = inf*ones(1,nrestarts);
Daniel@0 45
Daniel@0 46 for r=1:nrestarts
Daniel@0 47 mix{r} = gmm(nfeatures, K, cov_type); % random centers
Daniel@0 48 if ~isempty(init_means), mix{r}.centres = init_means(:,:,r); end
Daniel@0 49 mix{r}.covars = init_cov;
Daniel@0 50 if ~isempty(restartfn)
Daniel@0 51 feval(restartfn, mix{r}, data, r, 0, restartfnargs{:});
Daniel@0 52 end
Daniel@0 53 [mix{r}, niter, ll] = gmmem_kpm(mix{r}, data, unused_args{:});
Daniel@0 54 cost(r) = -ll; %-sum(log(gmmprob(mix{r}, data)));
Daniel@0 55 if ~isempty(restartfn)
Daniel@0 56 feval(restartfn, mix{r}, data, r, niter, restartfnargs{:});
Daniel@0 57 end
Daniel@0 58 end
Daniel@0 59
Daniel@0 60 [nll, bestr] = min(cost);
Daniel@0 61 fprintf('best r = %d\n', bestr);
Daniel@0 62 ll = -nll;
Daniel@0 63 means = mix{bestr}.centres;
Daniel@0 64 covs = mix{bestr}.covars;
Daniel@0 65 weights = mix{bestr}.priors;