wolffd@0: function CPD = hhmmQ_CPD(bnet, self, Qnodes, d, D, varargin) wolffd@0: % HHMMQ_CPD Make the CPD for a Q node at depth D of a D-level hierarchical HMM wolffd@0: % CPD = hhmmQ_CPD(bnet, self, Qnodes, d, D, ...) wolffd@0: % wolffd@0: % Fd(t-1) \ Q1:d-1(t) wolffd@0: % \ | wolffd@0: % \ v wolffd@0: % Qd(t-1) -> Qd(t) wolffd@0: % / wolffd@0: % / wolffd@0: % Fd+1(t-1) wolffd@0: % wolffd@0: % We assume parents are ordered (numbered) as follows: wolffd@0: % Qd(t-1), Fd+1(t-1), Fd(t-1), Q1(t), ..., Qd(t) wolffd@0: % wolffd@0: % The parents of Qd(t) can either be just Qd-1(t) or the whole stack Q1:d-1(t) (allQ) wolffd@0: % In either case, we will call them Qps. wolffd@0: % If d=1, Qps does not exist. Also, the F1(t-1) -> Q1(t) arc is optional. wolffd@0: % If the arc is missing, startprob does not need to be specified, wolffd@0: % since the toplevel is assumed to never reset (F1 does not exist). wolffd@0: % If d=D, Fd+1(t-1) does not exist (there is no signal from below). wolffd@0: % wolffd@0: % optional args [defaults] wolffd@0: % wolffd@0: % transprob - transprob(i,k,j) = prob transition from i to j given Qps = k ['leftright'] wolffd@0: % selfprob - prob of a transition from i to i given Qps=k [0.1] wolffd@0: % startprob - startprob(k,j) = prob start in j given Qps = k ['leftstart'] wolffd@0: % startargs - other args to be passed to the sub tabular_CPD for learning startprob wolffd@0: % transargs - other args will be passed to the sub tabular_CPD for learning transprob wolffd@0: % allQ - 1 means use all Q nodes above d as parents, 0 means just level d-1 [0] wolffd@0: % F1toQ1 - 1 means add F1(t-1) -> Q1(t) arc, 0 means level 1 never resets [0] wolffd@0: % wolffd@0: % For d=1, startprob(1,j) is only needed if F1toQ1=1 wolffd@0: % Also, transprob(i,j) can be used instead of transprob(i,1,j). wolffd@0: % wolffd@0: % hhmmQ_CPD is a subclass of tabular_CPD so we inherit inference methods like CPD_to_pot, etc. wolffd@0: % wolffd@0: % We create isolated tabular_CPDs with no F parents to learn transprob/startprob wolffd@0: % so we can avail of e.g., entropic or Dirichlet priors. wolffd@0: % In the future, we will be able to represent the transprob using a tree_CPD. wolffd@0: % wolffd@0: % For details, see "Linear-time inference in hierarchical HMMs", Murphy and Paskin, NIPS'01. wolffd@0: wolffd@0: wolffd@0: ss = bnet.nnodes_per_slice; wolffd@0: %assert(self == Qnodes(d)+ss); wolffd@0: ns = bnet.node_sizes(:); wolffd@0: CPD.Qsizes = ns(Qnodes); wolffd@0: CPD.d = d; wolffd@0: CPD.D = D; wolffd@0: allQ = 0; wolffd@0: wolffd@0: % find out which parents to use, to get right size wolffd@0: for i=1:2:length(varargin) wolffd@0: switch varargin{i}, wolffd@0: case 'allQ', allQ = varargin{i+1}; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: if d==1 wolffd@0: CPD.Qps = []; wolffd@0: else wolffd@0: if allQ wolffd@0: CPD.Qps = Qnodes(1:d-1); wolffd@0: else wolffd@0: CPD.Qps = Qnodes(d-1); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: Qsz = ns(self); wolffd@0: Qpsz = prod(ns(CPD.Qps)); wolffd@0: wolffd@0: % set default arguments wolffd@0: startprob = 'leftstart'; wolffd@0: transprob = 'leftright'; wolffd@0: startargs = {}; wolffd@0: transargs = {}; wolffd@0: CPD.F1toQ1 = 0; wolffd@0: selfprob = 0.1; wolffd@0: wolffd@0: for i=1:2:length(varargin) wolffd@0: switch varargin{i}, wolffd@0: case 'transprob', transprob = varargin{i+1}; wolffd@0: case 'selfprob', selfprob = varargin{i+1}; wolffd@0: case 'startprob', startprob = varargin{i+1}; wolffd@0: case 'startargs', startargs = varargin{i+1}; wolffd@0: case 'transargs', transargs = varargin{i+1}; wolffd@0: case 'F1toQ1', CPD.F1toQ1 = varargin{i+1}; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: Qps = CPD.Qps + ss; wolffd@0: old_self = self-ss; wolffd@0: wolffd@0: if strcmp(transprob, 'leftright') wolffd@0: LR = mk_leftright_transmat(Qsz, selfprob); wolffd@0: transprob = repmat(reshape(LR, [1 Qsz Qsz]), [Qpsz 1 1]); % transprob(k,i,j) wolffd@0: transprob = permute(transprob, [2 1 3]); % now transprob(i,k,j) wolffd@0: end wolffd@0: transargs{end+1} = 'CPT'; wolffd@0: transargs{end+1} = transprob; wolffd@0: CPD.sub_CPD_trans = mk_isolated_tabular_CPD([old_self Qps], ns([old_self Qps self]), transargs); wolffd@0: S = struct(CPD.sub_CPD_trans); wolffd@0: CPD.transprob = myreshape(S.CPT, [Qsz Qpsz Qsz]); wolffd@0: wolffd@0: wolffd@0: if strcmp(startprob, 'leftstart') wolffd@0: startprob = zeros(Qpsz, Qsz); wolffd@0: startprob(:,1) = 1; wolffd@0: end wolffd@0: wolffd@0: if (d==1) & ~CPD.F1toQ1 wolffd@0: CPD.sub_CPD_start = []; wolffd@0: CPD.startprob = []; wolffd@0: else wolffd@0: startargs{end+1} = 'CPT'; wolffd@0: startargs{end+1} = startprob; wolffd@0: CPD.sub_CPD_start = mk_isolated_tabular_CPD(Qps, ns([Qps self]), startargs); wolffd@0: S = struct(CPD.sub_CPD_start); wolffd@0: CPD.startprob = myreshape(S.CPT, [Qpsz Qsz]); wolffd@0: end wolffd@0: wolffd@0: CPD = class(CPD, 'hhmmQ_CPD', tabular_CPD(bnet, self)); wolffd@0: wolffd@0: CPD = update_CPT(CPD); wolffd@0: