comparison toolboxes/FullBNT-1.0.7/netlabKPM/gmmem_multi_restart.m @ 0:e9a9cd732c1e tip

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