Beispiel #1
0
def test_grid():
    model = grid_potts_model(4, 5, al_size=2, seed=0)
    gt = model.infer(algorithm='path_dp')

    result = model.infer(algorithm='mean_field', max_iter=1000)

    assert_results_close(result, gt, log_pf_tol=5.0, mp_mse_tol=0.05)
Beispiel #2
0
def test_grid_4x4x2():
    model = grid_potts_model(4, 4, al_size=2, seed=0)
    max_lh_gt = model.max_likelihood(algorithm='path_dp')
    max_lh_gt_value = np.log(model.evaluate(max_lh_gt))
    max_lh_lower_bound = lp_relaxation(model).lower_bound
    max_lh_upper_bound = lp_relaxation(model).upper_bound
    assert (max_lh_lower_bound <= max_lh_gt_value <= max_lh_upper_bound)
Beispiel #3
0
def test_infer_compare_with_pairwise_grid_5x5():
    pw_model = grid_potts_model(5, 5, al_size=2, seed=0)
    true_pf = np.exp(pw_model.infer(algorithm='path_dp').log_pf)
    nfg_model = inferlo.NormalFactorGraphModel.from_model(pw_model)

    pf = infer_edge_elimination(nfg_model)

    assert np.allclose(true_pf, pf)
Beispiel #4
0
def test_grid():
    model = grid_potts_model(10, 5, al_size=2)
    true_result = model.infer(algorithm='path_dp')
    result_bp = BP.infer(model)
    assert_results_close(true_result,
                         result_bp,
                         log_pf_tol=0.1,
                         mp_mse_tol=2e-3)
Beispiel #5
0
def test_cycle_relaxation_2x2_cycle():
    """
    Sherali-Adams is exact on line graph.
    """
    model = grid_potts_model(2, 2, al_size=2, seed=0)
    max_lh_gt = model.max_likelihood(algorithm='path_dp')
    sa_res = minimal_cycle(model)
    max_lh_ub = sa_res.upper_bound
    assert np.allclose(max_lh_ub, np.log(model.evaluate(max_lh_gt)))
Beispiel #6
0
def test_custom_dataset():
    model = grid_potts_model(4, 3, al_size=4, seed=1)
    true_result = model.infer(algorithm='path_dp')
    model = GenericGraphModel.from_model(model)
    dataset = Dataset(model=model,
                      true_log_pf=true_result.log_pf,
                      true_marginals=true_result.marg_prob,
                      name='test_dataset')
    dataset_loader.save_custom_dataset(dataset)
    reloaded_dataset = dataset_loader.load_custom_dataset('test_dataset')
    assert reloaded_dataset.model.num_variables == model.num_variables
    assert reloaded_dataset.model.factors[1].var_idx == model.factors[
        1].var_idx
    assert np.allclose(reloaded_dataset.model.factors[1].values,
                       model.factors[1].values)
    assert np.allclose(reloaded_dataset.true_log_pf, dataset.true_log_pf)
    assert np.allclose(reloaded_dataset.true_marginals, dataset.true_marginals)
    assert reloaded_dataset.name == 'test_dataset'
Beispiel #7
0
def test_grid_approx():
    model = grid_potts_model(5, 15, al_size=2, seed=0)
    mp_true = model.infer(algorithm='path_dp').marg_prob
    mp = model.infer(algorithm='message_passing').marg_prob

    assert np.mean(np.square(mp_true - mp)) < 1e-4
Beispiel #8
0
def test_sample_grid2x20x2():
    model = grid_potts_model(2, 20, al_size=2, seed=0)
    true_marg_probs = model.infer(algorithm='path_dp').marg_prob
    samples = model.sample(num_samples=1000)
    check_samples(samples=samples, true_marg_probs=true_marg_probs, tol=5e-4)
Beispiel #9
0
def test_max_likelihood_grid_4x50x2():
    model = grid_potts_model(4, 50, al_size=2, seed=0)
    true_ml = model.max_likelihood(algorithm='path_dp')
    ml = max_likelihood_junction_tree(model)
    assert np.allclose(ml, true_ml)
Beispiel #10
0
def test_inference_grid_4x50x2():
    model = grid_potts_model(4, 50, al_size=2, seed=0)
    ground_truth = model.infer(algorithm='path_dp')
    result = infer_junction_tree(model)
    assert_results_close(result, ground_truth)
Beispiel #11
0
def test_grid_2x2x10():
    model = grid_potts_model(2, 2, al_size=10, seed=0)
    max_lh_gt = model.max_likelihood(algorithm='bruteforce')
    max_lh = max_lh_path_dp(model)
    assert np.allclose(max_lh, max_lh_gt)
Beispiel #12
0
def test_potts_grid():
    model = grid_potts_model(6, 5, al_size=3)
    true_result = model.infer(algo='parth_dp')
    model = inferlo.GenericGraphModel.from_model(model)
    bw_result = BackwardBucketElimination.infer(model)
    assert_results_close(true_result, bw_result)
Beispiel #13
0
def test_grid_3x3():
    model = grid_potts_model(3, 3, al_size=2, seed=123)
    gt = model.infer(algorithm='bruteforce')
    result = model.infer(algorithm='path_dp')
    assert_results_close(result, gt)