from MLNPotential import MLNPotential, and_op, or_op, eq_op import numpy as np seed = 0 np.random.seed(seed) from EPBPLogVersion import EPBP from OneShot import OneShot from hybrid_gaussian_mrf import convert_to_bn, block_gibbs_sample, get_crv_marg, get_drv_marg, \ get_rv_marg_map_from_bn_params domain_bool = Domain(values=(0, 1), continuous=False) domain_real = Domain(values=(-10, 10), continuous=True) rvs = [ RV(domain=domain_bool), RV(domain=domain_bool), RV(domain=domain_real), RV(domain=domain_real) ] Nc = 2 # covs = np.array([np.eye(Nc)] * len(rvs[0].values)) # means = np.array([[-2., -2.], [0., 1.], [3., 0.]]) # factors = [F(nb=(rvs[0], rvs[2], rvs[3]), # log_potential_fun=LogHybridQuadratic(A=-0.5 * covs, # b=means, # c=-0.5 * np.array([np.dot(m, m) for m in means]))), # F(nb=(rvs[0],), log_potential_fun=LogTable(np.array([-0.1, 0, 2.]))), # F(nb=(rvs[0], rvs[1]), log_potential_fun=LogTable(np.array([[2., 0], [-0.1, 1]]))), # F(nb=(rvs[2],), log_potential_fun=LogQuadratic(A=-0.5 * np.ones([1, 1]), b=np.zeros([1]), c=0.)) # ]
import utils utils.set_path() import sampling_utils from Graph import F, RV, Domain, Graph from Potential import LogTable, LogQuadratic, LogHybridQuadratic import numpy as np seed = 0 np.random.seed(0) from hybrid_gaussian_mrf import convert_to_bn, block_gibbs_sample, get_crv_marg, get_drv_marg rvs = [ RV(domain=Domain(values=(0, 1, 2), continuous=False)), RV(domain=Domain(values=(0, 1), continuous=False)), RV(domain=Domain(values=(-5, 5), continuous=True)), RV(domain=Domain(values=(-5, 5), continuous=True)) ] Nc = 2 covs = np.array([np.eye(Nc)] * rvs[0].dstates) # means = np.array([[0., 0.], [0., 1.], [1., 0.]]) means = np.array([[-2., -2.], [0., 1.], [3., 0.]]) factors = [ F(nb=(rvs[0], rvs[2], rvs[3]), log_potential_fun=LogHybridQuadratic( A=-0.5 * covs, b=means, c=-0.5 * np.array([np.dot(m, m) for m in means]))), F(nb=(rvs[0], ), log_potential_fun=LogTable(np.array([-0.1, 0, 2.]))), F(nb=(rvs[0], rvs[1]),
# same as clausal form below # (lambda fxy, fyz, fxz: or_op(or_op(neg_op(fxy), neg_op(fyz)), fxz), 0.7), # (lambda sx, cx: or_op(neg_op(sx), cx), 1.5), # (lambda fxy, sx, sy: or_op(or_op(neg_op(fxy), neg_op(sx)), sy), 1.1) ] # log_potential_funs = [utils.weighted_feature_fun(f, w) for (f, w) in KB] potentials = [MLNPotential(formula=f, w=w) for (f, w) in KB] # create the MN (ground MLN for constants={A, B}) N = 8 nodes = np.arange( N ) # node ids; 0:F(A,A), 1:F(A,B), 2:S(A), 3:C(A), 4:F(B,B), 5:F(B,A), 6:S(B), 7:C(B) rvs = [ RV(domain=Domain(values=np.array([0, 1]), continuous=False)) for n in nodes ] factors = [ F(potential=potentials[1], nb=[rvs[i] for i in [0, 2, 2]]), F(potential=potentials[0], nb=[rvs[i] for i in [2, 3]]), F(potential=potentials[1], nb=[rvs[i] for i in [1, 2, 6]]), F(potential=potentials[1], nb=[rvs[i] for i in [5, 6, 2]]), F(potential=potentials[0], nb=[rvs[i] for i in [6, 7]]), F(potential=potentials[1], nb=[rvs[i] for i in [4, 6, 6]]), ] g = Graph() g.rvs = rvs g.factors = factors g.init_nb() g.init_rv_indices()
# currently extract the log_potential_fun manually def log_potential_fun(xs): x0, x1 = xs[0], xs[1] return 4 * (x0 == 1) * (x1 == 1) + 1 * (x0 == 1) * (x1 == 0) + 1 * (x0 == 0) * (x1 == 1) + 3 * (x0 == 0) * ( x1 == 0) # return 4 * x0 * x1 + 3 * (1 - x0) * (1 - x1) + 1 * (x0 != x1) # being clever in this case # from MLNPotential import MLNPotential # p1 = MLNPotential(formula=lambda xs: xs[0] * xs[1], w=1) # d = Domain([True, False]) d = Domain([0, 1], continuous=False) N = 4 rvs = [RV(d) for n in range(N)] fs = [F(potential=p1, log_potential_fun=log_potential_fun, nb=(rvs[i], rvs[i + 1])) for i in range(N - 1)] g = Graph() g.rvs = rvs g.factors = fs g.init_nb() utils.set_log_potential_funs(g.factors_list) # OSI assumes factors have callable .log_potential_fun cg = CompressedGraphSorted(g) cg.run() print(len(g.rvs), len(g.factors)) print(len(cg.rvs), len(cg.factors)) K = 2 T = 0
seed = 0 utils.set_seed(seed=seed) utils.set_path() from Graph import Domain, RV, F, Graph # test = 'd' # disc # test = 'd2' # disc # test = 'c' # cont test = 'h' # hybrid N = 2 nodes = np.arange(N) if test == 'd': rvs = [ RV(domain=Domain(values=np.array([0, 1]), continuous=False)) for n in nodes ] def lphi_0(xs): x = xs[0] return -0.2 * (xs[0] == 0) + 1 * (x == 1) def lphi_1(xs): x = xs[0] return 0.1 * (x == 0) + 0.2 * (x == 1) def lpsi(xs): x0 = xs[0] x1 = xs[1] return -1 * (x0 == 0) * (x1 == 0) + 1 * (x0 == 0) * (x1 == 1) + \