前提
import torch
import torch.nn.functional as F
N = 3 # 离散隐变量可以取到的值的个数
M = 2 # 可观测变量个数
pi = F.softmax(torch.randn((N,1),dtype=torch.float32),dim=0) # 初始状态概率矩阵
A = F.softmax(torch.randn((N,N),dtype=torch.float32),dim=-1) # 转移矩阵
B = F.softmax(torch.randn((N,M),dtype=torch.float32),dim=-1) # 发射矩阵
O_set = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]] # 观测变量序列
re = []
一.前向算法(迭代求解)
re2 = []
for O in O_set:forward = pi * B[:,[O[0]]]for t in range(1,len(O)):forward = torch.matmul(A.transpose(0,1),forward)*B[:,[O[t]]]forward = torch.sum(forward,dim=0)re2.append(forward.item())
print(torch.sum(torch.tensor(re2)))
输出为1,验证了算法的正确性。
二.后向算法(迭代求解)
for O in O_set:backward = torch.ones_like(pi,dtype=torch.float32)for t in range(len(O)-1,0,-1):observe_idx = O[t]backward = torch.matmul(A,backward * B[:,[O[t]]])backward = torch.sum(backward * pi * B[:,O[0]].unsqueeze(-1),dim=0)re.append(backward.item())
print(torch.sum(torch.tensor(re)))
输出为1,验证了算法的正确性。