Example #1
0
def test_continuous_y():
    # for inference_method in ["lp", "ad3"]:
    for inference_method in ["lp"]:
        X, Y = toy.generate_blocks(n_samples=1)
        x, y = X[0], Y[0]
        w = np.array([1, 1, 0, -4, 0])

        crf = GridCRF(inference_method=inference_method)
        psi = crf.psi(x, y)
        y_cont = np.zeros_like(x)
        gx, gy = np.indices(x.shape[:-1])
        y_cont[gx, gy, y] = 1
        # need to generate edge marginals
        vert = np.dot(y_cont[1:, :, :].reshape(-1, 2).T, y_cont[:-1, :, :].reshape(-1, 2))
        # horizontal edges
        horz = np.dot(y_cont[:, 1:, :].reshape(-1, 2).T, y_cont[:, :-1, :].reshape(-1, 2))
        pw = vert + horz

        psi_cont = crf.psi(x, (y_cont, pw))
        assert_array_almost_equal(psi, psi_cont)

        const = find_constraint(crf, x, y, w, relaxed=False)
        const_cont = find_constraint(crf, x, y, w, relaxed=True)

        # dpsi and loss are equal:
        assert_array_almost_equal(const[1], const_cont[1])
        assert_almost_equal(const[2], const_cont[2])

        # returned y_hat is one-hot version of other
        assert_array_equal(const[0], np.argmax(const_cont[0][0], axis=-1))

        # test loss:
        assert_equal(crf.loss(y, const[0]), crf.continuous_loss(y, const_cont[0][0]))
Example #2
0
def test_binary_blocks_crf_n8_lp():
    X, Y = toy.generate_blocks(n_samples=1, noise=1)
    x, y = X[0], Y[0]
    w = np.array([1, 1, 1, -1.4, 1])
    crf = GridCRF(inference_method="lp", neighborhood=8)
    y_hat = crf.inference(x, w)
    assert_array_equal(y, y_hat)
Example #3
0
def test_binary_blocks_one_slack_graph():
    #testing cutting plane ssvm on easy binary dataset
    # generate graphs explicitly for each example
    for inference_method in ["dai", "lp"]:
        print("testing %s" % inference_method)
        X, Y = toy.generate_blocks(n_samples=3)
        crf = GraphCRF(inference_method=inference_method)
        clf = OneSlackSSVM(problem=crf, max_iter=100, C=100, verbose=100,
                           check_constraints=True, break_on_bad=True,
                           n_jobs=1)
        x1, x2, x3 = X
        y1, y2, y3 = Y
        n_states = len(np.unique(Y))
        # delete some rows to make it more fun
        x1, y1 = x1[:, :-1], y1[:, :-1]
        x2, y2 = x2[:-1], y2[:-1]
        # generate graphs
        X_ = [x1, x2, x3]
        G = [make_grid_edges(x) for x in X_]

        # reshape / flatten x and y
        X_ = [x.reshape(-1, n_states) for x in X_]
        Y = [y.ravel() for y in [y1, y2, y3]]

        X = zip(X_, G)

        clf.fit(X, Y)
        Y_pred = clf.predict(X)
        for y, y_pred in zip(Y, Y_pred):
            assert_array_equal(y, y_pred)
Example #4
0
def test_binary_blocks_crf():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1, 1, 0, -4, 0])
    crf = GridCRF()
    y_hat = crf.inference(x, w)
    assert_array_equal(y, y_hat)
Example #5
0
def test_blocks_crf_directional():
    # test latent directional CRF on blocks
    # test that all results are the same as equivalent LatentGridCRF
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    pairwise_weights = np.array([0, 0, 0, -4, -4, 0, -4, -4, 0, 0])
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    pw_directional = np.array(
        [0, 0, -4, -4, 0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0, 0, 0, -4, -4, 0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0]
    )
    w_directional = np.hstack([unary_weights.ravel(), pw_directional])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    directional_crf = LatentDirectionalGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    h_hat_d = directional_crf.inference(x, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    h = crf.latent(x, y, w)
    h_d = directional_crf.latent(x, y, w_directional)
    assert_array_equal(h, h_d)

    h_hat = crf.loss_augmented_inference(x, y, w)
    h_hat_d = directional_crf.loss_augmented_inference(x, y, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    psi = crf.psi(x, h_hat)
    psi_d = directional_crf.psi(x, h_hat)
    assert_array_equal(np.dot(psi, w), np.dot(psi_d, w_directional))
Example #6
0
def test_binary_blocks():
    X, Y = toy.generate_blocks(n_samples=10)
    crf = GridCRF()
    clf = StructuredPerceptron(problem=crf, max_iter=40)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Example #7
0
def test_blocks_crf_unaries():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(h_hat / 2, np.argmax(x, axis=-1))
def test_binary_blocks_subgradient():
    #testing subgradient ssvm on easy binary dataset
    X, Y = toy.generate_blocks(n_samples=10)
    crf = GridCRF()
    clf = SubgradientSSVM(model=crf, max_iter=200, C=100, learning_rate=0.1)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_binary_blocks_perceptron_online():
    #testing subgradient ssvm on easy binary dataset
    X, Y = toy.generate_blocks(n_samples=10)
    crf = GridCRF()
    clf = StructuredPerceptron(model=crf, max_iter=20)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_binary_blocks_batches_n_slack():
    #testing cutting plane ssvm on easy binary dataset
    X, Y = toy.generate_blocks(n_samples=5)
    crf = GridCRF()
    clf = NSlackSSVM(model=crf, max_iter=20, C=100, check_constraints=True,
                     break_on_bad=False, n_jobs=1, batch_size=1)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Example #11
0
def test_blocks_crf_unaries():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    pairwise_weights = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(h_hat / 2, np.argmax(x, axis=-1))
Example #12
0
def test_binary_blocks_subgradient():
    #testing subgradient ssvm on easy binary dataset
    X, Y = toy.generate_blocks(n_samples=10)
    crf = GridCRF()
    clf = SubgradientStructuredSVM(problem=crf, max_iter=200, C=100,
                                   verbose=10, learning_rate=0.1, n_jobs=-1)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Example #13
0
def test_blocks_crf():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1, 1, 1, 1, 0, 0, 0, -4, -4, 0, -4, -4, 0, 0])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(y, h_hat / 2)

    h = crf.latent(x, y, w)
    assert_equal(crf.loss(h, h_hat), 0)
Example #14
0
def test_binary_ssvm_attractive_potentials():
    # test that submodular SSVM can learn the block dataset
    X, Y = toy.generate_blocks(n_samples=10)
    crf = GridCRF()
    submodular_clf = StructuredSVM(problem=crf, max_iter=200, C=100,
                                   verbose=1, check_constraints=True,
                                   positive_constraint=[3])
    submodular_clf.fit(X, Y)
    Y_pred = submodular_clf.predict(X)
    assert_array_equal(Y, Y_pred)
def test_binary_blocks_cutting_plane():
    #testing cutting plane ssvm on easy binary dataset
    for inference_method in get_installed(["dai", "lp", "qpbo", "ad3"]):
        X, Y = toy.generate_blocks(n_samples=5)
        crf = GridCRF(inference_method=inference_method)
        clf = NSlackSSVM(model=crf, max_iter=20, C=100,
                         check_constraints=True, break_on_bad=False)
        clf.fit(X, Y)
        Y_pred = clf.predict(X)
        assert_array_equal(Y, Y_pred)
Example #16
0
def test_binary_blocks_batches_n_slack():
    #testing cutting plane ssvm on easy binary dataset
    X, Y = toy.generate_blocks(n_samples=5)
    crf = GridCRF(inference_method='lp')
    clf = StructuredSVM(problem=crf, max_iter=20, C=100, verbose=0,
                        check_constraints=True, break_on_bad=False,
                        n_jobs=1, batch_size=1)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
Example #17
0
def test_binary_blocks_crf():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1, 0,  # unary
                  0, 1,
                  0,     # pairwise
                  -4, 0])
    for inference_method in ['dai', 'qpbo', 'lp', 'ad3']:
        crf = GridCRF(inference_method=inference_method)
        y_hat = crf.inference(x, w)
        assert_array_equal(y, y_hat)
def test_binary_ssvm_attractive_potentials():
    # test that submodular SSVM can learn the block dataset
    X, Y = toy.generate_blocks(n_samples=10)
    crf = GridCRF()
    submodular_clf = NSlackSSVM(model=crf, max_iter=200, C=100,
                                check_constraints=True,
                                positive_constraint=[5])
    submodular_clf.fit(X, Y)
    Y_pred = submodular_clf.predict(X)
    assert_array_equal(Y, Y_pred)
    assert_true(submodular_clf.w[5] < 0)  # don't ask me about signs
Example #19
0
def test_binary_blocks_cutting_plane():
    #testing cutting plane ssvm on easy binary dataset
    for inference_method in ["dai", "lp", "qpbo", "ad3"]:
        X, Y = toy.generate_blocks(n_samples=5)
        crf = GridCRF(inference_method=inference_method)
        clf = StructuredSVM(problem=crf, max_iter=20, C=100, verbose=0,
                            check_constraints=True, break_on_bad=False,
                            n_jobs=-1)
        clf.fit(X, Y)
        Y_pred = clf.predict(X)
        assert_array_equal(Y, Y_pred)
Example #20
0
def test_blocks_crf():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    pairwise_weights = np.array([0, 0, 0, -4, -4, 0, -4, -4, 0, 0])
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(y, h_hat / 2)

    h = crf.latent(x, y, w)
    assert_equal(crf.loss(h, h_hat), 0)
Example #21
0
def test_loss_augmentation():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1, 0,  # unary
                  0, 1,
                  0,     # pairwise
                  -4, 0])
    crf = GridCRF(inference_method='lp')
    y_hat, energy = crf.loss_augmented_inference(x, y, w, return_energy=True)

    assert_almost_equal(energy + crf.loss(y, y_hat),
                        -np.dot(w, crf.psi(x, y_hat)))
def test_binary_blocks_cutting_plane_latent_node():
    #testing cutting plane ssvm on easy binary dataset
    # we use the LatentNodeCRF without latent nodes and check that it does the
    # same as GraphCRF
    X, Y = toy.generate_blocks(n_samples=3)
    crf = GraphCRF(inference_method='lp')
    clf = StructuredSVM(model=crf, max_iter=20, C=100, verbose=0,
                        check_constraints=True, break_on_bad=False,
                        n_jobs=1)
    x1, x2, x3 = X
    y1, y2, y3 = Y
    n_states = len(np.unique(Y))
    # delete some rows to make it more fun
    x1, y1 = x1[:, :-1], y1[:, :-1]
    x2, y2 = x2[:-1], y2[:-1]
    # generate graphs
    X_ = [x1, x2, x3]
    G = [make_grid_edges(x) for x in X_]

    # reshape / flatten x and y
    X_ = [x.reshape(-1, n_states) for x in X_]
    Y = [y.ravel() for y in [y1, y2, y3]]

    X = zip(X_, G)

    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    for y, y_pred in zip(Y, Y_pred):
        assert_array_equal(y, y_pred)

    latent_crf = LatentNodeCRF(n_labels=2, inference_method='lp',
                               n_hidden_states=0)
    latent_svm = LatentSSVM(StructuredSVM(model=latent_crf, max_iter=20,
                                          C=100, verbose=0,
                                          check_constraints=True,
                                          break_on_bad=False, n_jobs=1),
                            latent_iter=3)
    X_latent = zip(X_, G, np.zeros(len(X_)))
    latent_svm.fit(X_latent, Y, H_init=Y)
    Y_pred = latent_svm.predict(X_latent)
    for y, y_pred in zip(Y, Y_pred):
        assert_array_equal(y, y_pred)

    assert_array_almost_equal(latent_svm.w, clf.w)
Example #23
0
def test_loss_augmentation():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    w = np.array([1.0, 1.0, 0.0, -4.0, 0.0])
    unary_params = w[:2]
    pairwise_flat = np.asarray(w[2:])
    pairwise_params = np.zeros((2, 2))
    pairwise_params[np.tri(2, dtype=np.bool)] = pairwise_flat
    pairwise_params = pairwise_params + pairwise_params.T - np.diag(np.diag(pairwise_params))
    crf = GridCRF()
    x_loss_augmented = crf.loss_augment(x, y, w)
    y_hat = crf.inference(x_loss_augmented, w)
    # test that loss_augmented_inference does the same
    y_hat_2 = crf.loss_augmented_inference(x, y, w)
    assert_array_equal(y_hat_2, y_hat)
    energy = compute_energy(x, y_hat, unary_params, pairwise_params)
    energy_loss_augmented = compute_energy(x_loss_augmented, y_hat, unary_params, pairwise_params)

    assert_almost_equal(energy + crf.loss(y, y_hat), energy_loss_augmented)

    # with zero in w:
    unary_params[1] = 0
    assert_raises(ValueError, crf.loss_augment, x, y, w)