def test_linked_random_spn_top_down(): # number small parameters n_levels = 10 vars = [2, 3, 2, 2, 4] n_max_children = 2 n_scope_children = 3 max_scope_split = 2 merge_prob = 0.5 # building it print('creating random spn') rand_gen = random.Random(789) # # doing this for more than once n_times = 10 for _i in range(n_times): spn = SpnFactory.linked_random_spn_top_down(vars, n_levels, n_max_children, n_scope_children, max_scope_split, merge_prob, rand_gen=rand_gen) # printing for comparison print(spn) print(spn.stats()) assert spn.is_valid() # translating to theano representation theano_spn = SpnFactory.linked_to_theano(spn) print(theano_spn) print(theano_spn.stats()) # # looking for the same computations # time for some inference comparison for instance in II: print('linked') res_l = spn.eval(instance) print(res_l) print('theano') res_t = theano_spn.eval(instance) print(res_t) assert_array_almost_equal(res_l, res_t)
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_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)