Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/HMM/dhmm_em_online.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 [transmat, obsmat, exp_num_trans, exp_num_emit, gamma, ll] = dhmm_em_online(... | |
2 prior, transmat, obsmat, exp_num_trans, exp_num_emit, decay, data, ... | |
3 act, adj_trans, adj_obs, dirichlet, filter_only) | |
4 % ONLINE_EM Adjust the parameters using a weighted combination of the old and new expected statistics | |
5 % | |
6 % [transmat, obsmat, exp_num_trans, exp_num_emit, gamma, ll] = online_em(... | |
7 % prior, transmat, obsmat, exp_num_trans, exp_num_emit, decay, data, act, ... | |
8 % adj_trans, adj_obs, dirichlet, filter_only) | |
9 % | |
10 % 0 < decay < 1, with smaller values meaning the past is forgotten more quickly. | |
11 % (We need to decay the old ess, since they were based on out-of-date parameters.) | |
12 % The other params are as in learn_hmm. | |
13 % We do a single forwards-backwards pass on the provided data, initializing with the specified prior. | |
14 % (If filter_only = 1, we only do a forwards pass.) | |
15 | |
16 if ~exist('act'), act = []; end | |
17 if ~exist('adj_trans'), adj_trans = 1; end | |
18 if ~exist('adj_obs'), adj_obs = 1; end | |
19 if ~exist('dirichlet'), dirichlet = 0; end | |
20 if ~exist('filter_only'), filter_only = 0; end | |
21 | |
22 % E step | |
23 olikseq = multinomial_prob(data, obsmat); | |
24 if isempty(act) | |
25 [alpha, beta, gamma, ll, xi] = fwdback(prior, transmat, olikseq, 'fwd_only', filter_only); | |
26 else | |
27 [alpha, beta, gamma, ll, xi] = fwdback(prior, transmat, olikseq, 'fwd_only', filter_only, ... | |
28 'act', act); | |
29 end | |
30 | |
31 % Increment ESS | |
32 [S O] = size(obsmat); | |
33 if adj_obs | |
34 exp_num_emit = decay*exp_num_emit + dirichlet*ones(S,O); | |
35 T = length(data); | |
36 if T < O | |
37 for t=1:T | |
38 o = data(t); | |
39 exp_num_emit(:,o) = exp_num_emit(:,o) + gamma(:,t); | |
40 end | |
41 else | |
42 for o=1:O | |
43 ndx = find(data==o); | |
44 if ~isempty(ndx) | |
45 exp_num_emit(:,o) = exp_num_emit(:,o) + sum(gamma(:, ndx), 2); | |
46 end | |
47 end | |
48 end | |
49 end | |
50 | |
51 if adj_trans & (T > 1) | |
52 if isempty(act) | |
53 exp_num_trans = decay*exp_num_trans + sum(xi,3); | |
54 else | |
55 % act(2) determines Q(2), xi(:,:,1) holds P(Q(1), Q(2)) | |
56 A = length(transmat); | |
57 for a=1:A | |
58 ndx = find(act(2:end)==a); | |
59 if ~isempty(ndx) | |
60 exp_num_trans{a} = decay*exp_num_trans{a} + sum(xi(:,:,ndx), 3); | |
61 end | |
62 end | |
63 end | |
64 end | |
65 | |
66 | |
67 % M step | |
68 | |
69 if adj_obs | |
70 obsmat = mk_stochastic(exp_num_emit); | |
71 end | |
72 if adj_trans & (T>1) | |
73 if isempty(act) | |
74 transmat = mk_stochastic(exp_num_trans); | |
75 else | |
76 for a=1:A | |
77 transmat{a} = mk_stochastic(exp_num_trans{a}); | |
78 end | |
79 end | |
80 end |