def test_chain():
    # test LP, AD3, AD3-BB and JT on a chain.
    # they should all be exact
    rnd = np.random.RandomState(0)
    for i in xrange(10):
        forward = np.c_[np.arange(9), np.arange(1, 10)]
        backward = np.c_[np.arange(1, 10), np.arange(9)]
        unary_potentials = rnd.normal(size=(10, 3))
        pairwise_potentials = rnd.normal(size=(3, 3))
        # test that reversing edges is same as transposing pairwise potentials
        y_forward = inference_lp(unary_potentials, pairwise_potentials,
                                 forward)
        y_backward = inference_lp(unary_potentials, pairwise_potentials.T,
                                  backward)
        assert_array_equal(y_forward, y_backward)
        for chain in [forward, backward]:
            y_lp = inference_lp(unary_potentials, pairwise_potentials, chain)
            try:
                from pystruct.inference import inference_dai
                y_dai = inference_dai(unary_potentials, pairwise_potentials,
                                      chain, alg='jt')
                assert_array_equal(y_dai, y_lp)
            except:
                pass
            try:
                from pystruct.inference import inference_ad3

                y_ad3 = inference_ad3(unary_potentials, pairwise_potentials,
                                      chain)
                y_ad3bb = inference_ad3(unary_potentials, pairwise_potentials,
                                        chain, branch_and_bound=True)
                assert_array_equal(y_ad3, y_lp)
                assert_array_equal(y_ad3bb, y_lp)
            except:
                pass
Example #2
0
def test_pystruct():

    unaries = np.zeros((3, 5))
    unaries[1, 2] = 2
    pairwise = np.eye(5)
    edges = np.array([[0, 1], [1, 2], [0, 2]], dtype=np.intp)

    # no parameters
    labels = inference_ad3(unaries, pairwise, edges)
    assert_array_equal(labels, [2, 2, 2])

    # exact decoding
    labels_exact = inference_ad3(unaries, pairwise, edges,
                                 branch_and_bound=True)
    assert_array_equal(labels_exact, [2, 2, 2])

    # request energy
    labels, energy = inference_ad3(unaries, pairwise, edges,
                                   return_energy=True)
    assert_array_equal(energy, -5)

    # exact decoding and request energy
    labels, energy = inference_ad3(unaries, pairwise, edges,
                                   branch_and_bound=True, return_energy=True)
    assert_array_equal(energy, -5)
Example #3
0
def test_pystruct():

    unaries = np.zeros((3, 5))
    unaries[1, 2] = 2
    pairwise = np.eye(5)
    edges = np.array([[0, 1], [1, 2], [0, 2]], dtype=np.intp)

    # no parameters
    labels = inference_ad3(unaries, pairwise, edges)
    assert_array_equal(labels, [2, 2, 2])

    # exact decoding
    labels_exact = inference_ad3(unaries,
                                 pairwise,
                                 edges,
                                 branch_and_bound=True)
    assert_array_equal(labels_exact, [2, 2, 2])

    # request energy
    labels, energy = inference_ad3(unaries,
                                   pairwise,
                                   edges,
                                   return_energy=True)
    assert_array_equal(energy, -5)

    # exact decoding and request energy
    labels, energy = inference_ad3(unaries,
                                   pairwise,
                                   edges,
                                   branch_and_bound=True,
                                   return_energy=True)
    assert_array_equal(energy, -5)
Example #4
0
def test_iterative_max_product_chain():
    rnd = np.random.RandomState(0)
    chain = np.c_[np.arange(9), np.arange(1, 10)]
    for i in xrange(10):
        unary_potentials = rnd.normal(size=(10, 3))
        pairwise_potentials = rnd.normal(size=(9, 3, 3))
        result_ad3 = inference_ad3(unary_potentials, pairwise_potentials, chain, branch_and_bound=True)
        result_mp = iterative_max_product(unary_potentials, pairwise_potentials, chain)
        assert_array_equal(result_ad3, result_mp)
Example #5
0
def test_tree_max_product_chain():
    rnd = np.random.RandomState(0)
    forward = np.c_[np.arange(9), np.arange(1, 10)]
    backward = np.c_[np.arange(1, 10), np.arange(9)]
    for i in range(10):
        unary_potentials = rnd.normal(size=(10, 3))
        pairwise_potentials = rnd.normal(size=(9, 3, 3))
        for chain in [forward, backward]:
            result_ad3 = inference_ad3(unary_potentials, pairwise_potentials, chain, branch_and_bound=True)
            result_mp = inference_max_product(unary_potentials, pairwise_potentials, chain)
            assert_array_equal(result_ad3, result_mp)
Example #6
0
def test_iterative_max_product_chain():
    rnd = np.random.RandomState(0)
    chain = np.c_[np.arange(9), np.arange(1, 10)]
    for i in xrange(10):
        unary_potentials = rnd.normal(size=(10, 3))
        pairwise_potentials = rnd.normal(size=(9, 3, 3))
        result_ad3 = inference_ad3(unary_potentials,
                                   pairwise_potentials,
                                   chain,
                                   branch_and_bound=True)
        result_mp = iterative_max_product(unary_potentials,
                                          pairwise_potentials, chain)
        assert_array_equal(result_ad3, result_mp)
Example #7
0
def test_tree_max_product_chain():
    rnd = np.random.RandomState(0)
    forward = np.c_[np.arange(9), np.arange(1, 10)]
    backward = np.c_[np.arange(1, 10), np.arange(9)]
    for i in range(10):
        unary_potentials = rnd.normal(size=(10, 3))
        pairwise_potentials = rnd.normal(size=(9, 3, 3))
        for chain in [forward, backward]:
            result_ad3 = inference_ad3(unary_potentials,
                                       pairwise_potentials,
                                       chain,
                                       branch_and_bound=True)
            result_mp = inference_max_product(unary_potentials,
                                              pairwise_potentials, chain)
            assert_array_equal(result_ad3, result_mp)
Example #8
0
    def predict(self, X,y=None,w=1):
        p1 = self.p1
        for i in p1:p1[i].value = T.shared(p1[i].value)

        edges = self.edges

        pdf = self._pdf(p1, X)
        theta = p1['theta'].value

        X = node_potn(pdf).eval()*self.w_nodes
        F = edge_potn(pdf, self._copula, theta, edges,shared_copula=self.shared_copula).dimshuffle(1,0,2,3).eval()*(1-self.w_nodes)
        E = edges.eval()

        y_hat = np.array([inference_ad3(-x,-f,E.T) for x,f in zip(X,F)])

        return y_hat
Example #9
0
def test_iterative_max_product_tree():
    try:
        from scipy.sparse.csgraph import minimum_spanning_tree
    except:
        raise SkipTest("Not testing trees, scipy version >= 0.11 required")
    rnd = np.random.RandomState(0)
    for i in xrange(100):
        # generate random tree using mst
        graph = rnd.uniform(size=(10, 10))
        tree = minimum_spanning_tree(sparse.csr_matrix(graph))
        tree_edges = np.c_[tree.nonzero()]

        unary_potentials = rnd.normal(size=(10, 3))
        pairwise_potentials = rnd.normal(size=(9, 3, 3))
        result_ad3 = inference_ad3(unary_potentials, pairwise_potentials, tree_edges, branch_and_bound=True)
        result_mp = iterative_max_product(unary_potentials, pairwise_potentials, tree_edges)
    assert_array_equal(result_ad3, result_mp)
Example #10
0
def test_iterative_max_product_tree():
    try:
        from scipy.sparse.csgraph import minimum_spanning_tree
    except:
        raise SkipTest("Not testing trees, scipy version >= 0.11 required")
    rnd = np.random.RandomState(0)
    for i in xrange(100):
        # generate random tree using mst
        graph = rnd.uniform(size=(10, 10))
        tree = minimum_spanning_tree(sparse.csr_matrix(graph))
        tree_edges = np.c_[tree.nonzero()]

        unary_potentials = rnd.normal(size=(10, 3))
        pairwise_potentials = rnd.normal(size=(9, 3, 3))
        result_ad3 = inference_ad3(unary_potentials,
                                   pairwise_potentials,
                                   tree_edges,
                                   branch_and_bound=True)
        result_mp = iterative_max_product(unary_potentials,
                                          pairwise_potentials, tree_edges)
    assert_array_equal(result_ad3, result_mp)