wolffd@0: function CPD = maximize_params(CPD, temp) wolffd@0: % MAXIMIZE_PARAMS Set the params of a hhmmQ node to their ML/MAP values. wolffd@0: % CPD = maximize_params(CPD, temperature) wolffd@0: wolffd@0: Qsz = CPD.Qsizes(CPD.d); wolffd@0: Qpsz = prod(CPD.Qsizes(CPD.Qps)); wolffd@0: wolffd@0: if ~isempty(CPD.sub_CPD_start) wolffd@0: CPD.sub_CPD_start = maximize_params(CPD.sub_CPD_start, temp); wolffd@0: S = struct(CPD.sub_CPD_start); wolffd@0: CPD.startprob = myreshape(S.CPT, [Qpsz Qsz]); wolffd@0: %CPD.startprob = S.CPT; wolffd@0: end wolffd@0: wolffd@0: if 1 wolffd@0: % If we are in a state that can only go the end state, wolffd@0: % we will never see a transition to another (non-end) state, wolffd@0: % so counts(i,k,j)=0 (and termprob(k,i)=1). wolffd@0: % We set counts(i,k,i)=1 in this case. wolffd@0: % This will cause remove_hhmm_end_state to return a wolffd@0: % stochastic matrix, but otherwise has no effect on EM. wolffd@0: counts = get_field(CPD.sub_CPD_trans, 'counts'); wolffd@0: counts = reshape(counts, [Qsz Qpsz Qsz]); wolffd@0: for k=1:Qpsz wolffd@0: for i=1:Qsz wolffd@0: if sum(counts(i,k,:))==0 % never witnessed a transition out of i wolffd@0: counts(i,k,i)=1; % add self loop wolffd@0: %fprintf('CPDQ d=%d i=%d k=%d\n', CPD.d, i, k); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: CPD.sub_CPD_trans = set_fields(CPD.sub_CPD_trans, 'counts', counts(:)); wolffd@0: end wolffd@0: wolffd@0: CPD.sub_CPD_trans = maximize_params(CPD.sub_CPD_trans, temp); wolffd@0: S = struct(CPD.sub_CPD_trans); wolffd@0: %CPD.transprob = S.CPT; wolffd@0: CPD.transprob = myreshape(S.CPT, [Qsz Qpsz Qsz]); wolffd@0: wolffd@0: CPD = update_CPT(CPD);