Exemple #1
0
    key_list.append(('cancer', f'p{p}'))

num_test = 5

avg_diff = dict()
variance = dict()
time_cost = dict()

ans = dict()

name = 'EPBP'
res = np.zeros((len(key_list), num_test))
for j in range(num_test):
    bp = EPBP(g, n=10, proposal_approximation='simple')
    start_time = time.process_time()
    bp.run(10, log_enable=True)
    time_cost[name] = (time.process_time() -
                       start_time) / num_test + time_cost.get(name, 0)
    print(name, f'time {time.process_time() - start_time}')
    for i, key in enumerate(key_list):
        res[i, j] = bp.probability(0.8, 1, rvs_table[key])
for i, key in enumerate(key_list):
    ans[key] = np.average(res[i, :])
variance[name] = np.average(np.var(res, axis=1))
print(name, 'var', variance[name])

# save ans
ans_array = list()
for key in key_list:
    ans_array.append((f'{key[0]},{key[1]}', ans[key]))
np.save('Data/smoker_ans', np.array(ans_array))
                    crv_marg_params = sampling_utils.fit_scalar_gm_from_samples(
                        cont_samples[:, crv_idx], K=num_gm_components_for_crv)
                    marg_logpdf = utils.get_scalar_gm_log_prob(
                        None, *crv_marg_params, get_fun=True)
                    margs[i] = marg_logpdf
            # save baseline
            baseline_mmap = mmap
            baseline_margs = margs
            marg_kls = np.zeros_like(mmap)
            cpu_time = wall_time = 0  # don't care

        elif algo_name == 'EPBP':
            bp = EPBP(g, n=20, proposal_approximation='simple')
            start_time = time.process_time()
            start_wall_time = time.time()
            bp.run(10, log_enable=False)
            cpu_time = time.process_time() - start_time
            wall_time = time.time() - start_wall_time

            for i, rv in enumerate(query_rvs):
                mmap[i] = bp.map(rv)
                # marg_logpdf = lambda x: bp.belief(x, rv, log_belief=True)  # probly slightly faster if not plotting
                marg_logpdf = utils.curry_epbp_belief(bp, rv, log_belief=True)
                margs[i] = marg_logpdf
                assert rv.domain_type[
                    0] == 'c', 'only looking at kl for cnode queries for now'
                # lb, ub = -np.inf, np.inf
                lb, ub = -np.inf, np.inf
                marg_kl = kl_continuous_logpdf(log_p=baseline_margs[i],
                                               log_q=margs[i],
                                               a=lb,
Exemple #3
0
        if rv.value is None:  # only test non-evidence nodes
            print(rv, key, osi.map(obs_rvs=[], query_rv=rv))

    import matplotlib.pyplot as plt

    for key in record:
        plt.plot(record[key], label=key)
    plt.legend(loc='best')
    save_name = __file__.split('.py')[0]
    plt.savefig('%s.png' % save_name)

    # EPBP inference
    from EPBPLogVersion import EPBP

    bp = EPBP(g, n=50, proposal_approximation='simple')
    bp.run(30, log_enable=True)

    for key, rv in rvs_table.items():
        if rv.value is None:  # only test non-evidence nodes
            print(key, bp.map(rv))

else:
    K = 3
    lr = 1e-2
    its = 1
    import tensorflow as tf

    for T in [10, 20, 50, 100]:
        print('grad check, T =', T)
        utils.set_seed(seed)
        osi = OneShot(g=g, K=K, T=T, seed=seed)