我正在尝试在Matlab中实现Viterbi算法,并且出于某种原因,在我的第一次传递之后我的感应部分将alpha变为0。 V =
0.7000 0
0.0980 0
V =
0.7000 0
0 0
V =
0.7000 0
0 0.1680
V =
0.7000 0
0 0
另外,BestPath只会产生一堆0,最后只有一个,我不知道为什么。
我的代码:
function [ViterbiScore,BestPath] = Viterbi(A,B,Pi,Obv)
%INPUTS:
%Obv = Observation sequence
%A = Transition probability matrix
%B = Emission matrix
%Pi = initial probability matrix
%OUTPUT:
%ViterbiScore
%BestPath = Best-state sequence as a vector of complex numbers.
T=length(Obv);
N=length(A(1,:));
%Initilization
for i=1:N
V(1,i)=Pi(i)*B(i,Obv(1));
BT(1,i)=0;
end
%Induction
for t=2:T
for j=1:N
for i=1:N
V(t,j)=max(V(t-1,i)*A(i,j))*B(j,Obv(t))
[rndm,BT(t,j)]=max(V(t-1,i)*A(i,j));
end
end
end
%Termination
for i=1:N
[ViterbiScore,BestPath(T)] = max(V(T,i));
end
for t=(T-1):1
BestPath(t)=BT(t+1,BestPath(t+1));
end