Skip to content

Commit

Permalink
Add random MRF tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akxlr committed May 10, 2018
1 parent 5e281df commit 81c13f5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build/

tests/uai/MAR_prob/*.dfg
tests/plots
plots/
libdai/Makefile.conf
libdai/lib/libdai.a
libdai/utils/createfg
Expand Down
36 changes: 36 additions & 0 deletions tbp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,42 @@ def ising_g(N, unary_min, unary_max, pairwise_min, pairwise_max):
return Graph(g), DecomposedGraph(dg)


def random_g(N, edge_prob, unary_min, unary_max, pairwise_min, pairwise_max):
"""
Connected N-variable MRF with Ising potentials uniformly chosen within the given limits. Each possible edge is
present with independent probability edge_prob.
:return: Tuple (g, dg) containing the MRF as a Graph and DecomposedGraph instance respectively. The tensor
decomposition used is described in Wrigley, Lee and Ye (2017), supplementary material.
"""
g = []
dg = []
while not g or len(Graph(g).get_connected_components()) > 1:
for i in range(N):

# Unary potential
theta = np.random.uniform(unary_min, unary_max)
g.append(Factor([i], np.exp([theta, -theta])))
dg.append(DecomposedFactor([i], np.array([1.0]), [np.exp([[theta], [-theta]])]))

for j in range(i + 1, N):

if np.random.rand() < edge_prob:

# Pairwise potential
theta = np.random.uniform(pairwise_min, pairwise_max)
f = Factor([i, j], np.exp([[theta, -theta], [-theta, theta]]))
G, H = factorise_ising(np.exp(theta))
df = DecomposedFactor([i, j], np.array([1.0, 1.0]), [G, H])

# Check decomposed potential is equal to original
assert df.expand() == f

g.append(f)
dg.append(df)

return Graph(g), DecomposedGraph(dg)


def l1_error(marg_1, marg_2, binary=False):
"""
Mean L1 marginal error.
Expand Down
53 changes: 52 additions & 1 deletion tests/icml17.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,62 @@ def load_ising_tests(ks, sample_size=100) -> List[Tuple]:
return tests


def load_random_tests(ks, sample_size=100) -> List[Tuple]:
"""
Load the random MRF tests used to produce Wrigley, Lee and Ye (2017), Figure 2.
"""

unary_str = 1
pairwise_str = 2
edge_prob = 0.5
N = 15
tests = []

# First two tests - varying k
for i in range(sample_size):
test_name = 'random_k_{}_mixed_{}'.format(N, i)
print("Generating test case {}".format(test_name))
g, dg = tbp.random_g(N, edge_prob, -unary_str, unary_str, -pairwise_str, pairwise_str)
true_marg = g.exact_marg_elim()
tests.append({
'name': test_name,
'g': g,
'dg': dg,
'ks': ks,
'plot_series': 'mixed',
'true_marg': true_marg,
})

for i in range(sample_size):
test_name = 'random_k_{}_attractive_{}'.format(N, i)
print("Generating test case {}".format(test_name))
g, dg = tbp.random_g(N, edge_prob, -unary_str, unary_str, 0, pairwise_str)
true_marg = g.exact_marg_elim()
tests.append({
'name': test_name,
'g': g,
'dg': dg,
'ks': ks,
'plot_series': 'attractive',
'true_marg': true_marg,
})

# TODO Second two tests - varying N


return tests

def run_ising():
tests_ising = load_ising_tests(ks=[10, 100, 1000, 10000, 100000], sample_size=20)
run_tests(tests_ising, binary_err=True)
plot_tests(tests_ising, 'icml17-ising')

def run_random():
tests_random = load_random_tests(ks=[10, 100, 1000, 10000, 100000], sample_size=100)
# tests_random = load_random_tests(ks=[10, 100, 1000], ns=[10, 15], sample_size=2)
run_tests(tests_random, binary_err=True)
plot_tests(tests_random, 'icml17-random')

def run_uai():
tests_uai_2 = load_uai_tests(['linkage_*.uai', 'Promedus_*.uai'], r=2, ks=[10, 100, 1000, 10000])
tests_uai_4 = load_uai_tests(['linkage_*.uai', 'Promedus_*.uai'], r=4, ks=[10, 100, 1000, 10000])
Expand All @@ -227,6 +278,6 @@ def run_all():
run_uai()

if __name__ == '__main__':
run_ising()
run_random()


0 comments on commit 81c13f5

Please sign in to comment.