Пример #1
0
def check_best_path(graph, max_potentials):
    """
    Test viterbi path finding.
    """
    path = pydecode.best_path(graph, max_potentials)
    nt.assert_not_equal(max_potentials.T * path.v, 0.0)
    utils.valid_path(graph, path)
    same = False
    for other_path in utils.all_paths(graph):
        assert max_potentials.T * path.v >= max_potentials.T * other_path.v
        if path == other_path:
            same = True
    assert same
Пример #2
0
def check_posteriors(graph, pot):
    """
    Check the posteriors by enumeration.
    """

    node_marg = pydecode.marginals(graph, pot)

    paths = utils.all_paths(graph)
    m = defaultdict(lambda: 0.0)
    total_score = 0.0
    for path in paths:
        #path_score = prod([pot[edge.id] for edge in path.edges])
        path_score = np.exp(np.log(pot.T) * path.v)
        total_score += path_score
        for edge in path:
            m[edge.id] += path_score