Пример #1
0
def test_inside():
    """
    Test inside chart gen.
    """
    for h in hypergraphs():
        w = utils.random_inside_potentials(h)
        inside = ph.inside(h, w)
Пример #2
0
def test_future_constraints():
    """
    Test constraint checking.
    """
    hypergraph = simple_hypergraph()


    def build_constraints(label):
        if label == "1":
            return [("one", 1)]
        return []
    constraints = cons.Constraints(hypergraph, [("one", -1)]). \
        build(build_constraints)

    # Compute min and max potentials.
    min_potentials = ph.MinSparseVectorPotentials(hypergraph).\
        from_potentials(constraints.potentials)
    max_potentials = ph.MaxSparseVectorPotentials(hypergraph).\
        from_potentials(constraints.potentials)

    print "sparse"
    for edge in hypergraph.edges:
        print edge.label, constraints.potentials[edge]

    # Compute min and max potentials.
    print "min"
    in_chart = ph.inside(hypergraph, min_potentials)
    out_chart = ph.outside(hypergraph, min_potentials, in_chart)
    for node in hypergraph.nodes:
        print "%20s %20s %20s"%(node.label, in_chart[node], out_chart[node])

    print "max"
    in_chart = ph.inside(hypergraph, max_potentials)
    out_chart = ph.outside(hypergraph, max_potentials, in_chart)
    for node in hypergraph.nodes:
        print "%20s %20s %20s"%(node.label, in_chart[node], out_chart[node])
Пример #3
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)
Пример #4
0
def test_posteriors():
    "Check the posteriors by enumeration."
    for h in hypergraphs():
        w = utils.random_inside_potentials(h)
        marg = ph.compute_marginals(h, w)


        paths = utils.all_paths(h)
        m = defaultdict(lambda: 0.0)
        total_score = 0.0
        for path in paths:
            path_score = w.dot(path)
            total_score += path_score
            for edge in path:
                m[edge.id] += path_score

        for edge in h.edges:
            nt.assert_almost_equal(
                marg[edge] / marg[h.root],
                m[edge.id] / total_score, places=4)

        chart = ph.inside(h, w)
        nt.assert_almost_equal(chart[h.root], total_score, places=4)
Пример #5
0
    def sum_potentials(self):
	if not self.potentials:
           self.get_potentials()
	chart =  ph.inside(self.hypergraph, self.potentials)
	self.total_potentials = chart[self.hypergraph.root]