# 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.)) # ] w0 = 0.1 factors = [ F(nb=(rvs[0], rvs[2], rvs[3]), potential=HybridQuadraticPotential(A=w0 * np.array( [np.array([[-1., 0], [0, 0]]), np.array([[-1., 0.], [0., 0.]])]), b=w0 * np.array([[16., 0], [-14., 0.]]), c=w0 * np.array([-64., -49.]))), # F(nb=(rvs[0], rvs[1]), potential=MLNPotential(lambda x: imp_op(x[0] * x[1], x[2]), w=1)), F(nb=(rvs[0], rvs[1]), potential=MLNPotential(lambda x: and_op(x[0], x[1]), w=1)), # F(nb=(rvs[2],), potential=QuadraticPotential(A=-0.5 * np.ones([1, 1]), b=np.zeros([1]), c=0.)) F(nb=(rvs[2], rvs[3]), potential=QuadraticPotential(A=-0.5 * np.eye(2), b=np.array([1., 2.]), c=0.)) # ensure normalizability ] # all disc potentials must be converted to TablePotential to be used by baseline
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]), log_potential_fun=LogTable(np.array([[2., 0], [-0.1, 1], [0, 0.2]]))), F(nb=(rvs[2], ), log_potential_fun=LogQuadratic(A=-0.5 * np.ones([1, 1]), b=np.zeros([1]), c=0.)) ] Vd = [rv for rv in rvs if rv.domain_type[0] == 'd'] # list of of discrete rvs Vc = [rv for rv in rvs if rv.domain_type[0] == 'c'] # list of cont rvs Vd_idx = {n: i for (i, n) in enumerate(Vd)} Vc_idx = {n: i for (i, n) in enumerate(Vc)}
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 lr = 1e-1
# (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() # from FactorGraph import FactorGraph
covs = np.array([np.eye(Nc)] * len(rvs[0].values)) # 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]), 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.)) # ] factors = [ F( nb=(rvs[0], rvs[2], rvs[3]), # potential=MLNPotential(lambda x: (1 - x[0]) * eq_op(x[1], 4.) + x[0] * eq_op(x[1], x[2]), w=0.2)), potential=MLNPotential( lambda x: (1 - x[0]) * eq_op(x[1], 8.) + x[0] * eq_op(x[2], -7.), w=0.5)), # F(nb=(rvs[0], rvs[1]), potential=MLNPotential(lambda x: imp_op(x[0] * x[1], x[2]), w=1)), F(nb=(rvs[0], rvs[1]), potential=MLNPotential(lambda x: and_op(x[0], x[1]), w=0.1)), # F(nb=(rvs[2],), potential=QuadraticPotential(A=-0.5 * np.ones([1, 1]), b=np.zeros([1]), c=0.)) F(nb=(rvs[2], rvs[3]), potential=QuadraticPotential(A=-0.5 * np.eye(2), b=np.array([1., 0.]), c=0.)) # ensure normalizability ] g = Graph() # not really needed here; just to conform to existing API g.rvs = rvs
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) + \ 2 * (x0 == 1) * (x1 == 0) + -1 * (x0 == 1) * (x1 == 1) factors = [ F(log_potential_fun=lphi_0, nb=[rvs[0]]), F(log_potential_fun=lphi_1, nb=[rvs[1]]), F(log_potential_fun=lpsi, nb=[rvs[0], rvs[1]]), ] Z = 0 for s0 in rvs[0].values: for s1 in rvs[1].values: Z += np.exp(lphi_0([s0]) + lphi_1([s1]) + lpsi([s0, s1])) print('true -log Z =', -np.log(Z)) elif test == 'd2': rvs = [ RV(domain=Domain(values=np.array([0, 1]), continuous=False)), RV(domain=Domain(values=np.array([0, 1, 2]), continuous=False)) ]