Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slgmm
Home > sltoolbox > stat > slgmm.m

slgmm

PURPOSE ^

SLGMM Learns Gaussian Mixture model from samples

SYNOPSIS ^

function [GS, pp, info] = slgmm(X, varargin)

DESCRIPTION ^

SLGMM Learns Gaussian Mixture model from samples

 $ Syntax $
   - GS = slgmm(X, ...)
   - [GS, pp] = slgmm(X, ...)
   - [GS, pp, info] = slgmm(X, ...)

 $ Arguments $
   - GS:       The Gaussian model struct with mixture weights
   - pp:       the posteriori of samples to component models
   - info:     the information of learning process
   - X:        the sample matrix 

 $ Description $
   - GS = slgmm(X, ...) learns Gaussian mixture model from samples
     in matrix X. The following properties can be specified:
     \*  
     \t    Table. Properties of Learning Gausssian Mixture Model
     \h    name         &      description                      \\
          'method'      & The method using for GMM learning. Currently,
                          there are only one method available: 'EM',
                          default = 'EM'.
          'K'           & The number of initial components in the mixture 
                          model (default = 3)                     \\
          'update'      & The way of updating (default = 'pass'):
                          1. 'pass'     Pass-wise update;
                          2. 'comp'     Component-wise update     \\
          'cyclecn'     & The ratio of components to be updated in each 
                          cycle for 'comp' update scheme. default = 1.
          'maxiter'     & The maximum number of iterations 
                          (default = 100) \\
          'tol'         & The maximum tolerance of posteriori error when 
                          the iteration terminates, (default = 1e-6) \\
          'verbose'     & whether to display information while iteration, 
                          default = true                              \\
          'annthres'    & The threshold of annealing in FJ algorithm, 
                          default = 0 (unit = average mixture weight) \\
          'weights'     & The weights of the samples, default = [], 
                          indicating non-weighted. Weights should be given
                          by a 1 x n row vector. \\
          'varform'     & The form of variance: 'univar'|'diagvar'|'covar', 
                          default = 'covar';          \\
          'sharevar'    & Whether the variance is shared by all models,
                          default = false;            \\
          'invparams'   & The parameters for computing inverse of variance
                          default = {}                \\
           
 $ Remarks $
   - In current version, the component-wise update scheme is only
     supported when sharevar is false.

 $ History $
   - Created by Dahua Lin, on Aug 28, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slclassify_eucnn SLCLASSIFY_EUCNN Classifies samples using Euclidena-based NN
  • slfmm SLFMM Learns a Finite Mixture Model (FMM)
  • slgaussest SLGAUSSEST Estimates the Gaussian models from samples
  • slgausspdf SLGAUSSPDF Computes the probability density of Gaussian models
  • slgausstype SLGAUSSTYPE Judges the type of a Gaussian model struct
  • slignorevars SLIGNOREVARS Ignores the input variables
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [GS, pp, info] = slgmm(X, varargin)
0002 %SLGMM Learns Gaussian Mixture model from samples
0003 %
0004 % $ Syntax $
0005 %   - GS = slgmm(X, ...)
0006 %   - [GS, pp] = slgmm(X, ...)
0007 %   - [GS, pp, info] = slgmm(X, ...)
0008 %
0009 % $ Arguments $
0010 %   - GS:       The Gaussian model struct with mixture weights
0011 %   - pp:       the posteriori of samples to component models
0012 %   - info:     the information of learning process
0013 %   - X:        the sample matrix
0014 %
0015 % $ Description $
0016 %   - GS = slgmm(X, ...) learns Gaussian mixture model from samples
0017 %     in matrix X. The following properties can be specified:
0018 %     \*
0019 %     \t    Table. Properties of Learning Gausssian Mixture Model
0020 %     \h    name         &      description                      \\
0021 %          'method'      & The method using for GMM learning. Currently,
0022 %                          there are only one method available: 'EM',
0023 %                          default = 'EM'.
0024 %          'K'           & The number of initial components in the mixture
0025 %                          model (default = 3)                     \\
0026 %          'update'      & The way of updating (default = 'pass'):
0027 %                          1. 'pass'     Pass-wise update;
0028 %                          2. 'comp'     Component-wise update     \\
0029 %          'cyclecn'     & The ratio of components to be updated in each
0030 %                          cycle for 'comp' update scheme. default = 1.
0031 %          'maxiter'     & The maximum number of iterations
0032 %                          (default = 100) \\
0033 %          'tol'         & The maximum tolerance of posteriori error when
0034 %                          the iteration terminates, (default = 1e-6) \\
0035 %          'verbose'     & whether to display information while iteration,
0036 %                          default = true                              \\
0037 %          'annthres'    & The threshold of annealing in FJ algorithm,
0038 %                          default = 0 (unit = average mixture weight) \\
0039 %          'weights'     & The weights of the samples, default = [],
0040 %                          indicating non-weighted. Weights should be given
0041 %                          by a 1 x n row vector. \\
0042 %          'varform'     & The form of variance: 'univar'|'diagvar'|'covar',
0043 %                          default = 'covar';          \\
0044 %          'sharevar'    & Whether the variance is shared by all models,
0045 %                          default = false;            \\
0046 %          'invparams'   & The parameters for computing inverse of variance
0047 %                          default = {}                \\
0048 %
0049 % $ Remarks $
0050 %   - In current version, the component-wise update scheme is only
0051 %     supported when sharevar is false.
0052 %
0053 % $ History $
0054 %   - Created by Dahua Lin, on Aug 28, 2006
0055 %
0056 
0057 %% parse and verify input arguments
0058 
0059 if ~isnumeric(X) || ndims(X) ~= 2
0060     error('sltoolbox:invalidarg', ...
0061         'X should be a 2D numeric matrix');
0062 end
0063 
0064 opts.method = 'EM';
0065 opts.K = 3;
0066 opts.update = 'pass';
0067 opts.cyclecn = 1;
0068 opts.maxiter = 100;
0069 opts.tol = 1e-6;
0070 opts.verbose = true;
0071 opts.annthres = 0;
0072 opts.weights = [];
0073 opts.varform = 'covar';
0074 opts.sharevar = false;
0075 opts.invparams = {};
0076 opts = slparseprops(opts, varargin{:});
0077 
0078 n = size(X, 2);
0079 
0080 
0081 %% initialization by random clustering
0082 
0083 if opts.K > n
0084     error('sltoolbox:sizoverflow', ...
0085         'The K is larger than the number of samples');
0086 end
0087 
0088 init_centerinds = randsample(n, opts.K);
0089 init_centers = X(:, init_centerinds);
0090 initc = slclassify_eucnn(init_centers, X);
0091 
0092 %% perform learning based on FMM
0093 
0094 estfunctor = {@gmm_estfunc, opts.varform, opts.sharevar, opts.invparams};
0095 evalfunctor = {@gmm_evalfunc};
0096 
0097 [GS, cw, pp, info] = slfmm(X, n, estfunctor, evalfunctor, ...
0098     'method', opts.method, ...
0099     'update', opts.update, ...
0100     'cyclecn', opts.cyclecn, ...
0101     'iter', {'maxiter', opts.maxiter}, ...
0102     'tol', opts.tol, ...
0103     'verbose', opts.verbose, ...
0104     'initc', initc, ...
0105     'annthres', opts.annthres, ...
0106     'weights', opts.weights, ...
0107     'estmode', 'innermul', ...
0108     'condpmode', 'log', ...
0109     'manifunc', @gmm_manifunc);
0110 
0111 %% post actions
0112 
0113 GS.mixweights = cw;
0114 
0115 
0116 %% Core slot functions
0117 
0118 function GS = gmm_estfunc(GS, X, n, W, selinds, varform, sharevar, invparams)
0119 
0120 slignorevars(n);
0121 
0122 if isempty(selinds)
0123     GS = slgaussest(X, 'weights', W, ...
0124         'varform', varform, 'sharevar', sharevar, ...
0125         'compinv', true, 'invparams', invparams);
0126 else
0127     if sharevar
0128         error('sltoolbox:rterror', ...
0129             'The selective updating scheme is only supported when sharevar is false');
0130     end
0131     
0132     Wsel = W(selinds, :);
0133     GSu = slgaussest(X, 'weights', Wsel, ...
0134         'varform', varform, 'sharevar', sharevar, ...
0135         'compinv', true, 'invparams', invparams);
0136     
0137     nu = length(selinds);
0138     switch varform
0139         case 'univar'
0140             for idx = 1 : nu
0141                 iu = selinds(idx);
0142                 GS.means(:, iu) = GSu.means(:,idx);
0143                 GS.vars(iu) = GSu.vars(idx);
0144                 GS.invvars(iu) = GSu.invvars(idx);
0145             end
0146         case 'diagvar'
0147             for idx = 1 : nu
0148                 iu = selinds(idx);
0149                 GS.means(:, iu) = GSu.means(:, idx);
0150                 GS.vars(:, iu) = GSu.vars(:, idx);
0151                 GS.invvars(:, iu) = GSu.invvars(:, idx);
0152             end
0153         case 'covar'
0154             for idx = 1 : nu
0155                 iu = selinds(idx);
0156                 GS.means(:, iu) = GSu.means(:, idx);
0157                 GS.covs(:,:, iu) = GSu.covs(:,:, idx);
0158                 GS.invcovs(:,:, iu) = GSu.invcovs(:,:, idx);
0159             end
0160     end
0161     
0162 end
0163 
0164 function condp = gmm_evalfunc(GS, X, n)
0165 
0166 slignorevars(n);
0167 condp = slgausspdf(GS, X, 'output', 'log');
0168 
0169 
0170 function GS = gmm_manifunc(GS0, op, varargin)
0171 
0172 switch op
0173     case 'select'
0174         
0175         selinds = varargin{1};
0176         
0177         tyi = slgausstype(GS0);
0178         
0179         GS.dim = GS0.dim;
0180         GS.nmodels = length(selinds);
0181         GS.means = GS0.means(:, selinds);
0182         
0183         switch tyi.varform
0184             case {'univar', 'diagvar'}
0185                 if tyi.sharevar
0186                     GS.vars = GS0.vars;
0187                     GS.invvars = GS0.invvars;
0188                 else
0189                     GS.vars = GS0.vars(:, selinds);
0190                     GS.invvars = GS0.invvars(:, selinds);
0191                 end
0192             case 'covar'
0193                 if tyi.sharevar
0194                     GS.covs = GS0.covs;
0195                     GS.invcovs = GS0.invcovs;
0196                 else
0197                     GS.covs = GS0.covs(:,:,selinds);
0198                     GS.invcovs = GS0.invcovs(:,:,selinds);
0199                 end
0200         end
0201         
0202         
0203     otherwise
0204         error('sltoolbox:invalidarg', ...
0205             'Unsupported manipulation option for GMM: %s', op);
0206 end
0207 
0208 
0209 
0210 
0211 
0212 
0213

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com