Ejemplo n.º 1
0
def test_outside():
    """
    Test outside chart properties.
    """
    for h in hypergraphs():
        w = utils.random_viterbi_potentials(h)
        path = ph.best_path(h, w)
        chart = ph.inside_values(h, w)
        best = w.dot(path)
        nt.assert_not_equal(best, 0.0)
        out_chart = ph.outside_values(h, w, chart)
        for node in h.nodes:
            other = chart[node] * out_chart[node]
            nt.assert_less_equal(other, best + 1e-4)
        for edge in path.edges:
            for node in edge.tail:
                if node.is_terminal:
                    nt.assert_almost_equal(other, best)
Ejemplo n.º 2
0
def test_semirings():
    for hypergraph in hypergraphs():
        potentials = ph.ViterbiPotentials(hypergraph).build(lambda l: 10.0)
        marg = ph.Viterbi.compute_marginals(hypergraph, potentials)

        log_potentials = ph.LogViterbiPotentials(hypergraph).build(lambda l: 10.0)
        potentials = ph.LogViterbiPotentials(hypergraph).build(lambda l: 10.0)
        chart = ph.inside(hypergraph, log_potentials)
        chart2 = ph.inside_values(hypergraph, potentials)
        for node in hypergraph.nodes:
            nt.assert_equal(chart[node], chart2[node])

        marg = ph.LogViterbi.compute_marginals(hypergraph, log_potentials)
        marg2 = ph.compute_marginals(hypergraph, potentials)
        for edge in hypergraph.edges:
            nt.assert_almost_equal(marg[edge], marg2[edge])


        potentials = ph.Inside.Potentials(hypergraph).build(lambda l: 0.5)
        chart = ph.Inside.inside(hypergraph, potentials)

        potentials = ph.Inside.Potentials(hypergraph).build(lambda l: 0.5)