资源描述:
《HMM工具箱命令使用说明.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、HOWTOUSETHEHMMTOOLBOX(MATLAB)一、离散输出的隐马尔科夫模型(DHMM,HMMwithdiscreteoutputs)最大似然参数估计EM(BaumWelch算法)Thescriptdhmm_em_demo.mgivesanexampleofhowtolearnanHMMwithdiscreteoutputs.LettherebeQ=2statesandO=3outputsymbols.Wecreaterandomstochasticmatricesasfollows.O=3;Q=2;prior0=normalise(rand(Q,1)
2、);transmat0=mk_stochastic(rand(Q,Q));obsmat0=mk_stochastic(rand(Q,O)); Nowwesamplenex=20sequencesoflengthT=10eachfromthismodel,touseastrainingdata.T=10; %序列长度nex=20; %样本序列数目data=dhmm_sample(prior0,transmat0,obsmat0,nex,T); Heredatais 20x10.Nowwemakearandomguessastowhattheparame
3、tersare,prior1=normalise(rand(Q,1));%初始状态概率transmat1=mk_stochastic(rand(Q,Q));%初始状态转移矩阵obsmat1=mk_stochastic(rand(Q,O));%初始观测状态到隐藏状态间的概率转移矩阵andimproveourguessusing5iterationsofEM...[LL,prior2,transmat2,obsmat2]=dhmm_em(data,prior1,transmat1,obsmat1,'max_iter',5);%prior2,transmat2,obs
4、mat2为训练好后的初始概率,状态转移矩阵及混合状态概率转移矩阵LL(t)isthelog-likelihoodafteriterationt,sowecanplotthelearningcurve.序列分类Toevaluatethelog-likelihoodofatrainedmodelgiventestdata,proceedasfollows:loglik=dhmm_logprob(data,prior2,transmat2,obsmat2) %HMM测试Note:thediscretealphabetisassumedtobe{1,2,...,O},w
5、hereO=size(obsmat,2).Hencedatacannotcontainany0s.Toclassifyasequenceintooneofkclasses,trainupkHMMs,oneperclass,andthencomputethelog-likelihoodthateachmodelgivestothetestsequence;ifthei'thmodelisthemostlikely,thendeclaretheclassofthesequencetobeclassi.Computingthemostprobablesequence(
6、Viterbi)FirstyouneedtoevaluateB(i,t)=P(y_t
7、Q_t=i)forallt,i:B=multinomial_prob(data,obsmat);Thenyoucanuse[path]=viterbi_path(prior,transmat,B) 二、具有高斯混合输出的隐马尔科夫模型(GHMM,HMMwithmixtureofGaussiansoutputs)MaximumlikelihoodparameterestimationusingEM(BaumWelch)Letusgeneratenex=50vector-valu
8、edsequencesoflengthT=50;eachvectorhassizeO=2.O=2;T=50;nex=50;data=randn(O,T,nex);NowletusefitamixtureofM=2GaussiansforeachoftheQ=2statesusingK-means.M=2;Q=2;left_right=0; prior0=normalise(rand(Q,1));transmat0=mk_stochastic(rand(Q,Q)); [mu0,Sigma0]=mixgauss_init(Q*M,reshape(data,[OT*n
9、ex]),cov_typ