annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/cmp_learning_dbn.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 [time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, varargin)
Daniel@0 2 % CMP_LEARNING_DBN Compare a bunch of inference engines by learning a DBN
Daniel@0 3 % function [time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, exact, T, ncases, max_iter)
Daniel@0 4 %
Daniel@0 5 % engine{i} is the i'th inference engine.
Daniel@0 6 % time(e) = elapsed time for doing inference with engine e
Daniel@0 7 % CPD{e,c} is the learned CPD for eclass c in engine e
Daniel@0 8 % LL{e} is the learning curve for engine e
Daniel@0 9 % cases{i} is the i'th training case
Daniel@0 10 %
Daniel@0 11 % The list below gives optional arguments [default value in brackets].
Daniel@0 12 %
Daniel@0 13 % exact - specifies which engines do exact inference [ 1:length(engine) ]
Daniel@0 14 % check_ll - 1 means we check that the log-likelihoods are correct [1]
Daniel@0 15 % ncases - num. random training cases [2]
Daniel@0 16 % max_iter - max. num EM iterations [2]
Daniel@0 17
Daniel@0 18 % set default params
Daniel@0 19 exact = 1:length(engine);
Daniel@0 20 check_ll = 1;
Daniel@0 21 ncases = 2;
Daniel@0 22 max_iter = 2;
Daniel@0 23
Daniel@0 24 args = varargin;
Daniel@0 25 nargs = length(args);
Daniel@0 26 for i=1:2:nargs
Daniel@0 27 switch args{i},
Daniel@0 28 case 'exact', exact = args{i+1};
Daniel@0 29 case 'check_ll', check_ll = args{i+1};
Daniel@0 30 case 'ncases', ncases = args{i+1};
Daniel@0 31 case 'max_iter', max_iter = args{i+1};
Daniel@0 32 otherwise,
Daniel@0 33 error(['unrecognized argument ' args{i}])
Daniel@0 34 end
Daniel@0 35 end
Daniel@0 36
Daniel@0 37 E = length(engine);
Daniel@0 38 ss = length(bnet.intra);
Daniel@0 39 onodes = bnet.observed;
Daniel@0 40
Daniel@0 41 cases = cell(1, ncases);
Daniel@0 42 for i=1:ncases
Daniel@0 43 ev = sample_dbn(bnet, 'length', T);
Daniel@0 44 cases{i} = cell(ss,T);
Daniel@0 45 cases{i}(onodes,:) = ev(onodes, :);
Daniel@0 46 end
Daniel@0 47
Daniel@0 48 LL = cell(1,E);
Daniel@0 49 time = zeros(1,E);
Daniel@0 50 for i=1:E
Daniel@0 51 tic
Daniel@0 52 [bnet2{i}, LL{i}] = learn_params_dbn_em(engine{i}, cases, 'max_iter', max_iter);
Daniel@0 53 time(i) = toc;
Daniel@0 54 fprintf('engine %d took %6.4f seconds\n', i, time(i));
Daniel@0 55 end
Daniel@0 56
Daniel@0 57 ref = exact(1); % reference
Daniel@0 58 cmp = mysetdiff(exact, ref);
Daniel@0 59 if check_ll
Daniel@0 60 for i=cmp(:)'
Daniel@0 61 if ~approxeq(LL{ref}, LL{i})
Daniel@0 62 error(['engine ' num2str(i) ' has wrong ll'])
Daniel@0 63 end
Daniel@0 64 end
Daniel@0 65 end
Daniel@0 66
Daniel@0 67 nCPDs = length(bnet.CPD);
Daniel@0 68 CPD = cell(E, nCPDs);
Daniel@0 69 tabular = zeros(1, nCPDs);
Daniel@0 70 for i=1:E
Daniel@0 71 temp = bnet2{i};
Daniel@0 72 for c=1:nCPDs
Daniel@0 73 tabular(c) = isa(temp.CPD{c}, 'tabular_CPD');
Daniel@0 74 CPD{i,c} = struct(temp.CPD{c});
Daniel@0 75 end
Daniel@0 76 end
Daniel@0 77
Daniel@0 78 for i=cmp(:)'
Daniel@0 79 for c=1:nCPDs
Daniel@0 80 if tabular(c)
Daniel@0 81 assert(approxeq(CPD{i,c}.CPT, CPD{ref,c}.CPT));
Daniel@0 82 else
Daniel@0 83 assert(approxeq(CPD{i,c}.mean, CPD{ref,c}.mean));
Daniel@0 84 assert(approxeq(CPD{i,c}.cov, CPD{ref,c}.cov));
Daniel@0 85 assert(approxeq(CPD{i,c}.weights, CPD{ref,c}.weights));
Daniel@0 86 end
Daniel@0 87 end
Daniel@0 88 end
Daniel@0 89