Example #1
0
def check_outside(graph, pot):
    """
    Test outside chart properties.
    """
    print graph
    path = pydecode.best_path(graph, pot)
    chart = pydecode.inside(graph, pot)
    print pot.shape, path.v.shape
    best = pot.T * path.v
    print path.v
    print best
    nt.assert_almost_equal(best, chart[graph.root.id])
    nt.assert_not_equal(best, 0.0)

    out_chart = pydecode.outside(graph, pot, chart)

    # Array-form
    for vertex in graph.vertices:
        other = chart[vertex.id] + out_chart[vertex.id]
        nt.assert_less_equal(other, best + 1e-4,
                             "%f %f %d %f %f"%(other, best, vertex.id,
                                         chart[vertex.id], out_chart[vertex.id]))

    # Matrix-form
    m = chart + out_chart
    assert (m < best + 1e4).all()

    # for node in graph.nodes:
    #     other = chart[node] * out_chart[node]
    #     nt.assert_less_equal(other, best + 1e-4)
    print chart
    print out_chart

    for edge in path.edges:
        for node in edge.tail:
            if node.is_terminal:
                other = out_chart[node.id]
                nt.assert_almost_equal(other, best)
Example #2
0
    def special_decode(self, method, problem, hypergraph, scores, constraints,
                       scorer):
        if method == "CUBE":
            groups = [node.label.i for node in hypergraph.nodes]
            ins = ph.inside(hypergraph, scores)
            out = ph.outside(hypergraph, scores, ins)

            beam_chart = ph.beam_search_BinaryVector(
                hypergraph, scores, constraints.to_binary_potentials(),
                out, -10000, groups, [1000] * len(groups), cube_pruning=True)
            return beam_chart.path(0)

        elif method == "BEAM":
            groups = [node.label.i for node in hypergraph.nodes]
            ins = ph.inside(hypergraph, scores)
            out = ph.outside(hypergraph, scores, ins)

            beam_chart = ph.beam_search_BinaryVector(
                hypergraph, scores, constraints.to_binary_potentials(),
                out, -10000, groups, [1000] * len(groups))
            return beam_chart.path(0)
        elif method == "MULTIDFA":
            old = hypergraph
            old_hmap = None

            for j in range(problem.size):
                states = 2
                symbols = 2
                dfa = ph.DFA(states, symbols, [{0:0, 1:1} , {0:1}], [1])
                vec = [(1 if (edge.head.label.j == j) else 0)
                       for edge in old.edges]
                counts = ph.CountingPotentials(old).from_vector(vec)
                hmap = ph.extend_hypergraph_by_dfa(old, counts, dfa)
                old = hmap.domain_hypergraph
                old.labeling = ph.Labeling(old, [hmap[node].label
                                                 for node in old.nodes],
                                           None)
                #new_scores = old_scores.up_project(old, hmap)
                if old_hmap is not None:
                    old_hmap = old_hmap.compose(hmap)
                else:
                    old_hmap = hmap
                # old_scores = new_scores
            new_scores = scores.up_project(old, old_hmap)
            #new_scores = self.potentials(old, scorer)
            return ph.best_path(old, new_scores)

        elif method == "BIGDFA":
            old = hypergraph
            states = 2**problem.size
            symbols = problem.size + 1
            final_state = 0
            for i in range(problem.size):
                final_state |= 2**i

            transitions = \
                [{j : i | 2**j for j in range(symbols) if i & 2**j == 0}
                 for i in range(states)]
            dfa = ph.DFA(states, symbols,
                         transitions,
                         [final_state])
            vec = [edge.head.label.j for edge in old.edges]
            counts = ph.CountingPotentials(old).from_vector(vec)
            hmap = ph.extend_hypergraph_by_dfa(old, counts, dfa)
            old = hmap.domain_hypergraph
            old.labeling = ph.Labeling(old, [hmap[node].label
                                             for node in old.nodes],
                                       None)
            new_scores = scores.up_project(old, hmap)
            return ph.best_path(old, new_scores)
Example #3
0
    def special_decode(self, method, problem, hypergraph, scores, constraints,
                       scorer):
        if method == "CUBE":
            groups = [node.label.i for node in hypergraph.nodes]
            ins = ph.inside(hypergraph, scores)
            out = ph.outside(hypergraph, scores, ins)

            beam_chart = ph.beam_search_BinaryVector(
                hypergraph,
                scores,
                constraints.to_binary_potentials(),
                out,
                -10000,
                groups, [1000] * len(groups),
                cube_pruning=True)
            return beam_chart.path(0)

        elif method == "BEAM":
            groups = [node.label.i for node in hypergraph.nodes]
            ins = ph.inside(hypergraph, scores)
            out = ph.outside(hypergraph, scores, ins)

            beam_chart = ph.beam_search_BinaryVector(
                hypergraph, scores, constraints.to_binary_potentials(), out,
                -10000, groups, [1000] * len(groups))
            return beam_chart.path(0)
        elif method == "MULTIDFA":
            old = hypergraph
            old_hmap = None

            for j in range(problem.size):
                states = 2
                symbols = 2
                dfa = ph.DFA(states, symbols, [{0: 0, 1: 1}, {0: 1}], [1])
                vec = [(1 if (edge.head.label.j == j) else 0)
                       for edge in old.edges]
                counts = ph.CountingPotentials(old).from_vector(vec)
                hmap = ph.extend_hypergraph_by_dfa(old, counts, dfa)
                old = hmap.domain_hypergraph
                old.labeling = ph.Labeling(
                    old, [hmap[node].label for node in old.nodes], None)
                #new_scores = old_scores.up_project(old, hmap)
                if old_hmap is not None:
                    old_hmap = old_hmap.compose(hmap)
                else:
                    old_hmap = hmap
                # old_scores = new_scores
            new_scores = scores.up_project(old, old_hmap)
            #new_scores = self.potentials(old, scorer)
            return ph.best_path(old, new_scores)

        elif method == "BIGDFA":
            old = hypergraph
            states = 2**problem.size
            symbols = problem.size + 1
            final_state = 0
            for i in range(problem.size):
                final_state |= 2**i

            transitions = \
                [{j : i | 2**j for j in range(symbols) if i & 2**j == 0}
                 for i in range(states)]
            dfa = ph.DFA(states, symbols, transitions, [final_state])
            vec = [edge.head.label.j for edge in old.edges]
            counts = ph.CountingPotentials(old).from_vector(vec)
            hmap = ph.extend_hypergraph_by_dfa(old, counts, dfa)
            old = hmap.domain_hypergraph
            old.labeling = ph.Labeling(
                old, [hmap[node].label for node in old.nodes], None)
            new_scores = scores.up_project(old, hmap)
            return ph.best_path(old, new_scores)