Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/fgraph/fg2.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 a factor graph corresponding to an HMM, where we absorb the evidence up front, | |
2 % and then eliminate the observed nodes. | |
3 % Compare this with not absorbing the evidence. | |
4 | |
5 seed = 1; | |
6 rand('state', seed); | |
7 randn('state', seed); | |
8 | |
9 T = 3; | |
10 Q = 3; | |
11 O = 2; | |
12 cts_obs = 0; | |
13 param_tying = 1; | |
14 bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying); | |
15 N = 2*T; | |
16 onodes = bnet.observed; | |
17 hnodes = mysetdiff(1:N, onodes); | |
18 | |
19 data = sample_bnet(bnet); | |
20 | |
21 init_factor = bnet.CPD{1}; | |
22 obs_factor = bnet.CPD{3}; | |
23 edge_factor = bnet.CPD{2}; % trans matrix | |
24 | |
25 nfactors = T; | |
26 nvars = T; % hidden only | |
27 G = zeros(nvars, nfactors); | |
28 G(1,1) = 1; | |
29 for t=1:T-1 | |
30 G(t:t+1, t+1)=1; | |
31 end | |
32 | |
33 node_sizes = Q*ones(1,T); | |
34 | |
35 % We tie params as follows: | |
36 % the first hidden node use init_factor (number 1) | |
37 % all hidden nodes on the backbone use edge_factor (number 2) | |
38 % all observed nodes use the same factor, namely obs_factor | |
39 | |
40 small_fg = mk_fgraph_given_ev(G, node_sizes, {init_factor, edge_factor}, {obs_factor}, data(onodes), ... | |
41 'equiv_class', [1 2*ones(1,T-1)], 'ev_equiv_class', ones(1,T)); | |
42 | |
43 small_bnet = fgraph_to_bnet(small_fg); | |
44 | |
45 % don't pre-process evidence | |
46 big_fg = bnet_to_fgraph(bnet); | |
47 big_bnet = fgraph_to_bnet(big_fg); | |
48 | |
49 | |
50 | |
51 engine = {}; | |
52 engine{1} = jtree_inf_engine(bnet); | |
53 engine{2} = belprop_fg_inf_engine(small_fg, 'max_iter', 2*T); | |
54 engine{3} = jtree_inf_engine(small_bnet); | |
55 engine{4} = belprop_fg_inf_engine(big_fg, 'max_iter', 3*T); | |
56 engine{5} = jtree_inf_engine(big_bnet); | |
57 nengines = length(engine); | |
58 | |
59 | |
60 % on BN, use the original evidence | |
61 evidence = cell(1, 2*T); | |
62 evidence(onodes) = data(onodes); | |
63 tic; [engine{1}, ll(1)] = enter_evidence(engine{1}, evidence); toc | |
64 | |
65 | |
66 % on small_fg, we have already included the evidence | |
67 evidence = cell(1,T); | |
68 tic; [engine{2}, ll(2)] = enter_evidence(engine{2}, evidence); toc | |
69 | |
70 | |
71 % on small_bnet, we must add evidence to the dummy nodes | |
72 V = small_fg.nvars; | |
73 dummy = V+1:V+small_fg.nfactors; | |
74 N = max(dummy); | |
75 evidence = cell(1, N); | |
76 evidence(dummy) = {1}; | |
77 tic; [engine{3}, ll(3)] = enter_evidence(engine{3}, evidence); toc | |
78 | |
79 | |
80 % on big_fg, use the original evidence | |
81 evidence = cell(1, 2*T); | |
82 evidence(onodes) = data(onodes); | |
83 tic; [engine{4}, ll(4)] = enter_evidence(engine{4}, evidence); toc | |
84 | |
85 | |
86 % on big_bnet, we must add evidence to the dummy nodes | |
87 V = big_fg.nvars; | |
88 assert(V == 2*T); | |
89 dummy = V+1:V+big_fg.nfactors; | |
90 N = max(dummy); | |
91 evidence = cell(1, N); | |
92 evidence(onodes) = data(onodes); | |
93 evidence(dummy) = {1}; | |
94 tic; [engine{5}, ll(5)] = enter_evidence(engine{5}, evidence); toc | |
95 | |
96 | |
97 marg = zeros(T, nengines, Q); % marg(t,e,:) | |
98 for t=1:T | |
99 for e=1:nengines | |
100 m = marginal_nodes(engine{e}, t); | |
101 marg(t,e,:) = m.T; | |
102 end | |
103 end | |
104 marg(:,:,1) |