def test_binary_blocks_cutting_plane(): #testing cutting plane ssvm on easy binary dataset # generate graphs explicitly for each example for inference_method in ["dai", "lp", "qpbo", "ad3"]: print("testing %s" % inference_method) X, Y = toy.generate_blocks(n_samples=3) crf = GraphCRF(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) 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)
def test_multinomial_blocks_directional_anti_symmetric(): # testing cutting plane ssvm with directional CRF on easy multinomial # dataset X_, Y_ = toy.generate_blocks_multinomial(n_samples=10, noise=0.3, seed=0) G = [make_grid_edges(x, return_lists=True) for x in X_] edge_features = [edge_list_to_features(edge_list) for edge_list in G] edges = [np.vstack(g) for g in G] X = zip([x.reshape(-1, 3) for x in X_], edges, edge_features) Y = [y.ravel() for y in Y_] for inference_method in ['lp', 'ad3']: crf = EdgeFeatureGraphCRF(n_states=3, inference_method=inference_method, n_edge_features=2, symmetric_edge_features=[0], antisymmetric_edge_features=[1]) clf = StructuredSVM(model=crf, max_iter=20, C=1000, verbose=10, check_constraints=False, n_jobs=-1) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred) pairwise_params = clf.w[-9 * 2:].reshape(2, 3, 3) sym = pairwise_params[0] antisym = pairwise_params[1] print(sym) print(antisym) assert_array_equal(sym, sym.T) assert_array_equal(antisym, -antisym.T)
def test_multinomial_checker_cutting_plane(): X, Y = toy.generate_checker_multinomial(n_samples=10, noise=.1) n_labels = len(np.unique(Y)) crf = GridCRF(n_states=n_labels) clf = StructuredSVM(problem=crf, max_iter=20, C=100000, verbose=0, check_constraints=True, n_jobs=-1) 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(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)
def test_simple_1d_dataset_cutting_plane(): # 10 1d datapoints between 0 and 1 X = np.random.uniform(size=(30, 1)) Y = (X.ravel() > 0.5).astype(np.int) # we have to add a constant 1 feature by hand :-/ X = np.hstack([X, np.ones((X.shape[0], 1))]) pbl = CrammerSingerSVMModel(n_features=2) svm = StructuredSVM(pbl, verbose=10, check_constraints=True, C=10000) svm.fit(X, Y) assert_array_equal(Y, np.hstack(svm.predict(X)))
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=[5], n_jobs=-1) 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
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)
def test_multinomial_blocks_cutting_plane(): #testing cutting plane ssvm on easy multinomial dataset X, Y = toy.generate_blocks_multinomial(n_samples=10, noise=0.3, seed=0) n_labels = len(np.unique(Y)) for inference_method in ['lp', 'qpbo', 'ad3']: crf = GridCRF(n_states=n_labels, inference_method=inference_method) clf = StructuredSVM(problem=crf, max_iter=10, C=100, verbose=0, check_constraints=False, n_jobs=-1) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def test_simple_1d_dataset_cutting_plane(): # 10 1d datapoints between 0 and 1 X = np.random.uniform(size=(30, 1)) # linearly separable labels Y = 1 - 2 * (X.ravel() < .5) # we have to add a constant 1 feature by hand :-/ X = np.hstack([X, np.ones((X.shape[0], 1))]) pbl = BinarySVMModel(n_features=2) svm = StructuredSVM(pbl, verbose=3, check_constraints=True, C=1000) svm.fit(X, Y) assert_array_equal(Y, np.hstack(svm.predict(X)))
def test_blobs_2d_cutting_plane(): # make two gaussian blobs X, Y = make_blobs(n_samples=80, centers=2, random_state=1) Y = 2 * Y - 1 # we have to add a constant 1 feature by hand :-/ X = np.hstack([X, np.ones((X.shape[0], 1))]) X_train, X_test, Y_train, Y_test = X[:40], X[40:], Y[:40], Y[40:] pbl = BinarySVMModel(n_features=3) svm = StructuredSVM(pbl, verbose=3, check_constraints=True, C=1000) svm.fit(X_train, Y_train) assert_array_equal(Y_test, np.hstack(svm.predict(X_test)))
def test_blobs_2d_cutting_plane(): # make two gaussian blobs X, Y = make_blobs(n_samples=80, centers=3, random_state=42) # we have to add a constant 1 feature by hand :-/ X = np.hstack([X, np.ones((X.shape[0], 1))]) X_train, X_test, Y_train, Y_test = X[:40], X[40:], Y[:40], Y[40:] pbl = CrammerSingerSVMProblem(n_features=3, n_classes=3) svm = StructuredSVM(pbl, verbose=10, check_constraints=True, C=1000, batch_size=1) svm.fit(X_train, Y_train) assert_array_equal(Y_test, np.hstack(svm.predict(X_test)))
def test_n_slack_svm_as_crf_pickling(): iris = load_iris() X, y = iris.data, iris.target X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] Y = y.reshape(-1, 1) X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1) _, file_name = mkstemp() pbl = GraphCRF(n_features=4, n_states=3, inference_method='lp') logger = SaveLogger(file_name, verbose=1) svm = StructuredSVM(pbl, verbose=0, C=100, n_jobs=1, logger=logger) svm.fit(X_train, y_train) assert_less(.97, svm.score(X_test, y_test)) assert_less(.97, logger.load().score(X_test, y_test))
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)
def test_binary_ssvm_repellent_potentials(): # test non-submodular learning with and without positivity constraint # dataset is checkerboard X, Y = toy.generate_checker() for inference_method in ["lp", "qpbo", "ad3"]: crf = GridCRF(inference_method=inference_method) clf = StructuredSVM(problem=crf, max_iter=10, C=100, verbose=0, check_constraints=True, n_jobs=-1) clf.fit(X, Y) Y_pred = clf.predict(X) # standard crf can predict perfectly assert_array_equal(Y, Y_pred) submodular_clf = StructuredSVM(problem=crf, max_iter=10, C=100, verbose=0, check_constraints=True, positive_constraint=[4, 5, 6], n_jobs=-1) submodular_clf.fit(X, Y) Y_pred = submodular_clf.predict(X) # submodular crf can not do better than unaries for i, x in enumerate(X): y_pred_unaries = crf.inference(x, np.array([1, 0, 0, 1, 0, 0, 0])) assert_array_equal(y_pred_unaries, Y_pred[i])
SubgradientSSVM) # do a binary digit classification digits = load_digits() X, y = digits.data, digits.target # make binary task by doing odd vs even numers y = y % 2 # code as +1 and -1 y = 2 * y - 1 X /= X.max() X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) pbl = BinarySVMModel(n_features=X_train.shape[1] + 1) # add one for bias n_slack_svm = StructuredSVM(pbl, verbose=0, check_constraints=False, C=10, batch_size=-1) one_slack_svm = OneSlackSSVM(pbl, verbose=10, check_constraints=False, C=10, max_iter=1000, tol=0.1) subgradient_svm = SubgradientSSVM(pbl, C=10, learning_rate=0.1, max_iter=100, decay_exponent=0, batch_size=10, verbose=10) # we add a constant 1 feature for the bias X_train_bias = np.hstack([X_train, np.ones((X_train.shape[0], 1))]) X_test_bias = np.hstack([X_test, np.ones((X_test.shape[0], 1))]) # n-slack cutting plane ssvm start = time() n_slack_svm.fit(X_train_bias, y_train) time_n_slack_svm = time() - start y_pred = np.hstack(n_slack_svm.predict(X_test_bias)) print("Score with pystruct n-slack ssvm: %f (took %f seconds)"
from pystruct.learners import (StructuredSVM, OneSlackSSVM, SubgradientSSVM) # do a binary digit classification digits = load_digits() X, y = digits.data, digits.target X /= X.max() X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) # we add a constant 1 feature for the bias X_train_bias = np.hstack([X_train, np.ones((X_train.shape[0], 1))]) pbl = CrammerSingerSVMModel(n_features=X_train_bias.shape[1], n_classes=10) n_slack_svm = StructuredSVM(pbl, verbose=0, check_constraints=False, C=20, max_iter=500, batch_size=10) one_slack_svm = OneSlackSSVM(pbl, verbose=0, check_constraints=False, C=20, max_iter=1000, tol=0.001) subgradient_svm = SubgradientSSVM(pbl, C=20, learning_rate=0.01, max_iter=300, decay_exponent=0, momentum=0, verbose=0) # n-slack cutting plane ssvm n_slack_svm.fit(X_train_bias, y_train) ## 1-slack cutting plane ssvm one_slack_svm.fit(X_train_bias, y_train) # online subgradient ssvm subgradient_svm.fit(X_train_bias, y_train) #plt.plot(n_slack_svm.objective_curve_, label="n-slack lower bound")
from time import time import numpy as np from sklearn.datasets import load_iris from sklearn.cross_validation import train_test_split from pystruct.models import GraphCRF from pystruct.learners import StructuredSVM iris = load_iris() X, y = iris.data, iris.target # make each example into a tuple of a single feature vector and an empty edge # list X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] Y = y.reshape(-1, 1) X_train, X_test, y_train, y_test = train_test_split(X_, Y) pbl = GraphCRF(n_features=4, n_states=3, inference_method='lp') svm = StructuredSVM(pbl, verbose=1, check_constraints=True, C=100, n_jobs=1) start = time() svm.fit(X_train, y_train) time_svm = time() - start y_pred = np.vstack(svm.predict(X_test)) print("Score with pystruct crf svm: %f (took %f seconds)" % (np.mean(y_pred == y_test), time_svm))
# learn the "easy" 2x2 boxes dataset. # a 2x2 box is placed randomly in a 4x4 grid # we add a latent variable for each 2x2 patch # that should make the model fairly simple X, Y = toy.make_simple_2x2(seed=1) # flatten X and Y X_flat = [x.reshape(-1, 1).astype(np.float) for x in X] Y_flat = [y.ravel() for y in Y] # first, use standard graph CRF. Can't do much, high loss. crf = GraphCRF(n_states=2, n_features=1, inference_method='lp') svm = StructuredSVM(model=crf, max_iter=200, C=1, verbose=0, check_constraints=True, break_on_bad=False, n_jobs=1) # make dataset from X and graph without edges #G_ = [np.zeros((0, 2), dtype=np.int) for x in X] G = [make_grid_edges(x) for x in X] asdf = zip(X_flat, G) svm.fit(asdf, Y_flat) plot_boxes(svm.predict(asdf)) print("Training score multiclass svm CRF: %f" % svm.score(asdf, Y_flat)) # using one latent variable for each 2x2 rectangle latent_crf = LatentNodeCRF(n_labels=2, n_features=1, inference_method='lp', n_hidden_states=2) #latent_svm = LatentSSVM(model=latent_crf, max_iter=200, C=10, verbose=10, #check_constraints=True, break_on_bad=True, n_jobs=1,
# make binary task by doing odd vs even numers y = y_org % 2 X /= X.max() # make each example into a tuple of a single feature vector and an empty edge # list X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] Y = y.reshape(-1, 1) X_train_, X_test_, X_train, X_test, y_train, y_test, y_org_train, y_org_test =\ train_test_split(X_, X, Y, y_org, test_size=.5) # first, do it with a standard CRF / SVM pbl = GraphCRF(n_features=64, n_states=2, inference_method='lp') svm = StructuredSVM(pbl, verbose=1, check_constraints=True, C=1000, n_jobs=1, batch_size=-1) svm.fit(X_train_, y_train) y_pred = np.vstack(svm.predict(X_test_)) print("Score with pystruct crf svm: %f" % np.mean(y_pred == y_test)) print(svm.score(X_train_, y_train)) print(svm.score(X_test_, y_test)) # now with latent CRF SVM latent_pbl = LatentGraphCRF(n_features=64, n_labels=2, n_states_per_label=5, inference_method='dai') latent_svm = LatentSubgradientSSVM(model=latent_pbl, max_iter=5000, C=1, verbose=2, n_jobs=1, learning_rate=0.1, show_loss_every=10, momentum=0.0, decay_exponent=0.5) #latent_svm = LatentSSVM(latent_pbl, verbose=2, check_constraints=True, C=100,