annotate toolboxes/FullBNT-1.0.7/netlab3.3/mdnfwd.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 [mixparams, y, z, a] = mdnfwd(net, x)
Daniel@0 2 %MDNFWD Forward propagation through Mixture Density Network.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % MIXPARAMS = MDNFWD(NET, X) takes a mixture density network data
Daniel@0 6 % structure NET and a matrix X of input vectors, and forward propagates
Daniel@0 7 % the inputs through the network to generate a structure MIXPARAMS
Daniel@0 8 % which contains the parameters of several mixture models. Each row
Daniel@0 9 % of X represents one input vector and the corresponding row of the
Daniel@0 10 % matrices in MIXPARAMS represents the parameters of a mixture model
Daniel@0 11 % for the conditional probability of target vectors given the input
Daniel@0 12 % vector. This is not represented as an array of GMM structures to
Daniel@0 13 % improve the efficiency of MDN training.
Daniel@0 14 %
Daniel@0 15 % The fields in MIXPARAMS are
Daniel@0 16 % type = 'mdnmixes'
Daniel@0 17 % ncentres = number of mixture components
Daniel@0 18 % dimtarget = dimension of target space
Daniel@0 19 % mixcoeffs = mixing coefficients
Daniel@0 20 % centres = means of Gaussians: stored as one row per pattern
Daniel@0 21 % covars = covariances of Gaussians
Daniel@0 22 % nparams = number of parameters
Daniel@0 23 %
Daniel@0 24 % [MIXPARAMS, Y, Z] = MDNFWD(NET, X) also generates a matrix Y of the
Daniel@0 25 % outputs of the MLP and a matrix Z of the hidden unit activations
Daniel@0 26 % where each row corresponds to one pattern.
Daniel@0 27 %
Daniel@0 28 % [MIXPARAMS, Y, Z, A] = MLPFWD(NET, X) also returns a matrix A giving
Daniel@0 29 % the summed inputs to each output unit, where each row corresponds to
Daniel@0 30 % one pattern.
Daniel@0 31 %
Daniel@0 32 % See also
Daniel@0 33 % MDN, MDN2GMM, MDNERR, MDNGRAD, MLPFWD
Daniel@0 34 %
Daniel@0 35
Daniel@0 36 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 37 % David J Evans (1998)
Daniel@0 38
Daniel@0 39 % Check arguments for consistency
Daniel@0 40 errstring = consist(net, 'mdn', x);
Daniel@0 41 if ~isempty(errstring)
Daniel@0 42 error(errstring);
Daniel@0 43 end
Daniel@0 44
Daniel@0 45 % Extract mlp and mixture model descriptors
Daniel@0 46 mlpnet = net.mlp;
Daniel@0 47 mixes = net.mdnmixes;
Daniel@0 48
Daniel@0 49 ncentres = mixes.ncentres; % Number of components in mixture model
Daniel@0 50 dim_target = mixes.dim_target; % Dimension of targets
Daniel@0 51 nparams = mixes.nparams; % Number of parameters in mixture model
Daniel@0 52
Daniel@0 53 % Propagate forwards through MLP
Daniel@0 54 [y, z, a] = mlpfwd(mlpnet, x);
Daniel@0 55
Daniel@0 56 % Compute the postion for each parameter in the whole
Daniel@0 57 % matrix. Used to define the mixparams structure
Daniel@0 58 mixcoeff = [1:1:ncentres];
Daniel@0 59 centres = [ncentres+1:1:(ncentres*(1+dim_target))];
Daniel@0 60 variances = [(ncentres*(1+dim_target)+1):1:nparams];
Daniel@0 61
Daniel@0 62 % Convert output values into mixture model parameters
Daniel@0 63
Daniel@0 64 % Use softmax to calculate priors
Daniel@0 65 % Prevent overflow and underflow: use same bounds as glmfwd
Daniel@0 66 % Ensure that sum(exp(y), 2) does not overflow
Daniel@0 67 maxcut = log(realmax) - log(ncentres);
Daniel@0 68 % Ensure that exp(y) > 0
Daniel@0 69 mincut = log(realmin);
Daniel@0 70 temp = min(y(:,1:ncentres), maxcut);
Daniel@0 71 temp = max(temp, mincut);
Daniel@0 72 temp = exp(temp);
Daniel@0 73 mixpriors = temp./(sum(temp, 2)*ones(1,ncentres));
Daniel@0 74
Daniel@0 75 % Centres are just copies of network outputs
Daniel@0 76 mixcentres = y(:,(ncentres+1):ncentres*(1+dim_target));
Daniel@0 77
Daniel@0 78 % Variances are exp of network outputs
Daniel@0 79 mixwidths = exp(y(:,(ncentres*(1+dim_target)+1):nparams));
Daniel@0 80
Daniel@0 81 % Now build up all the mixture model weight vectors
Daniel@0 82 ndata = size(x, 1);
Daniel@0 83
Daniel@0 84 % Return parameters
Daniel@0 85 mixparams.type = mixes.type;
Daniel@0 86 mixparams.ncentres = mixes.ncentres;
Daniel@0 87 mixparams.dim_target = mixes.dim_target;
Daniel@0 88 mixparams.nparams = mixes.nparams;
Daniel@0 89
Daniel@0 90 mixparams.mixcoeffs = mixpriors;
Daniel@0 91 mixparams.centres = mixcentres;
Daniel@0 92 mixparams.covars = mixwidths;
Daniel@0 93