def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(['qpbo']) or not get_installed(['ad3']): return X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2, inference_method='qpbo') crf.initialize(X, Y) H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000, switch_to=('ad3', {'branch_and_bound': True}), C=10. ** 3) clf = LatentSSVM(base_ssvm) clf.fit(X, Y, H_init=H_init) assert_equal(clf.model.inference_method[0], 'ad3') Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_blocks_crf_directional(): # test latent directional CRF on blocks # test that all results are the same as equivalent LatentGridCRF X, Y = 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_states_per_label=2, inference_method='lp') crf.initialize(X, Y) directional_crf = LatentDirectionalGridCRF(n_states_per_label=2, inference_method='lp') directional_crf.initialize(X, Y) 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) joint_feature = crf.joint_feature(x, h_hat) joint_feature_d = directional_crf.joint_feature(x, h_hat) assert_array_equal(np.dot(joint_feature, w), np.dot(joint_feature_d, w_directional))
def test_with_crosses_bad_init(): # use less perfect initialization rnd = np.random.RandomState(0) X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2) crf.initialize(X, Y) H_init = crf.init_latent(X, Y) mask = rnd.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) one_slack_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, C=100) clf = LatentSSVM(one_slack_ssvm) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(["qpbo"]) or not get_installed(["ad3"]): return X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2, inference_method="qpbo") crf.initialize(X, Y) H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > 0.7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM( crf, inactive_threshold=1e-8, cache_tol=0.0001, inference_cache=50, max_iter=10000, switch_to=("ad3", {"branch_and_bound": True}), C=10.0 ** 3, ) clf = LatentSSVM(base_ssvm) # evil hackery to get rid of ad3 output try: devnull = open("/dev/null", "w") oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) replaced_stdout = True except: replaced_stdout = False clf.fit(X, Y, H_init=H_init) if replaced_stdout: os.dup2(oldstdout_fno, 1) assert_equal(clf.model.inference_method[0], "ad3") Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(0.98 < clf.score(X_test, Y_test) < 1)
def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(['qpbo']) or not get_installed(['ad3']): return X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2, inference_method='qpbo') crf.initialize(X, Y) H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000, switch_to=('ad3', { 'branch_and_bound': True }), C=10.**3) clf = LatentSSVM(base_ssvm) # evil hackery to get rid of ad3 output try: devnull = open('/dev/null', 'w') oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) replaced_stdout = True except: replaced_stdout = False clf.fit(X, Y, H_init=H_init) if replaced_stdout: os.dup2(oldstdout_fno, 1) assert_equal(clf.model.inference_method[0], 'ad3') Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_blocks_crf_directional(): # test latent directional CRF on blocks # test that all results are the same as equivalent LatentGridCRF X, Y = 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_states_per_label=2, inference_method='lp') crf.initialize(X, Y) directional_crf = LatentDirectionalGridCRF(n_states_per_label=2, inference_method='lp') directional_crf.initialize(X, Y) 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))
def test_objective(): # test that SubgradientLatentSSVM does the same as SubgradientSVM, # in particular that it has the same loss, if there are no latent states. X, Y = generate_blocks_multinomial(n_samples=10, noise=.3, seed=1) inference_method = get_installed(["qpbo", "ad3", "lp"])[0] n_labels = 3 crfl = LatentGridCRF(n_labels=n_labels, n_states_per_label=1, inference_method=inference_method) clfl = SubgradientLatentSSVM(model=crfl, max_iter=20, C=10., learning_rate=0.001, momentum=0.98) crfl.initialize(X, Y) clfl.w = np.zeros(crfl.size_joint_feature) # this disables random init clfl.fit(X, Y) crf = GridCRF(n_states=n_labels, inference_method=inference_method) clf = SubgradientSSVM(model=crf, max_iter=20, C=10., learning_rate=0.001, momentum=0.98) clf.fit(X, Y) assert_array_almost_equal(clf.w, clfl.w) assert_almost_equal(clf.objective_curve_[-1], clfl.objective_curve_[-1]) assert_array_equal(clf.predict(X), clfl.predict(X)) assert_array_equal(clf.predict(X), Y)
def test_with_crosses_bad_init(): # use less perfect initialization rnd = np.random.RandomState(0) X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2) crf.initialize(X, Y) H_init = crf.init_latent(X, Y) mask = rnd.uniform(size=H_init.shape) > 0.7 H_init[mask] = 2 * (H_init[mask] / 2) one_slack_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=0.0001, inference_cache=50, C=100) clf = LatentSSVM(one_slack_ssvm) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(0.98 < clf.score(X_test, Y_test) < 1)