# TODO: fitting gm twice, wasteful cont_samples = hgsampler.cont_samples crv_idx = cond_g.Vc_idx[rv] 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
else: pred[key] = osi.map( obs_rvs=obs_rvs, query_rv=rvs_table[key]) # obs_rvs from the original graph err.append(abs(pred[key] - ans[key])) # print(pred) avg_err[name] = np.average(err) / num_test + avg_err.get(name, 0) max_err[name] = np.max(err) / num_test + max_err.get(name, 0) err_var[name] = np.average(err)**2 / num_test + err_var.get(name, 0) print(name, f'avg err {np.average(err)}') print(name, f'max err {np.max(err)}') run = False if run: name = 'EPBP' bp = EPBP(g, n=20) start_time = time.process_time() bp.run(15, log_enable=False) time_cost[name] = (time.process_time() - start_time) / num_test + time_cost.get(name, 0) print(name, f'time {time.process_time() - start_time}') err = [] for key in key_list: if key not in data: pred[key] = bp.map(rvs_table[key]) err.append(abs(pred[key] - ans[key])) print(pred) avg_err[name] = np.average(err) / num_test + avg_err.get(name, 0) max_err[name] = np.max(err) / num_test + max_err.get(name, 0) err_var[name] = np.average(err)**2 / num_test + err_var.get(name, 0) print(name, f'avg err {np.average(err)}')
g.init_nb() from C2FVarInference import VarInference # np.random.seed(9) vi = VarInference(g, num_mixtures=4, num_quadrature_points=8) vi.run(200, lr=0.1) print(vi.free_energy()) for rv in g.rvs: if rv.value is None: # only test non-evidence nodes p = dict() for x in rv.domain.values: p[x] = vi.belief(x, rv) print(rv, p) # EPBP inference from EPBPLogVersion import EPBP bp = EPBP(g, n=50, proposal_approximation='simple') bp.run(20, log_enable=False) for rv in g.rvs: if rv.value is None: # only test non-evidence nodes p = dict() for x in rv.domain.values: p[x] = bp.belief(x, rv) print(rv, p)
key_list = list() for p in range(100): 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:
print('number of factors', len(g.factors)) print('number of evidence', len(data)) key_list = list() for y_ in Y: key_list.append(('A', y_)) for x_ in X: if ('B', x_) not in data: key_list.append(('B', x_)) ans = dict() name = 'EPBP' res = np.zeros((len(key_list), 1)) for j in range(1): 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) / 1 / 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.map(rvs_table[key]) 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_test + avg_diff.get(name, 0) err_var[name] = np.average(np.average( res**2, axis=1)) / num_test + err_var.get(name, 0)
lr = 5e-1 # its = 1000 its = 300 # fix_mix_its = int(its * 0.1) fix_mix_its = int(its * 1.0) # fix_mix_its = 500 logging_itv = 50 obs_rvs = [v for v in g.rvs if v.value is not None] # cond = True cond = True if cond: cond_g.init_nb( ) # this will make cond_g rvs' .nb attributes consistent; OSI uses when defining obj 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)
baseline_margs = margs elif algo_name == 'GaBP': bp = GaBP(g) start_time = time.process_time() start_wall_time = time.time() bp.run(20, 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) belief_params = bp.get_belief_params(rv) margs[i] = utils.curry_normal_logpdf(*belief_params) elif algo_name == 'EPBP': bp = EPBP(g, n=50, proposal_approximation='simple') start_time = time.process_time() start_wall_time = time.time() bp.run(20, 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 elif algo_name in ('OSI', 'LOSI', 'NPVI', 'LNPVI'): cond = True if cond:
bp.run(15, c2f=0, log_enable=False) time_cost[name] = (time.process_time() - start_time) / num_test + time_cost.get(name, 0) print(name, f'time {time.process_time() - start_time}') err = [] for key in key_list: if key not in data: err.append(abs(bp.map(rvs_table[key]) - ans[key])) avg_err[name] = np.average(err) / num_test + avg_err.get(name, 0) max_err[name] = np.max(err) / num_test + max_err.get(name, 0) err_var[name] = np.average(err)**2 / num_test + err_var.get(name, 0) print(name, f'avg err {np.average(err)}') print(name, f'max err {np.max(err)}') name = 'EPBP' bp = EPBP(g, n=20) start_time = time.process_time() bp.run(15, log_enable=False) time_cost[name] = (time.process_time() - start_time) / num_test + time_cost.get(name, 0) print(name, f'time {time.process_time() - start_time}') err = [] for key in key_list: if key not in data: err.append(abs(bp.map(rvs_table[key]) - ans[key])) avg_err[name] = np.average(err) / num_test + avg_err.get(name, 0) max_err[name] = np.max(err) / num_test + max_err.get(name, 0) err_var[name] = np.average(err)**2 / num_test + err_var.get(name, 0) print(name, f'avg err {np.average(err)}') print(name, f'max err {np.max(err)}')
# vi.run(500, lr=0.1) # # print(vi.free_energy()) # # for key, rv in sorted(rvs_table.items()): # if rv.value is None: # only test non-evidence nodes # # p = dict() # # for x in rv.domain.values: # # p[x] = osi.belief(x, rv) # # print(key, p) # print(key, vi.map(rv)) # EPBP inference from EPBPLogVersion import EPBP bp = EPBP(g, n=20, proposal_approximation='simple') bp.run(20, log_enable=False) for key, rv in sorted(rvs_table.items()): if rv.value is None: # only test non-evidence nodes # p = dict() # for x in rv.domain.values: # p[x] = bp.belief(x, rv) # print(key, p) print(key, bp.map(rv)) # GaBP inference # from GaBP import GaBP # # bp = GaBP(g) # bp.run(20, log_enable=False)
for key, rv in rvs_table.items(): 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)