def test_spn_construction_by_init_and_evaluation(): # building the same levels input_layer = build_spn_indicator_layer(vars) sum_layer, prod_layer = build_spn_layers(input_layer) spn = Spn(input_layer=input_layer, layers=[sum_layer, prod_layer]) res = spn.eval(I) print('First evaluation') print(res) assert_log_array_almost_equal(root_vals, res)
def test_spn_construction_by_add_and_evaluation(): spn = Spn() # building the same levels input_layer = build_spn_indicator_layer(vars) sum_layer, prod_layer = build_spn_layers(input_layer) # adding all layers to the spn spn.set_input_layer(input_layer) spn.add_layer(sum_layer) spn.add_layer(prod_layer) res = spn.eval(I) print('First evaluation') print(res) assert_log_array_almost_equal(root_vals, res)
def test_spn_construction_by_add_and_evaluation_II(): spn = Spn() # print('empty spn') # print(spn) input_layer = build_spn_smoothed_layer(vars, dicts, alpha) prod_layer = build_spn_layers_II(input_layer) # adding all layers to the spn spn.set_input_layer(input_layer) spn.add_layer(prod_layer) # print('created spn') # print(spn) res = spn.eval(I) print('First smoothed evaluation') print(res) assert_log_array_almost_equal(root_vals, res)
def test_linked_to_theano_indicator(): # creating single nodes root = SumNode() prod1 = ProductNode() prod2 = ProductNode() prod3 = ProductNode() sum1 = SumNode() sum2 = SumNode() sum3 = SumNode() sum4 = SumNode() ind1 = CategoricalIndicatorNode(var=0, var_val=0) ind2 = CategoricalIndicatorNode(var=0, var_val=1) ind3 = CategoricalIndicatorNode(var=1, var_val=0) ind4 = CategoricalIndicatorNode(var=1, var_val=1) ind5 = CategoricalIndicatorNode(var=2, var_val=0) ind6 = CategoricalIndicatorNode(var=2, var_val=1) ind7 = CategoricalIndicatorNode(var=2, var_val=2) ind8 = CategoricalIndicatorNode(var=3, var_val=0) ind9 = CategoricalIndicatorNode(var=3, var_val=1) ind10 = CategoricalIndicatorNode(var=3, var_val=2) ind11 = CategoricalIndicatorNode(var=3, var_val=3) prod4 = ProductNode() prod5 = ProductNode() prod6 = ProductNode() prod7 = ProductNode() # linking nodes root.add_child(prod1, 0.3) root. add_child(prod2, 0.3) root.add_child(prod3, 0.4) prod1.add_child(sum1) prod1.add_child(sum2) prod2.add_child(ind7) prod2.add_child(ind8) prod2.add_child(ind11) prod3.add_child(sum3) prod3.add_child(sum4) sum1.add_child(ind1, 0.3) sum1.add_child(ind2, 0.3) sum1.add_child(prod4, 0.4) sum2.add_child(ind2, 0.5) sum2.add_child(prod4, 0.2) sum2.add_child(prod5, 0.3) sum3.add_child(prod6, 0.5) sum3.add_child(prod7, 0.5) sum4.add_child(prod6, 0.5) sum4.add_child(prod7, 0.5) prod4.add_child(ind3) prod4.add_child(ind4) prod5.add_child(ind5) prod5.add_child(ind6) prod6.add_child(ind9) prod6.add_child(ind10) prod7.add_child(ind9) prod7.add_child(ind10) # building layers from nodes root_layer = SumLayerLinked([root]) prod_layer = ProductLayerLinked([prod1, prod2, prod3]) sum_layer = SumLayerLinked([sum1, sum2, sum3, sum4]) aprod_layer = ProductLayerLinked([prod4, prod5, prod6, prod7]) ind_layer = CategoricalIndicatorLayer(nodes=[ind1, ind2, ind3, ind4, ind5, ind6, ind7, ind8, ind9, ind10, ind11]) # creating the linked spn spn_linked = SpnLinked(input_layer=ind_layer, layers=[aprod_layer, sum_layer, prod_layer, root_layer]) print(spn_linked) # converting to theano repr spn_theano = SpnFactory.linked_to_theano(spn_linked) print(spn_theano) # time for some inference comparison for instance in I: print('linked') res_l = spn_linked.eval(instance) print(res_l) print('theano') res_t = spn_theano.eval(instance) print(res_t) assert_array_almost_equal(res_l, res_t)
def test_linked_to_theano_categorical(): vars = [2, 2, 3, 4] freqs = [{'var': 0, 'freqs': [1, 2]}, {'var': 1, 'freqs': [2, 2]}, {'var': 0, 'freqs': [3, 2]}, {'var': 1, 'freqs': [0, 3]}, {'var': 2, 'freqs': [1, 0, 2]}, {'var': 3, 'freqs': [1, 2, 1, 2]}, {'var': 3, 'freqs': [3, 4, 0, 1]}] # create input layer first input_layer = CategoricalSmoothedLayer(vars=vars, node_dicts=freqs) # get nodes ind_nodes = [node for node in input_layer.nodes()] root_node = ProductNode() sum1 = SumNode() sum2 = SumNode() prod1 = ProductNode() prod2 = ProductNode() sum3 = SumNode() sum4 = SumNode() # linking root_node.add_child(sum1) root_node.add_child(sum2) root_node.add_child(ind_nodes[0]) root_node.add_child(ind_nodes[1]) sum1.add_child(ind_nodes[2], 0.4) sum1.add_child(ind_nodes[3], 0.6) sum2.add_child(ind_nodes[3], 0.2) sum2.add_child(prod1, 0.5) sum2.add_child(prod2, 0.3) prod1.add_child(ind_nodes[4]) prod1.add_child(sum3) prod1.add_child(sum4) prod2.add_child(sum3) prod2.add_child(sum4) sum3.add_child(ind_nodes[5], 0.5) sum3.add_child(ind_nodes[6], 0.5) sum4.add_child(ind_nodes[5], 0.4) sum4.add_child(ind_nodes[6], 0.6) # creating layers root_layer = ProductLayerLinked([root_node]) sum_layer = SumLayerLinked([sum1, sum2]) prod_layer = ProductLayerLinked([prod1, prod2]) sum_layer2 = SumLayerLinked([sum3, sum4]) # create the linked spn spn_linked = SpnLinked(input_layer=input_layer, layers=[sum_layer2, prod_layer, sum_layer, root_layer]) print(spn_linked) # converting to theano repr spn_theano = SpnFactory.linked_to_theano(spn_linked) print(spn_theano) # time for some inference comparison for instance in I: print('linked') res_l = spn_linked.eval(instance) print(res_l) print('theano') res_t = spn_theano.eval(instance) print(res_t) assert_array_almost_equal(res_l, res_t)
def test_toy_spn_numpy_linked(): input_vec = numpy.array([[0., 0., 0.], [0., 0., 0.], [0., 1., 1.], [MARG_IND, MARG_IND, MARG_IND]]).T ind_node_1 = CategoricalIndicatorNode(var=0, var_val=0) ind_node_2 = CategoricalIndicatorNode(var=0, var_val=1) ind_node_3 = CategoricalIndicatorNode(var=1, var_val=0) ind_node_4 = CategoricalIndicatorNode(var=1, var_val=1) ind_node_5 = CategoricalIndicatorNode(var=2, var_val=0) ind_node_6 = CategoricalIndicatorNode(var=2, var_val=1) input_layer = CategoricalInputLayer(nodes=[ ind_node_1, ind_node_2, ind_node_3, ind_node_4, ind_node_5, ind_node_6 ]) n_nodes_layer_1 = 6 layer_1_sum_nodes = [SumNode() for i in range(n_nodes_layer_1)] layer_1_sum_nodes[0].add_child(ind_node_1, 0.6) layer_1_sum_nodes[0].add_child(ind_node_2, 0.4) layer_1_sum_nodes[1].add_child(ind_node_1, 0.3) layer_1_sum_nodes[1].add_child(ind_node_2, 0.7) layer_1_sum_nodes[2].add_child(ind_node_3, 0.1) layer_1_sum_nodes[2].add_child(ind_node_4, 0.9) layer_1_sum_nodes[3].add_child(ind_node_3, 0.7) layer_1_sum_nodes[3].add_child(ind_node_4, 0.3) layer_1_sum_nodes[4].add_child(ind_node_5, 0.5) layer_1_sum_nodes[4].add_child(ind_node_6, 0.5) layer_1_sum_nodes[5].add_child(ind_node_5, 0.2) layer_1_sum_nodes[5].add_child(ind_node_6, 0.8) layer_1 = SumLayer(layer_1_sum_nodes) n_nodes_layer_2 = 4 layer_2_prod_nodes = [ProductNode() for i in range(n_nodes_layer_2)] layer_2_prod_nodes[0].add_child(layer_1_sum_nodes[0]) layer_2_prod_nodes[0].add_child(layer_1_sum_nodes[2]) layer_2_prod_nodes[0].add_child(layer_1_sum_nodes[4]) layer_2_prod_nodes[1].add_child(layer_1_sum_nodes[1]) layer_2_prod_nodes[1].add_child(layer_1_sum_nodes[3]) layer_2_prod_nodes[1].add_child(layer_1_sum_nodes[5]) layer_2_prod_nodes[2].add_child(layer_1_sum_nodes[0]) layer_2_prod_nodes[2].add_child(layer_1_sum_nodes[2]) layer_2_prod_nodes[2].add_child(layer_1_sum_nodes[5]) layer_2_prod_nodes[3].add_child(layer_1_sum_nodes[1]) layer_2_prod_nodes[3].add_child(layer_1_sum_nodes[3]) layer_2_prod_nodes[3].add_child(layer_1_sum_nodes[4]) layer_2 = ProductLayer(layer_2_prod_nodes) root = SumNode() root.add_child(layer_2_prod_nodes[0], 0.2) root.add_child(layer_2_prod_nodes[1], 0.4) root.add_child(layer_2_prod_nodes[2], 0.15) root.add_child(layer_2_prod_nodes[3], 0.25) layer_3 = SumLayer([root]) spn = Spn(input_layer=input_layer, layers=[layer_1, layer_2, layer_3]) res = spn.eval(input_vec) print('First evaluation') print(res)
def test_linked_to_theano_indicator(): # creating single nodes root = SumNode() prod1 = ProductNode() prod2 = ProductNode() prod3 = ProductNode() sum1 = SumNode() sum2 = SumNode() sum3 = SumNode() sum4 = SumNode() ind1 = CategoricalIndicatorNode(var=0, var_val=0) ind2 = CategoricalIndicatorNode(var=0, var_val=1) ind3 = CategoricalIndicatorNode(var=1, var_val=0) ind4 = CategoricalIndicatorNode(var=1, var_val=1) ind5 = CategoricalIndicatorNode(var=2, var_val=0) ind6 = CategoricalIndicatorNode(var=2, var_val=1) ind7 = CategoricalIndicatorNode(var=2, var_val=2) ind8 = CategoricalIndicatorNode(var=3, var_val=0) ind9 = CategoricalIndicatorNode(var=3, var_val=1) ind10 = CategoricalIndicatorNode(var=3, var_val=2) ind11 = CategoricalIndicatorNode(var=3, var_val=3) prod4 = ProductNode() prod5 = ProductNode() prod6 = ProductNode() prod7 = ProductNode() # linking nodes root.add_child(prod1, 0.3) root.add_child(prod2, 0.3) root.add_child(prod3, 0.4) prod1.add_child(sum1) prod1.add_child(sum2) prod2.add_child(ind7) prod2.add_child(ind8) prod2.add_child(ind11) prod3.add_child(sum3) prod3.add_child(sum4) sum1.add_child(ind1, 0.3) sum1.add_child(ind2, 0.3) sum1.add_child(prod4, 0.4) sum2.add_child(ind2, 0.5) sum2.add_child(prod4, 0.2) sum2.add_child(prod5, 0.3) sum3.add_child(prod6, 0.5) sum3.add_child(prod7, 0.5) sum4.add_child(prod6, 0.5) sum4.add_child(prod7, 0.5) prod4.add_child(ind3) prod4.add_child(ind4) prod5.add_child(ind5) prod5.add_child(ind6) prod6.add_child(ind9) prod6.add_child(ind10) prod7.add_child(ind9) prod7.add_child(ind10) # building layers from nodes root_layer = SumLayerLinked([root]) prod_layer = ProductLayerLinked([prod1, prod2, prod3]) sum_layer = SumLayerLinked([sum1, sum2, sum3, sum4]) aprod_layer = ProductLayerLinked([prod4, prod5, prod6, prod7]) ind_layer = CategoricalIndicatorLayer(nodes=[ ind1, ind2, ind3, ind4, ind5, ind6, ind7, ind8, ind9, ind10, ind11 ]) # creating the linked spn spn_linked = SpnLinked( input_layer=ind_layer, layers=[aprod_layer, sum_layer, prod_layer, root_layer]) print(spn_linked) # converting to theano repr spn_theano = SpnFactory.linked_to_theano(spn_linked) print(spn_theano) # time for some inference comparison for instance in I: print('linked') res_l = spn_linked.eval(instance) print(res_l) print('theano') res_t = spn_theano.eval(instance) print(res_t) assert_array_almost_equal(res_l, res_t)
def test_linked_to_theano_categorical(): vars = [2, 2, 3, 4] freqs = [{ 'var': 0, 'freqs': [1, 2] }, { 'var': 1, 'freqs': [2, 2] }, { 'var': 0, 'freqs': [3, 2] }, { 'var': 1, 'freqs': [0, 3] }, { 'var': 2, 'freqs': [1, 0, 2] }, { 'var': 3, 'freqs': [1, 2, 1, 2] }, { 'var': 3, 'freqs': [3, 4, 0, 1] }] # create input layer first input_layer = CategoricalSmoothedLayer(vars=vars, node_dicts=freqs) # get nodes ind_nodes = [node for node in input_layer.nodes()] root_node = ProductNode() sum1 = SumNode() sum2 = SumNode() prod1 = ProductNode() prod2 = ProductNode() sum3 = SumNode() sum4 = SumNode() # linking root_node.add_child(sum1) root_node.add_child(sum2) root_node.add_child(ind_nodes[0]) root_node.add_child(ind_nodes[1]) sum1.add_child(ind_nodes[2], 0.4) sum1.add_child(ind_nodes[3], 0.6) sum2.add_child(ind_nodes[3], 0.2) sum2.add_child(prod1, 0.5) sum2.add_child(prod2, 0.3) prod1.add_child(ind_nodes[4]) prod1.add_child(sum3) prod1.add_child(sum4) prod2.add_child(sum3) prod2.add_child(sum4) sum3.add_child(ind_nodes[5], 0.5) sum3.add_child(ind_nodes[6], 0.5) sum4.add_child(ind_nodes[5], 0.4) sum4.add_child(ind_nodes[6], 0.6) # creating layers root_layer = ProductLayerLinked([root_node]) sum_layer = SumLayerLinked([sum1, sum2]) prod_layer = ProductLayerLinked([prod1, prod2]) sum_layer2 = SumLayerLinked([sum3, sum4]) # create the linked spn spn_linked = SpnLinked( input_layer=input_layer, layers=[sum_layer2, prod_layer, sum_layer, root_layer]) print(spn_linked) # converting to theano repr spn_theano = SpnFactory.linked_to_theano(spn_linked) print(spn_theano) # time for some inference comparison for instance in I: print('linked') res_l = spn_linked.eval(instance) print(res_l) print('theano') res_t = spn_theano.eval(instance) print(res_t) assert_array_almost_equal(res_l, res_t)
def test_build_linked_spn_from_scope_graph(): # # creating a region graph as an input scope graph n_cols = 2 n_rows = 2 coarse = 2 # # create initial region root_region = Region.create_whole_region(n_rows, n_cols) region_graph = create_poon_region_graph(root_region, coarse=coarse) # print(region_graph) print('# partitions', region_graph.n_partitions()) print('# regions', region_graph.n_scopes()) print(region_graph) # # k = 2 spn = build_linked_spn_from_scope_graph(region_graph, k) print(spn) print(spn.stats()) # # back to the scope graph root_layer = list(spn.root_layer().nodes()) assert len(root_layer) == 1 root = root_layer[0] scope_graph = get_scope_graph_from_linked_spn(root) print(scope_graph) assert scope_graph == region_graph # # building an spn from scratch # # building leaf nodes n_vars = 4 vars = [0, 1, 2, 3] leaves = [ CategoricalIndicatorNode(var, val) for var in range(n_vars) for val in [0, 1] ] input_layer = CategoricalIndicatorLayer(nodes=leaves, vars=vars) # # building root root_node = SumNode(var_scope=frozenset(vars)) root_layer = SumLayer([root_node]) # # building product nodes prod_list_1 = [ProductNode(var_scope=vars) for i in range(4)] prod_list_2 = [ProductNode(var_scope=vars) for i in range(4)] prod_nodes_1 = prod_list_1 + prod_list_2 product_layer_1 = ProductLayer(prod_nodes_1) for p in prod_nodes_1: root_node.add_child(p, 1.0 / len(prod_nodes_1)) # # build sum nodes sum_list_1 = [SumNode() for i in range(2)] sum_list_2 = [SumNode() for i in range(2)] sum_list_3 = [SumNode() for i in range(2)] sum_list_4 = [SumNode() for i in range(2)] sum_layer_2 = SumLayer(sum_list_1 + sum_list_2 + sum_list_3 + sum_list_4) sum_pairs = [] for s_1 in sum_list_1: for s_2 in sum_list_2: sum_pairs.append((s_1, s_2)) for p, (s_1, s_2) in zip(prod_list_1, sum_pairs): p.add_child(s_1) p.add_child(s_2) sum_pairs = [] for s_3 in sum_list_3: for s_4 in sum_list_4: sum_pairs.append((s_3, s_4)) for p, (s_3, s_4) in zip(prod_list_2, sum_pairs): p.add_child(s_3) p.add_child(s_4) # # again product nodes prod_list_3 = [ProductNode() for i in range(4)] prod_list_4 = [ProductNode() for i in range(4)] prod_list_5 = [ProductNode() for i in range(4)] prod_list_6 = [ProductNode() for i in range(4)] product_layer_3 = ProductLayer(prod_list_3 + prod_list_4 + prod_list_5 + prod_list_6) for s in sum_list_1: for p in prod_list_3: s.add_child(p, 1.0 / len(prod_list_3)) for s in sum_list_2: for p in prod_list_4: s.add_child(p, 1.0 / len(prod_list_4)) for s in sum_list_3: for p in prod_list_5: s.add_child(p, 1.0 / len(prod_list_5)) for s in sum_list_4: for p in prod_list_6: s.add_child(p, 1.0 / len(prod_list_6)) # # build sum nodes sum_list_5 = [SumNode() for i in range(2)] sum_list_6 = [SumNode() for i in range(2)] sum_list_7 = [SumNode() for i in range(2)] sum_list_8 = [SumNode() for i in range(2)] sum_layer_4 = SumLayer(sum_list_5 + sum_list_6 + sum_list_7 + sum_list_8) sum_pairs = [] for s_5 in sum_list_5: for s_7 in sum_list_7: sum_pairs.append((s_5, s_7)) for p, (s_5, s_7) in zip(prod_list_3, sum_pairs): p.add_child(s_5) p.add_child(s_7) sum_pairs = [] for s_6 in sum_list_6: for s_8 in sum_list_8: sum_pairs.append((s_6, s_8)) for p, (s_6, s_8) in zip(prod_list_4, sum_pairs): p.add_child(s_6) p.add_child(s_8) sum_pairs = [] for s_5 in sum_list_5: for s_6 in sum_list_6: sum_pairs.append((s_5, s_6)) for p, (s_5, s_6) in zip(prod_list_5, sum_pairs): p.add_child(s_5) p.add_child(s_6) sum_pairs = [] for s_7 in sum_list_7: for s_8 in sum_list_8: sum_pairs.append((s_7, s_8)) for p, (s_7, s_8) in zip(prod_list_6, sum_pairs): p.add_child(s_7) p.add_child(s_8) # # linking to input layer for s in sum_list_5: for i in leaves[0:2]: s.add_child(i, 0.5) for s in sum_list_6: for i in leaves[2:4]: s.add_child(i, 0.5) for s in sum_list_7: for i in leaves[4:6]: s.add_child(i, 0.5) for s in sum_list_8: for i in leaves[6:]: s.add_child(i, 0.5) lspn = LinkedSpn(input_layer=input_layer, layers=[ sum_layer_4, product_layer_3, sum_layer_2, product_layer_1, root_layer ]) print(lspn) print(lspn.stats()) # # trying to evaluate them input_vec = numpy.array([[1., 1., 1., 0.], [0., 0., 0., 0.], [0., 1., 1., 0.], [MARG_IND, MARG_IND, MARG_IND, MARG_IND]]).T res = spn.eval(input_vec) print('First evaluation') print(res) res = lspn.eval(input_vec) print('Second evaluation') print(res)