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