annotate toolboxes/FullBNT-1.0.7/bnt/learning/learn_params_em.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 [bnet, LL, engine] = learn_params_em(engine, evidence, max_iter, thresh)
Daniel@0 2 % LEARN_PARAMS_EM Set the parameters of each adjustable node to their ML/MAP values using batch EM.
Daniel@0 3 % [bnet, LLtrace, engine] = learn_params_em(engine, data, max_iter, thresh)
Daniel@0 4 %
Daniel@0 5 % data{i,l} is the value of node i in case l, or [] if hidden.
Daniel@0 6 % Suppose you have L training cases in an O*L array, D, where O is the num observed
Daniel@0 7 % scalar nodes, and N is the total num nodes.
Daniel@0 8 % Then you can create 'data' as follows, where onodes is the index of the observable nodes:
Daniel@0 9 % data = cell(N, L);
Daniel@0 10 % data(onodes,:) = num2cell(D);
Daniel@0 11 % Of course it is possible for different sets of nodes to be observed in each case.
Daniel@0 12 %
Daniel@0 13 % We return the modified bnet and engine.
Daniel@0 14 % To see the learned parameters for node i, use the construct
Daniel@0 15 % s = struct(bnet.CPD{i}); % violate object privacy
Daniel@0 16 % LLtrace is the learning curve: the vector of log-likelihood scores at each iteration.
Daniel@0 17 %
Daniel@0 18 % max_iter specifies the maximum number of iterations. Default: 10.
Daniel@0 19 %
Daniel@0 20 % thresh specifies the thresold for stopping EM. Default: 1e-3.
Daniel@0 21 % We stop when |f(t) - f(t-1)| / avg < threshold,
Daniel@0 22 % where avg = (|f(t)| + |f(t-1)|)/2 and f is log lik.
Daniel@0 23
Daniel@0 24 if nargin < 3, max_iter = 10; end
Daniel@0 25 if nargin < 4, thresh = 1e-3; end
Daniel@0 26
Daniel@0 27 verbose = 1;
Daniel@0 28
Daniel@0 29 loglik = 0;
Daniel@0 30 previous_loglik = -inf;
Daniel@0 31 converged = 0;
Daniel@0 32 num_iter = 1;
Daniel@0 33 LL = [];
Daniel@0 34
Daniel@0 35 while ~converged & (num_iter <= max_iter)
Daniel@0 36 [engine, loglik] = EM_step(engine, evidence);
Daniel@0 37 if verbose, fprintf('EM iteration %d, ll = %8.4f\n', num_iter, loglik); end
Daniel@0 38 num_iter = num_iter + 1;
Daniel@0 39 converged = em_converged(loglik, previous_loglik, thresh);
Daniel@0 40 previous_loglik = loglik;
Daniel@0 41 LL = [LL loglik];
Daniel@0 42 end
Daniel@0 43 if verbose, fprintf('\n'); end
Daniel@0 44
Daniel@0 45 bnet = bnet_from_engine(engine);
Daniel@0 46
Daniel@0 47 %%%%%%%%%
Daniel@0 48
Daniel@0 49 function [engine, loglik] = EM_step(engine, cases)
Daniel@0 50
Daniel@0 51 bnet = bnet_from_engine(engine); % engine contains the old params that are used for the E step
Daniel@0 52 CPDs = bnet.CPD; % these are the new params that get maximized
Daniel@0 53 num_CPDs = length(CPDs);
Daniel@0 54 adjustable = zeros(1,num_CPDs);
Daniel@0 55 for e=1:num_CPDs
Daniel@0 56 adjustable(e) = adjustable_CPD(CPDs{e});
Daniel@0 57 end
Daniel@0 58 adj = find(adjustable);
Daniel@0 59 n = length(bnet.dag);
Daniel@0 60
Daniel@0 61 for e=adj(:)'
Daniel@0 62 CPDs{e} = reset_ess(CPDs{e});
Daniel@0 63 end
Daniel@0 64
Daniel@0 65 loglik = 0;
Daniel@0 66 ncases = size(cases, 2);
Daniel@0 67 for l=1:ncases
Daniel@0 68 evidence = cases(:,l);
Daniel@0 69 [engine, ll] = enter_evidence(engine, evidence);
Daniel@0 70 loglik = loglik + ll;
Daniel@0 71 hidden_bitv = zeros(1,n);
Daniel@0 72 hidden_bitv(isemptycell(evidence))=1;
Daniel@0 73 for i=1:n
Daniel@0 74 e = bnet.equiv_class(i);
Daniel@0 75 if adjustable(e)
Daniel@0 76 fmarg = marginal_family(engine, i);
Daniel@0 77 CPDs{e} = update_ess(CPDs{e}, fmarg, evidence, bnet.node_sizes, bnet.cnodes, hidden_bitv);
Daniel@0 78 end
Daniel@0 79 end
Daniel@0 80 end
Daniel@0 81
Daniel@0 82 for e=adj(:)'
Daniel@0 83 CPDs{e} = maximize_params(CPDs{e});
Daniel@0 84 end
Daniel@0 85
Daniel@0 86 engine = update_engine(engine, CPDs);
Daniel@0 87
Daniel@0 88