后向算法隐马尔可夫模型,第 0 个索引(终止步骤)产生错误结果

Backward algorithm Hidden Markov Model, 0th index (termination step) yields wrong result

我正在 PyTorch 中实现后向 HMM 算法。我用这个 link 作为参考。 link 包含所用数值示例的结果(我正在尝试实现它并将生成的结果与它进行比较)。 Page 3, section 2. Backward probability, 有一个table包含计算结果。

这是我的代码:

# Initial Transition matrix as shown in page 2 of above link
A = np.array([[0.6, 0.4], [0.3, 0.7]])
A = torch.from_numpy(A)
# Initial State Probability (page 2)
pi = np.array([0.8, 0.2])
pi = torch.from_numpy(pi)
# Output probabilities (page 2)
emission_matrix = np.array([[0.3, 0.4, 0.3, 0.3], [0.4, 0.3, 0.3, 0.3]])
emission_matrix = torch.from_numpy(emission_matrix)
# Initialize empty 2x4 matrix (dimensions of emission matrix)
backward = torch.zeros(emission_matrix.shape, dtype=torch.float64)

# Backward algorithm
def _backward(emission_matrix):       
    # Initialization: A(i, j) * B(T, i) * B(Ot+1, j) , where B(Ot+1, j)  = 1
    backward[:, -1] = torch.matmul(A, emission_matrix[:, -1])
    # I reversed the emission matrix so as to start from the last column
    rev_emission_mat = torch.flip(emission_matrix[:, :-1], [1])
    # I transposed the reversed emission matrix such that each iterable in the for 
    # loop is the observation sequence probability
    T_rev_emission_mat = torch.transpose(rev_emission_mat, 1, 0)
    # This step is so that I assign a reverse index enumeration to each iterable in the
    # emission matrix starts from time T to 0, rather than the opposite
    zipped_cols = list(zip(range(len(T_rev_emission_mat)-1, -1, -1), T_rev_emission_mat))

    for i, obs_prob in zipped_cols:
        # Induction: Σ A(i, j) * B(j)(Ot+1) * β(t+1, j)   
        if i != 0:
            backward[:, i] = torch.matmul(A * obs_prob, backward[:, i+1])      
    # Termination: Σ π(i) * bi * β(1, i)
    backward[:, 0] = torch.matmul(pi * obs_prob, backward[:, 1])

# run backward algorithm
_backward(emission_matrix)
# check results, backward is an all zero matrix that was initialized above
print(backward)
>>> tensor([[0.0102, 0.0324, 0.0900, 0.3000],
           [0.0102, 0.0297, 0.0900, 0.3000]], dtype=torch.float64)

如您所见,第0个索引与上一个link的第3页中的结果不匹配。我做错了什么?如果有什么我可以澄清的,请告诉我。提前致谢!

backward[:, 0] = pi * obs_prob * backward[:, 1]