コード例 #1
0
g.init_rv_indices()
Vd, Vc, Vd_idx, Vc_idx = g.Vd, g.Vc, g.Vd_idx, g.Vc_idx

names = ('EPBP', 'OSI')
num_algos = len(names)
mmap_res = np.empty([num_algos, len(rvs)])

algo = 0
name = names[algo]
bp = EPBP(g, n=20, proposal_approximation='simple')
# start_time = time.process_time()
bp.run(10, log_enable=False)
# time_cost[name] = (time.process_time() - start_time) / num_runs / num_tests + time_cost.get(name, 0)
# print(name, f'time {time.process_time() - start_time}')
for i, rv in enumerate(rvs):
    mmap_res[algo, i] = bp.map(rv)

algo += 1
name = names[algo]
utils.set_log_potential_funs(
    g.factors_list)  # OSI assumes factors have callable .log_potential_fun
K = 2
T = 12
lr = 0.5
its = 200
fix_mix_its = int(its * 0.5)
osi = OneShot(
    g=g, K=K, T=T, seed=seed
)  # can be moved outside of all loops if the ground MRF doesn't change
osi.run(lr=lr, its=its, fix_mix_its=fix_mix_its)
# print('osi params', osi.params)
コード例 #2
0
    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)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=lr)
        res = osi.run(lr=lr, optimizer=optimizer, its=its, grad_check=True)
        print()
コード例 #3
0
            key_list.append(('B', x_))

    ans = dict()

    name = 'EPBP'
    res = np.zeros((len(key_list), num_runs))
    for j in range(num_runs):
        bp = EPBP(g, n=20, proposal_approximation='simple')
        start_time = time.process_time()
        bp.run(10, log_enable=False)
        time_cost[name] = (time.process_time() -
                           start_time) / num_runs / num_tests + 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.map(rvs_table[key])
        print(res[:, j])
    for i, key in enumerate(key_list):
        ans[key] = np.average(res[i, :])
    for i, key in enumerate(key_list):
        res[i, :] -= ans[key]
    avg_diff[name] = np.average(np.average(
        abs(res), axis=1)) / num_tests + avg_diff.get(name, 0)
    err_var[name] = np.average(np.average(
        res**2, axis=1)) / num_tests + err_var.get(name, 0)
    print(name, 'diff', np.average(np.average(abs(res), axis=1)))
    print(
        name, 'var',
        np.average(np.average(res**2, axis=1)) -
        np.average(np.average(abs(res), axis=1))**2)
コード例 #4
0
    if algo == 'EPBP':
        bp = EPBP(g, n=50, proposal_approximation='simple')
        print('number of vr', len(g.rvs))
        num_evidence = 0
        for rv in g.rvs:
            if rv.value is not None:
                num_evidence += 1
        print('number of evidence', num_evidence)

        start_time = time.process_time()
        bp.run(20, log_enable=False)
        time_cost.append(time.process_time() - start_time)
        print('time lapse', time.process_time() - start_time)

        for idx, rv in enumerate(rvs_table[t - 1]):
            result[idx, i] = bp.map(rv)

    elif algo == 'OSI':
        if cond:
            osi = OneShot(g=cond_g, K=K, T=T, seed=seed)
        else:
            osi = OneShot(g=g, K=K, T=T, seed=seed)
        start_time = time.process_time()
        osi.run(lr=lr, its=its, fix_mix_its=fix_mix_its, logging_itv=logging_itv)
        time_cost.append(time.process_time() - start_time)
        # print('Mu =\n', osi.params['Mu'], '\nVar =\n', osi.params['Var'])
        print(algo, f'time {time_cost[-1]}')

        for idx, rv in enumerate(rvs_table[t - 1]):
            if cond:
                result[idx, i] = osi.map(obs_rvs=[], query_rv=rv)
コード例 #5
0
    g, rvs_table = kmf.grounded_graph(t, data)
    bp = EPBP(g, n=50, proposal_approximation='simple')
    print('number of vr', len(g.rvs))
    num_evidence = 0
    for rv in g.rvs:
        if rv.value is not None:
            num_evidence += 1
    print('number of evidence', num_evidence)

    start_time = time.process_time()
    bp.run(20, log_enable=False)
    time_cost.append(time.process_time() - start_time)
    print('time lapse', time.process_time() - start_time)

    for idx, rv in enumerate(rvs_table[t - 1]):
        result[idx, i] = bp.map(rv)

    bp = GaLBP(g)
    bp.run(20, log_enable=False)

    for idx, rv in enumerate(rvs_table[t - 1]):
        ans2[idx, i] = bp.map(rv)

    print(f'avg err {np.average(result[:, i] - ans[:, i])}')
    print(f'avg err2 {np.average(result[:, i] - ans2[:, i])}')

err = abs(result - ans)
err = np.average(err, axis=0)

err2 = abs(result - ans)
err2 = np.average(err2, axis=0)