Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/dhmm1.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 % Make an HMM with discrete observations | |
2 % X1 -> X2 | |
3 % | | | |
4 % v v | |
5 % Y1 Y2 | |
6 | |
7 intra = zeros(2); | |
8 intra(1,2) = 1; | |
9 inter = zeros(2); | |
10 inter(1,1) = 1; | |
11 n = 2; | |
12 | |
13 Q = 2; % num hidden states | |
14 O = 2; % num observable symbols | |
15 | |
16 ns = [Q O]; | |
17 dnodes = 1:2; | |
18 onodes = [2]; | |
19 eclass1 = [1 2]; | |
20 eclass2 = [3 2]; | |
21 bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... | |
22 'observed', onodes); | |
23 | |
24 rand('state', 0); | |
25 prior1 = normalise(rand(Q,1)); | |
26 transmat1 = mk_stochastic(rand(Q,Q)); | |
27 obsmat1 = mk_stochastic(rand(Q,O)); | |
28 bnet.CPD{1} = tabular_CPD(bnet, 1, prior1); | |
29 bnet.CPD{2} = tabular_CPD(bnet, 2, obsmat1); | |
30 bnet.CPD{3} = tabular_CPD(bnet, 3, transmat1); | |
31 | |
32 | |
33 T = 5; % fixed length sequences | |
34 | |
35 engine = {}; | |
36 engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); | |
37 engine{end+1} = hmm_inf_engine(bnet); | |
38 engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); | |
39 engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); | |
40 if 1 | |
41 %engine{end+1} = frontier_inf_engine(bnet); % broken | |
42 engine{end+1} = bk_inf_engine(bnet, 'clusters', {[1]}); | |
43 engine{end+1} = jtree_dbn_inf_engine(bnet); | |
44 end | |
45 | |
46 inf_time = cmp_inference_dbn(bnet, engine, T); | |
47 | |
48 ncases = 2; | |
49 max_iter = 2; | |
50 [learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); | |
51 | |
52 % Compare to HMM toolbox | |
53 | |
54 data = zeros(ncases, T); | |
55 for i=1:ncases | |
56 %data(i,:) = cat(2, cases{i}{onodes,:}); | |
57 data(i,:) = cell2num(cases{i}(onodes,:)); | |
58 end | |
59 [LL2, prior2, transmat2, obsmat2] = dhmm_em(data, prior1, transmat1, obsmat1, 'max_iter', max_iter); | |
60 | |
61 e = 1; | |
62 assert(approxeq(prior2, CPD{e,1}.CPT)) | |
63 assert(approxeq(obsmat2, CPD{e,2}.CPT)) | |
64 assert(approxeq(transmat2, CPD{e,3}.CPT)) | |
65 assert(approxeq(LL2, LL{e})) | |
66 |