Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/Old/update_ess.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 CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) | |
2 % UPDATE_ESS Update the Expected Sufficient Statistics of a hhmm Q node. | |
3 % function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, idden_bitv) | |
4 | |
5 % Figure out the node numbers associated with each parent | |
6 % e.g., D=4, d=3, Qps = all Qs above, so dom = [Q3(t-1) F4(t-1) F3(t-1) Q1(t) Q2(t) Q3(t)]. | |
7 % so self = Q3(t), old_self = Q3(t-1), CPD.Qps = [1 2], Qps = [Q1(t) Q2(t)] | |
8 dom = fmarginal.domain; | |
9 self = dom(end); | |
10 old_self = dom(1); | |
11 Qps = dom(length(dom)-length(CPD.Qps):end-1); | |
12 | |
13 Qsz = CPD.Qsizes(CPD.d); | |
14 Qpsz = prod(CPD.Qsizes(CPD.Qps)); | |
15 | |
16 % If some of the Q nodes are observed (which happens during supervised training) | |
17 % the counts will only be non-zero in positions | |
18 % consistent with the evidence. We put the computed marginal responsibilities | |
19 % into the appropriate slots of the big counts array. | |
20 % (Recall that observed discrete nodes only have a single effective value.) | |
21 % (A more general, but much slower, way is to call add_evidence_to_dmarginal.) | |
22 % We assume the F nodes are never observed. | |
23 | |
24 obs_self = ~hidden_bitv(self); | |
25 obs_Qps = (~isempty(Qps)) & (~any(hidden_bitv(Qps))); % we assume that all or none of the Q parents are observed | |
26 | |
27 if obs_self | |
28 self_val = evidence{self}; | |
29 oldself_val = evidence{old_self}; | |
30 end | |
31 | |
32 if obs_Qps | |
33 Qps_val = subv2ind(Qpsz, cat(1, evidence{Qps})); | |
34 if Qps_val == 0 | |
35 keyboard | |
36 end | |
37 end | |
38 | |
39 if CPD.d==1 % no Qps from above | |
40 if ~CPD.F1toQ1 % no F from self | |
41 % marg(Q1(t-1), F2(t-1), Q1(t)) | |
42 % F2(t-1) P(Q1(t)=j | Q1(t-1)=i) | |
43 % 1 delta(i,j) | |
44 % 2 transprob(i,j) | |
45 if obs_self | |
46 hor_counts = zeros(Qsz, Qsz); | |
47 hor_counts(oldself_val, self_val) = fmarginal.T(2); | |
48 else | |
49 marg = reshape(fmarginal.T, [Qsz 2 Qsz]); | |
50 hor_counts = squeeze(marg(:,2,:)); | |
51 end | |
52 else | |
53 % marg(Q1(t-1), F2(t-1), F1(t-1), Q1(t)) | |
54 % F2(t-1) F1(t-1) P(Qd(t)=j| Qd(t-1)=i) | |
55 % ------------------------------------------------------ | |
56 % 1 1 delta(i,j) | |
57 % 2 1 transprob(i,j) | |
58 % 1 2 impossible | |
59 % 2 2 startprob(j) | |
60 if obs_self | |
61 marg = myreshape(fmarginal.T, [1 2 2 1]); | |
62 hor_counts = zeros(Qsz, Qsz); | |
63 hor_counts(oldself_val, self_val) = marg(1,2,1,1); | |
64 ver_counts = zeros(Qsz, 1); | |
65 %ver_counts(self_val) = marg(1,2,2,1); | |
66 ver_counts(self_val) = marg(1,2,2,1) + marg(1,1,2,1); | |
67 else | |
68 marg = reshape(fmarginal.T, [Qsz 2 2 Qsz]); | |
69 hor_counts = squeeze(marg(:,2,1,:)); | |
70 %ver_counts = squeeze(sum(marg(:,2,2,:),1)); % sum over i | |
71 ver_counts = squeeze(sum(marg(:,2,2,:),1)) + squeeze(sum(marg(:,1,2,:),1)); % sum i,b | |
72 end | |
73 end % F1toQ1 | |
74 else % d ~= 1 | |
75 if CPD.d < CPD.D % general case | |
76 % marg(Qd(t-1), Fd+1(t-1), Fd(t-1), Qps(t), Qd(t)) | |
77 % Fd+1(t-1) Fd(t-1) P(Qd(t)=j| Qd(t-1)=i, Qps(t)=k) | |
78 % ------------------------------------------------------ | |
79 % 1 1 delta(i,j) | |
80 % 2 1 transprob(i,k,j) | |
81 % 1 2 impossible | |
82 % 2 2 startprob(k,j) | |
83 if obs_Qps & obs_self | |
84 marg = myreshape(fmarginal.T, [1 2 2 1 1]); | |
85 k = 1; | |
86 hor_counts = zeros(Qsz, Qpsz, Qsz); | |
87 hor_counts(oldself_val, Qps_val, self_val) = marg(1, 2,1, k,1); | |
88 ver_counts = zeros(Qpsz, Qsz); | |
89 %ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1); | |
90 ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1) + marg(1, 1,2, k,1); | |
91 elseif obs_Qps & ~obs_self | |
92 marg = myreshape(fmarginal.T, [Qsz 2 2 1 Qsz]); | |
93 k = 1; | |
94 hor_counts = zeros(Qsz, Qpsz, Qsz); | |
95 hor_counts(:, Qps_val, :) = marg(:, 2,1, k,:); | |
96 ver_counts = zeros(Qpsz, Qsz); | |
97 %ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1); | |
98 ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1) + sum(marg(:, 1,2, k,:), 1); | |
99 elseif ~obs_Qps & obs_self | |
100 error('not yet implemented') | |
101 else % everything is hidden | |
102 marg = reshape(fmarginal.T, [Qsz 2 2 Qpsz Qsz]); | |
103 hor_counts = squeeze(marg(:,2,1,:,:)); % i,k,j | |
104 %ver_counts = squeeze(sum(marg(:,2,2,:,:),1)); % sum over i | |
105 ver_counts = squeeze(sum(marg(:,2,2,:,:),1)) + squeeze(sum(marg(:,1,2,:,:),1)); % sum over i,b | |
106 end | |
107 else % d == D, so no F from below | |
108 % marg(QD(t-1), FD(t-1), Qps(t), QD(t)) | |
109 % FD(t-1) P(QD(t)=j | QD(t-1)=i, Qps(t)=k) | |
110 % 1 transprob(i,k,j) | |
111 % 2 startprob(k,j) | |
112 if obs_Qps & obs_self | |
113 marg = myreshape(fmarginal.T, [1 2 1 1]); | |
114 k = 1; | |
115 hor_counts = zeros(Qsz, Qpsz, Qsz); | |
116 hor_counts(oldself_val, Qps_val, self_val) = marg(1, 1, k,1); | |
117 ver_counts = zeros(Qpsz, Qsz); | |
118 ver_counts(Qps_val, self_val) = marg(1, 2, k,1); | |
119 elseif obs_Qps & ~obs_self | |
120 marg = myreshape(fmarginal.T, [Qsz 2 1 Qsz]); | |
121 k = 1; | |
122 hor_counts = zeros(Qsz, Qpsz, Qsz); | |
123 hor_counts(:, Qps_val, :) = marg(:, 1, k,:); | |
124 ver_counts = zeros(Qpsz, Qsz); | |
125 ver_counts(Qps_val, :) = sum(marg(:, 2, k, :), 1); | |
126 elseif ~obs_Qps & obs_self | |
127 error('not yet implemented') | |
128 else % everything is hidden | |
129 marg = reshape(fmarginal.T, [Qsz 2 Qpsz Qsz]); | |
130 hor_counts = squeeze(marg(:,1,:,:)); | |
131 ver_counts = squeeze(sum(marg(:,2,:,:),1)); % sum over i | |
132 end | |
133 end | |
134 end | |
135 | |
136 CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts); | |
137 | |
138 if ~isempty(CPD.sub_CPD_start) | |
139 CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts); | |
140 end | |
141 |