0

当我在 HMM 中制作模型时遇到问题 AssertionError 但无法弄清楚问题是什么?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from helpers import show_model
from pomegranate import State, HiddenMarkovModel, DiscreteDistribution



model = HiddenMarkovModel(name='Example Model')

#Emission probabiliy of distributions, p(umbrella|weather)
sunny_emission = DiscreteDistribution({'yes':0.1, 'no':0.8})
sunny_state = State(sunny_emission, name='Sunny')

rainy_emission = DiscreteDistribution({'yes':0.8, 'no':0.2})
rainy_state = State(rainy_emission, name='rainy')


model.add_states(sunny_state, rainy_state)

assert rainy_emission.probability('yes') == 0.8, "The director brings his umbrella with probability 0.8 on rainy days"
print("looks good so far")

model.add_transition(model.start, sunny_state, 0.5)
model.add_transition(model.start, rainy_state, 0.5)


model.add_transition(sunny_state, sunny_state, 0.8) # 80%sunny days
model.add_transition(rainy_state, rainy_state, 0.2) # 20% rainy days


model.add_transition(rainy_state, rainy_state, 0.6)
model.add_transition(rainy_state, sunny_state, 0.4)

model.bake()


assert model.edge_count() == 6, "There should be two edges from model.start, two from Rainy, and two from Sunny"
assert model.node_count() == 4, "The states should include model.start, model.end, Rainy, and Sunny"

AssertionError: model.start 应该有两条边,Rainy 有两条,Sunny 有两条

4

1 回答 1

0

您收到该错误的原因是,您的代码中只有 5 个来自隐藏状态('SUNNY')的边,但 assert 语句正在检查 6 个边。

**model.add_transition(rainy_state, rainy_state, 0.2) # 20% rainy days**

这是错误所在的行,这里的过渡是从“下雨到下雨”而不是“从晴到下雨”。

于 2020-05-05T07:19:15.490 回答