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