annotate toolboxes/FullBNT-1.0.7/netlab3.3/gtmem.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 [net, options, errlog] = gtmem(net, t, options)
Daniel@0 2 %GTMEM EM algorithm for Generative Topographic Mapping.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % [NET, OPTIONS, ERRLOG] = GTMEM(NET, T, OPTIONS) uses the Expectation
Daniel@0 6 % Maximization algorithm to estimate the parameters of a GTM defined by
Daniel@0 7 % a data structure NET. The matrix T represents the data whose
Daniel@0 8 % expectation is maximized, with each row corresponding to a vector.
Daniel@0 9 % It is assumed that the latent data NET.X has been set following a
Daniel@0 10 % call to GTMINIT, for example. The optional parameters have the
Daniel@0 11 % following interpretations.
Daniel@0 12 %
Daniel@0 13 % OPTIONS(1) is set to 1 to display error values; also logs error
Daniel@0 14 % values in the return argument ERRLOG. If OPTIONS(1) is set to 0, then
Daniel@0 15 % only warning messages are displayed. If OPTIONS(1) is -1, then
Daniel@0 16 % nothing is displayed.
Daniel@0 17 %
Daniel@0 18 % OPTIONS(3) is a measure of the absolute precision required of the
Daniel@0 19 % error function at the solution. If the change in log likelihood
Daniel@0 20 % between two steps of the EM algorithm is less than this value, then
Daniel@0 21 % the function terminates.
Daniel@0 22 %
Daniel@0 23 % OPTIONS(14) is the maximum number of iterations; default 100.
Daniel@0 24 %
Daniel@0 25 % The optional return value OPTIONS contains the final error value
Daniel@0 26 % (i.e. data log likelihood) in OPTIONS(8).
Daniel@0 27 %
Daniel@0 28 % See also
Daniel@0 29 % GTM, GTMINIT
Daniel@0 30 %
Daniel@0 31
Daniel@0 32 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 33
Daniel@0 34 % Check that inputs are consistent
Daniel@0 35 errstring = consist(net, 'gtm', t);
Daniel@0 36 if ~isempty(errstring)
Daniel@0 37 error(errstring);
Daniel@0 38 end
Daniel@0 39
Daniel@0 40 % Sort out the options
Daniel@0 41 if (options(14))
Daniel@0 42 niters = options(14);
Daniel@0 43 else
Daniel@0 44 niters = 100;
Daniel@0 45 end
Daniel@0 46
Daniel@0 47 display = options(1);
Daniel@0 48 store = 0;
Daniel@0 49 if (nargout > 2)
Daniel@0 50 store = 1; % Store the error values to return them
Daniel@0 51 errlog = zeros(1, niters);
Daniel@0 52 end
Daniel@0 53 test = 0;
Daniel@0 54 if options(3) > 0.0
Daniel@0 55 test = 1; % Test log likelihood for termination
Daniel@0 56 end
Daniel@0 57
Daniel@0 58 % Calculate various quantities that remain constant during training
Daniel@0 59 [ndata, tdim] = size(t);
Daniel@0 60 ND = ndata*tdim;
Daniel@0 61 [net.gmmnet.centres, Phi] = rbffwd(net.rbfnet, net.X);
Daniel@0 62 Phi = [Phi ones(size(net.X, 1), 1)];
Daniel@0 63 PhiT = Phi';
Daniel@0 64 [K, Mplus1] = size(Phi);
Daniel@0 65
Daniel@0 66 A = zeros(Mplus1, Mplus1);
Daniel@0 67 cholDcmp = zeros(Mplus1, Mplus1);
Daniel@0 68 % Use a sparse representation for the weight regularizing matrix.
Daniel@0 69 if (net.rbfnet.alpha > 0)
Daniel@0 70 Alpha = net.rbfnet.alpha*speye(Mplus1);
Daniel@0 71 Alpha(Mplus1, Mplus1) = 0;
Daniel@0 72 end
Daniel@0 73
Daniel@0 74 for n = 1:niters
Daniel@0 75 % Calculate responsibilities
Daniel@0 76 [R, act] = gtmpost(net, t);
Daniel@0 77 % Calculate error value if needed
Daniel@0 78 if (display | store | test)
Daniel@0 79 prob = act*(net.gmmnet.priors)';
Daniel@0 80 % Error value is negative log likelihood of data
Daniel@0 81 e = - sum(log(max(prob,eps)));
Daniel@0 82 if store
Daniel@0 83 errlog(n) = e;
Daniel@0 84 end
Daniel@0 85 if display > 0
Daniel@0 86 fprintf(1, 'Cycle %4d Error %11.6f\n', n, e);
Daniel@0 87 end
Daniel@0 88 if test
Daniel@0 89 if (n > 1 & abs(e - eold) < options(3))
Daniel@0 90 options(8) = e;
Daniel@0 91 return;
Daniel@0 92 else
Daniel@0 93 eold = e;
Daniel@0 94 end
Daniel@0 95 end
Daniel@0 96 end
Daniel@0 97
Daniel@0 98 % Calculate matrix be inverted (Phi'*G*Phi + alpha*I in the papers).
Daniel@0 99 % Sparse representation of G normally executes faster and saves
Daniel@0 100 % memory
Daniel@0 101 if (net.rbfnet.alpha > 0)
Daniel@0 102 A = full(PhiT*spdiags(sum(R)', 0, K, K)*Phi + ...
Daniel@0 103 (Alpha.*net.gmmnet.covars(1)));
Daniel@0 104 else
Daniel@0 105 A = full(PhiT*spdiags(sum(R)', 0, K, K)*Phi);
Daniel@0 106 end
Daniel@0 107 % A is a symmetric matrix likely to be positive definite, so try
Daniel@0 108 % fast Cholesky decomposition to calculate W, otherwise use SVD.
Daniel@0 109 % (PhiT*(R*t)) is computed right-to-left, as R
Daniel@0 110 % and t are normally (much) larger than PhiT.
Daniel@0 111 [cholDcmp singular] = chol(A);
Daniel@0 112 if (singular)
Daniel@0 113 if (display)
Daniel@0 114 fprintf(1, ...
Daniel@0 115 'gtmem: Warning -- M-Step matrix singular, using pinv.\n');
Daniel@0 116 end
Daniel@0 117 W = pinv(A)*(PhiT*(R'*t));
Daniel@0 118 else
Daniel@0 119 W = cholDcmp \ (cholDcmp' \ (PhiT*(R'*t)));
Daniel@0 120 end
Daniel@0 121 % Put new weights into network to calculate responsibilities
Daniel@0 122 % net.rbfnet = netunpak(net.rbfnet, W);
Daniel@0 123 net.rbfnet.w2 = W(1:net.rbfnet.nhidden, :);
Daniel@0 124 net.rbfnet.b2 = W(net.rbfnet.nhidden+1, :);
Daniel@0 125 % Calculate new distances
Daniel@0 126 d = dist2(t, Phi*W);
Daniel@0 127
Daniel@0 128 % Calculate new value for beta
Daniel@0 129 net.gmmnet.covars = ones(1, net.gmmnet.ncentres)*(sum(sum(d.*R))/ND);
Daniel@0 130 end
Daniel@0 131
Daniel@0 132 options(8) = -sum(log(gtmprob(net, t)));
Daniel@0 133 if (display >= 0)
Daniel@0 134 disp(maxitmess);
Daniel@0 135 end