hmm Viterbi+algorithm.docx

上传人:b****7 文档编号:9969146 上传时间:2023-02-07 格式:DOCX 页数:18 大小:55.31KB
下载 相关 举报
hmm Viterbi+algorithm.docx_第1页
第1页 / 共18页
hmm Viterbi+algorithm.docx_第2页
第2页 / 共18页
hmm Viterbi+algorithm.docx_第3页
第3页 / 共18页
hmm Viterbi+algorithm.docx_第4页
第4页 / 共18页
hmm Viterbi+algorithm.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

hmm Viterbi+algorithm.docx

《hmm Viterbi+algorithm.docx》由会员分享,可在线阅读,更多相关《hmm Viterbi+algorithm.docx(18页珍藏版)》请在冰豆网上搜索。

hmm Viterbi+algorithm.docx

hmmViterbi+algorithm

Viterbialgorithm

Overview

Theassumptionslistedabovecanbeelaboratedasfollows.TheViterbialgorithmoperatesonastatemachineassumption.Thatis,atanytimethesystembeingmodeledisinsomestate.Thereareafinitenumberofstates.Whilemultiplesequencesofstates(paths)canleadtoagivenstate,atleastoneofthemisamostlikelypathtothatstate,calledthe"survivorpath".Thisisafundamentalassumptionofthealgorithmbecausethealgorithmwillexamineallpossiblepathsleadingtoastateandonlykeeptheonemostlikely.Thiswaythealgorithmdoesnothavetokeeptrackofallpossiblepaths,onlyoneperstate.

Asecondkeyassumptionisthatatransitionfromapreviousstatetoanewstateismarkedbyanincrementalmetric,usuallyanumber.Thistransitioniscomputedfromtheevent.Thethirdkeyassumptionisthattheeventsarecumulativeoverapathinsomesense,usuallyadditive.Sothecruxofthealgorithmistokeepanumberforeachstate.Whenaneventoccurs,thealgorithmexaminesmovingforwardtoanewsetofstatesbycombiningthemetricofapossiblepreviousstatewiththeincrementalmetricofthetransitionduetotheeventandchoosesthebest.Theincrementalmetricassociatedwithaneventdependsonthetransitionpossibilityfromtheoldstatetothenewstate.Forexampleindatacommunications,itmaybepossibletoonlytransmithalfthesymbolsfromanoddnumberedstateandtheotherhalffromanevennumberedstate.Additionally,inmanycasesthestatetransitiongraphisnotfullyconnected.Asimpleexampleisacarthathas3states—forward,stopandreverse—andatransitionfromforwardtoreverseisnotallowed.Itmustfirstenterthestopstate.Aftercomputingthecombinationsofincrementalmetricandstatemetric,onlythebestsurvivesandallotherpathsarediscarded.Therearemodificationstothebasicalgorithmwhichallowforaforwardsearchinadditiontothebackwardsonedescribedhere.

Pathhistorymustbestored.Insomecases,thesearchhistoryiscompletebecausethestatemachineattheencoderstartsinaknownstateandthereissufficientmemorytokeepallthepaths.Inothercases,aprogrammaticsolutionmustbefoundforlimitedresources:

oneexampleisconvolutionalencoding,wherethedecodermusttruncatethehistoryatadepthlargeenoughtokeepperformancetoanacceptablelevel.AlthoughtheViterbialgorithmisveryefficientandtherearemodificationsthatreducethecomputationalload,thememoryrequirementstendtoremainconstant.

Algorithm

SupposingwearegivenaHiddenMarkovModel(HMM)withstatesY,initialprobabilitiesπiofbeinginstateiandtransitionprobabilitiesai,joftransitioningfromstateitostatej.Sayweobserveoutputs

.Thestatesequence

mostlikelytohaveproducedtheobservationsisgivenbytherecurrencerelations:

[1]

HereVt,kistheprobabilityofthemostprobablestatesequenceresponsibleforthefirstt+1observations(weaddonebecauseindexingstartedat0)thathaskasitsfinalstate.TheViterbipathcanberetrievedbysavingbackpointerswhichrememberwhichstateywasusedinthesecondequation.LetPtr(k,t)bethefunctionthatreturnsthevalueofyusedtocomputeVt,kift>0,orkift=0.Then:

Thecomplexityofthisalgorithmis

.

Example

Considertwofriends,AliceandBob,wholivefarapartfromeachotherandwhotalktogetherdailyoverthetelephoneaboutwhattheydidthatday.Bobisonlyinterestedinthreeactivities:

walkinginthepark,shopping,andcleaninghisapartment.Thechoiceofwhattodoisdeterminedexclusivelybytheweatheronagivenday.AlicehasnodefiniteinformationabouttheweatherwhereBoblives,butsheknowsgeneraltrends.BasedonwhatBobtellsherhedideachday,Alicetriestoguesswhattheweathermusthavebeenlike.

AlicebelievesthattheweatheroperatesasadiscreteMarkovchain.Therearetwostates,"Rainy"and"Sunny",butshecannotobservethemdirectly,thatis,theyarehiddenfromher.Oneachday,thereisacertainchancethatBobwillperformoneofthefollowingactivities,dependingontheweather:

"walk","shop",or"clean".SinceBobtellsAliceabouthisactivities,thosearetheobservations.TheentiresystemisthatofahiddenMarkovmodel(HMM).

Aliceknowsthegeneralweathertrendsinthearea,andwhatBoblikestodoonaverage.Inotherwords,theparametersoftheHMMareknown.TheycanbewrittendowninthePythonprogramminglanguage:

states=('Rainy','Sunny')

observations=('walk','shop','clean')

start_probability={'Rainy':

0.6,'Sunny':

0.4}

transition_probability={

'Rainy':

{'Rainy':

0.7,'Sunny':

0.3},

'Sunny':

{'Rainy':

0.4,'Sunny':

0.6},

}

emission_probability={

'Rainy':

{'walk':

0.1,'shop':

0.4,'clean':

0.5},

'Sunny':

{'walk':

0.6,'shop':

0.3,'clean':

0.1},

}

Inthispieceofcode,start_probabilityrepresentsAlice'sbeliefaboutwhichstatetheHMMisinwhenBobfirstcallsher(allsheknowsisthatittendstoberainyonaverage).Theparticularprobabilitydistributionusedhereisnottheequilibriumone,whichis(giventhetransitionprobabilities)approximately{'Rainy':

0.57,'Sunny':

0.43}.Thetransition_probabilityrepresentsthechangeoftheweatherintheunderlyingMarkovchain.Inthisexample,thereisonlya30%chancethattomorrowwillbesunnyiftodayisrainy.Theemission_probabilityrepresentshowlikelyBobistoperformacertainactivityoneachday.Ifitisrainy,thereisa50%chancethatheiscleaninghisapartment;ifitissunny,thereisa60%chancethatheisoutsideforawalk.

AlicetalkstoBobthreedaysinarowanddiscoversthatonthefirstdayhewentforawalk,ontheseconddayhewentshopping,andonthethirddayhecleanedhisapartment.Alicehasaquestion:

whatisthemostlikelysequenceofrainy/sunnydaysthatwouldexplaintheseobservations?

ThisisansweredbytheViterbialgorithm.

#HelpsvisualizethestepsofViterbi.

defprint_dptable(V):

print"",

foriinrange(len(V)):

print"%7s"%("%d"%i),

print

foryinV[0].keys():

print"%.5s:

"%y,

fortinrange(len(V)):

print"%.7s"%("%f"%V[t][y]),

print

defviterbi(obs,states,start_p,trans_p,emit_p):

V=[{}]

path={}

#Initializebasecases(t==0)

foryinstates:

V[0][y]=start_p[y]*emit_p[y][obs[0]]

path[y]=[y]

#RunViterbifort>0

fortinrange(1,len(obs)):

V.append({})

newpath={}

foryinstates:

(prob,state)=max([(V[t-1][y0]*trans_p[y0][y]*emit_p[y][obs[t]],y0)fory0instates])

V[t][y]=prob

newpath[y]=path[state]+[y]

#Don'tneedtoremembertheoldpaths

path=newpath

print_dptable(V)

(prob,state)=max([(V[len(obs)-1][y],y)foryinstates])

return(prob,path[state])

Thefunctionviterbitakesthefollowingarguments:

obsisthesequenceofobservations,e.g.['walk','shop','clean'];statesisthesetofhiddenstates;start_pisthestartprobability;trans_parethetransitionprobabilities;andemit_paretheemissionprobabilities.Forsimplicityofcode,weassumethattheobservationsequenceobsisnon-emptyandthattrans_p[i][j]andemit_p[i][j]isdefinedforallstatesi,j.

Intherunningexample,theforward/Viterbialgorithmisusedasfollows:

defexample():

returnviterbi(observations,

states,

start_probability,

transition_probability,

emission_probability)

printexample()

Thisrevealsthattheobservations['walk','shop','clean']weremostlikelygeneratedbystates['Sunny','Rainy','Rainy'],withprobability0.01344.Inotherwords,giventheobservedactivities,itwasmostlikelysunnywhenBobwentforawalkandthenitstartedtorainthenextdayandkeptonraining.

TheoperationofViterbi'salgorithmcanbevisualizedbymeansofatrellisdiagram.TheViterbipathisessentiallytheshortestpaththroughthistrellis.Thetrellisfortheweatherexampleisshownbelow;thecorrespondingViterbipathisinbold:

WhenimplementingViterbi'salgorithm,itshouldbenotedthatmanylanguagesuseFloatingPointarithmetic-aspissmall,thismayleadtounderflowintheresults.Acommontechniquetoavoidthisistotakethelogarithmoftheprobabilitiesanduseitthroughoutthecomputation,thesametechniqueusedintheLogarithmicNumberSystem.Oncethealgorithmhasterminated,anaccuratevaluecanbeobtainedbyperformingtheappropriateexponentiation.

Ifyouwouldliketoimplementthisalgorithmsuchthatitcanacceptanynumberofstates,observations,andprobabilitiesfromthekeyboard,substitutetheparametersdeclarationpartwiththefollowingcode:

#TakesterminalinputtocreateHMM.

states=[]

number_of_states=input('Howmanystates?

Pleaseenteranintegerdifferentfrom0or1')

index=0

whileindex

states.append(str(raw_input('Giveanametothestatenumber'+str(index))))

index=index+1

observations=[]

number_of_observations=input('Howmanyobservations?

Pleaseenteranintegerdifferentfrom0or1')

index=0

whileindex

observations.append(str(raw_input('Giveanametotheobservationnumber'+str(index))))

index=index+1

start_probability={}

forstateinstates:

start_probability[state]=input('Giveavalueforthestartingprobabilityofthestate'+state)

transition_probability={}

forinitial_stateinstates:

transition_probability[initial_state]={}

forfinal_stateinstates:

transition_probability[initial_state][final_state]=input('Giveavalueforthetransitionprobabilityfromthestate'+initial_state+'tothestate'+final_state)

emission_probability={}

forstateinstates:

emission_probability[state]={}

for

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1