Exemple #1
0
def test_sum_node_is_complete():
    # create a sum node with a scope
    scope = frozenset({0, 2, 7, 13})
    sum_node = SumNode(var_scope=scope)

    # creating children with same scope
    children = [ProductNode(var_scope=scope) for i in range(4)]
    for prod_node in children:
        sum_node.add_child(prod_node, 1.0)

    assert sum_node.is_complete()

    # now altering one child's scope with one less var
    children[0].var_scope = frozenset({0, 7, 13})

    assert sum_node.is_complete() is False

    # now adding one more
    children[0].var_scope = scope
    children[3].var_scope = frozenset({0, 2, 7, 13, 3})

    assert not sum_node.is_complete()

    # now checking with indicator input nodes
    var = 4
    sum_node = SumNode(var_scope=frozenset({var}))
    children = [CategoricalIndicatorNode(var=var, var_val=i)
                for i in range(4)]
    for input_node in children:
        sum_node.add_child(input_node, 1.0)

    assert sum_node.is_complete()
Exemple #2
0
    def __init__(self, nodes=None, vars=None):
        """
        WRITEME
        """
        if nodes is None:
            # self._vars = vars
            nodes = [CategoricalIndicatorNode(var, i)
                     for var in range(len(vars))
                     for i in range(vars[var])]

            # self._feature_vals = [2 for i in range(len(nodes))]
        else:
            # assuming the nodes are complete and coherent
            vars_dict = {}
            for node in nodes:
                try:
                    vars_dict[node.var] += 1
                except:
                    vars_dict[node.var] = 1

            sorted_keys = sorted(vars_dict.items(), key=lambda t: t[0])
            vars = [vals for id, vals in sorted_keys]

            # self.feature_vals = compute_feature_vals(nodes)

        CategoricalInputLayer.__init__(self, nodes, vars)
        self._feature_vals = compute_feature_vals(nodes)
Exemple #3
0
def test_categorical_indicator_node_create_and_eval():

    # created a node on the first var and its first value
    ind = CategoricalIndicatorNode(0, 0)

    # seen x0 = 0 -> 1.
    ind.eval(0)
    assert ind.log_val == 0.

    # this indicator is not fired
    ind.eval(1)
    assert ind.log_val == LOG_ZERO

    # all indicators for that var are fired
    ind.eval(MARG_IND)
    assert ind.log_val == 0.

    # the var has only 2 values, but the node does not know!
    ind.eval(2)
    assert ind.log_val == LOG_ZERO
Exemple #4
0
def test_sum_layer_is_complete():
    # creating two scopes and two sum nodes
    scope1 = frozenset({0, 2, 3})
    scope2 = frozenset({10})
    sum_node_1 = SumNode(var_scope=scope1)
    sum_node_2 = SumNode(var_scope=scope2)

    # adding product nodes as children to the first, indicator the second
    for i in range(4):
        sum_node_1.add_child(ProductNode(var_scope=scope1), 1.0)
        sum_node_2.add_child(CategoricalIndicatorNode(var=10, var_val=i), 1.0)

    # creating sum layer
    sum_layer = SumLayer(nodes=[sum_node_1, sum_node_2])

    assert sum_layer.is_complete()

    # now with errors in scope
    scope3 = frozenset({6})
    sum_node_1 = SumNode(var_scope=scope1)
    sum_node_2 = SumNode(var_scope=scope3)

    # adding product nodes as children to the first, indicator the second
    for i in range(4):
        sum_node_1.add_child(ProductNode(var_scope=scope1), 1.0)
        sum_node_2.add_child(CategoricalIndicatorNode(var=10, var_val=i), 1.0)

    # creating sum layer
    sum_layer = SumLayer(nodes=[sum_node_1, sum_node_2])

    assert not sum_layer.is_complete()

    sum_node_2.var_scope = scope2

    assert sum_layer.is_complete()

    sum_node_2.children[3].var_scope = scope3

    assert not sum_layer.is_complete()
Exemple #5
0
def test_categorical_input_layer():
    print('categorical input layer')
    # I could loop through alpha as well
    alpha = 0.1

    for var_id1 in range(len(vars)):
        for var_id2 in range(len(vars)):
            for var_val1 in range(vars[var_id1]):
                print('varid1, varid2, varval1',
                      var_id1, var_id2, var_val1)
                # var_id1 = 0
                # var_val1 = 0
                node1 = CategoricalIndicatorNode(var_id1,
                                                 var_val1)
                # var_id2 = 0
                var_vals2 = vars[var_id2]
                node2 = CategoricalSmoothedNode(
                    var_id2, var_vals2, alpha, freqs[var_id2])

                # creating the generic input layer
                input_layer = CategoricalInputLayer([node1,
                                                     node2])

                # evaluating according to an observation
                input_layer.eval(obs)

                layer_evals = input_layer.node_values()
                print('layer eval nodes')
                print(layer_evals)

                # computing evaluation by hand
                val1 = 1 if var_val1 == obs[var_id1] or obs[
                    var_id1] == MARG_IND else 0
                logval1 = log(val1) if val1 == 1 else LOG_ZERO

                logval2 = compute_smoothed_ll(
                    obs[var_id2], freqs[var_id2], vars[var_id2], alpha)
                logvals = [logval1, logval2]
                print('log vals')
                print(logvals)

                for logval, eval in zip(logvals, layer_evals):
                    if logval == LOG_ZERO:
                        # for zero log check this way for correctness
                        assert IS_LOG_ZERO(eval) is True
                    else:
                        assert_almost_equal(logval, eval, PRECISION)
Exemple #6
0
def test_categorical_indicator_node_create_and_eval_keras():

    alpha = 0.0

    data = numpy.array([[1, 1, 1, 0],
                        [0, 0, 1, 0],
                        [0, 0, 1, 0],
                        [1, 0, 0, 0],
                        [1, 0, 1, 0],
                        [0, 1, 1, 0],
                        [MARG_IND, 0, 0, 1],
                        [MARG_IND, MARG_IND, MARG_IND, MARG_IND]]).astype(numpy.int32)

    input = K.placeholder(ndim=2, dtype='int32')

    for i, var in enumerate(vars):

        for j in [0, 1]:
            log_vals = []

            print('var {} val {}'.format(i, j))
            smi = CategoricalIndicatorNode(i, j)
            print(smi)
            smi.build_k(input)

            for d in data:
                # print(d)
                smi.eval(d)
                log_vals.append(smi.log_val)
                print('smi values')
                print(smi.log_val)

            eval_input_node_f = K.function([input], [smi.log_vals])
            keras_log_vals = eval_input_node_f([data])[0]
            print('keras vals')
            print(keras_log_vals)

            assert_array_almost_equal(numpy.array(log_vals)[:, numpy.newaxis],
                                      keras_log_vals,
                                      decimal=4)
Exemple #7
0
def linked_categorical_input_to_indicators(spn, input_layer=None):
    """
    Convertes a linked spn categorical input layer into an indicator one
    """

    #
    # get child, parent relations for node relinking
    child_assoc = retrieve_children_parent_assoc(spn)

    #
    # get input layer
    cat_input_layer = spn.input_layer()
    assert isinstance(cat_input_layer, CategoricalSmoothedLayerLinked)

    #
    # one indicator node for each var value
    vars = cat_input_layer.vars()
    if not vars:
        vars = list(sorted({node.var for node in cat_input_layer.nodes()}))

    feature_values = cat_input_layer.feature_vals()
    # print('vars', vars)
    # print('feature values', feature_values)

    indicator_nodes = [
        CategoricalIndicatorNode(var, val) for i, var in enumerate(vars)
        for val in range(feature_values[i])
    ]
    # for node in indicator_nodes:
    #     print(node)

    indicator_map = defaultdict(set)
    for ind_node in indicator_nodes:
        indicator_map[ind_node.var].add(ind_node)

    sum_nodes = []
    #
    # as many sum nodes as cat nodes
    for node in cat_input_layer.nodes():

        sum_node = SumNode(var_scope=frozenset([node.var]))
        sum_nodes.append(sum_node)

        for ind_node in sorted(indicator_map[node.var],
                               key=lambda x: x.var_val):
            sum_node.add_child(ind_node,
                               numpy.exp(node._var_probs[ind_node.var_val]))

        #
        # removing links to parents
        parents = child_assoc[node]
        for p_node in parents:
            #
            # assume it to be a product node
            # TODO: generalize
            assert isinstance(p_node, ProductNode)
            p_node.children.remove(node)
            p_node.add_child(sum_node)

    #
    # creating layer
    sum_layer = SumLayerLinked(sum_nodes)

    indicator_layer = CategoricalIndicatorLayerLinked(indicator_nodes)

    cat_input_layer.disconnect_layer()
    spn.set_input_layer(indicator_layer)
    spn.insert_layer(sum_layer, 0)

    return spn
Exemple #8
0
def test_categorical_indicator_layer_vars():
    # create indicator nodes first
    ind1 = CategoricalIndicatorNode(var=0, var_val=0)
    ind2 = CategoricalIndicatorNode(var=3, var_val=0)
    ind3 = CategoricalIndicatorNode(var=3, var_val=1)
    ind4 = CategoricalIndicatorNode(var=2, var_val=0)
    ind5 = CategoricalIndicatorNode(var=1, var_val=1)
    ind6 = CategoricalIndicatorNode(var=2, var_val=1)
    ind7 = CategoricalIndicatorNode(var=1, var_val=0)
    ind8 = CategoricalIndicatorNode(var=0, var_val=1)
    ind9 = CategoricalIndicatorNode(var=2, var_val=2)
    ind10 = CategoricalIndicatorNode(var=3, var_val=2)
    ind11 = CategoricalIndicatorNode(var=3, var_val=3)

    # building the layer from nodes
    layer = CategoricalIndicatorLayer(nodes=[ind1, ind2,
                                             ind3, ind4,
                                             ind5, ind6,
                                             ind7, ind8,
                                             ind9, ind10, ind11])

    # checking for the construction of the vars property
    layer_vars = layer.vars()

    assert vars == layer_vars
Exemple #9
0
def test_spn_set_get_weights():
    # create a simple spn
    root_node = SumNode()
    root_layer = SumLayer([root_node])

    prod_node_1 = ProductNode()
    prod_node_2 = ProductNode()
    root_node.add_child(prod_node_1, 0.5)
    root_node.add_child(prod_node_2, 0.5)
    prod_layer = ProductLayer([prod_node_1,
                               prod_node_2])

    sum_node_1 = SumNode()
    sum_node_2 = SumNode()
    sum_node_3 = SumNode()
    prod_node_1.add_child(sum_node_1)
    prod_node_1.add_child(sum_node_2)
    prod_node_2.add_child(sum_node_2)
    prod_node_2.add_child(sum_node_3)
    sum_layer = SumLayer([sum_node_1, sum_node_2,
                          sum_node_3])

    ind_node_1 = CategoricalIndicatorNode(var=0, var_val=1)
    ind_node_2 = CategoricalIndicatorNode(var=0, var_val=1)
    ind_node_3 = CategoricalIndicatorNode(var=0, var_val=1)
    ind_node_4 = CategoricalIndicatorNode(var=0, var_val=1)
    ind_node_5 = CategoricalIndicatorNode(var=0, var_val=1)
    input_layer = CategoricalInputLayer(nodes=[ind_node_1,
                                               ind_node_2,
                                               ind_node_3,
                                               ind_node_4,
                                               ind_node_5])
    sum_node_1.add_child(ind_node_1, 0.2)
    sum_node_1.add_child(ind_node_2, 0.2)
    sum_node_2.add_child(ind_node_2, 0.2)
    sum_node_2.add_child(ind_node_3, 0.2)
    sum_node_2.add_child(ind_node_4, 0.2)
    sum_node_3.add_child(ind_node_4, 0.2)
    sum_node_3.add_child(ind_node_5, 0.2)

    spn = Spn(input_layer=input_layer,
              layers=[sum_layer, prod_layer, root_layer])

    print(spn)

    # storing these weights
    curr_weights = spn.get_weights()

    # setting the new weights
    spn.set_weights(weights_ds)

    # getting them again
    new_weights = spn.get_weights()

    # comparing them
    assert new_weights == weights_ds

    # now setting back the previous one
    spn.set_weights(curr_weights)

    # getting them back again
    old_weights = spn.get_weights()

    # and checking
    assert old_weights == curr_weights
Exemple #10
0
def build_linked_layered_spn(print_spn=True):
    #
    # building an indicator layer
    ind_x_00 = CategoricalIndicatorNode(0, 0)
    ind_x_01 = CategoricalIndicatorNode(0, 1)
    ind_x_10 = CategoricalIndicatorNode(1, 0)
    ind_x_11 = CategoricalIndicatorNode(1, 1)
    ind_x_20 = CategoricalIndicatorNode(2, 0)
    ind_x_21 = CategoricalIndicatorNode(2, 1)

    input_layer = CategoricalIndicatorLayer(
        [ind_x_00, ind_x_01, ind_x_10, ind_x_11, ind_x_20, ind_x_21])

    #
    # sum layer
    #
    sum_node_1 = SumNode(frozenset([0]))
    sum_node_1.add_child(ind_x_00, 0.1)
    sum_node_1.add_child(ind_x_01, 0.9)

    sum_node_2 = SumNode(frozenset([0]))
    sum_node_2.add_child(ind_x_00, 0.4)
    sum_node_2.add_child(ind_x_01, 0.6)

    sum_node_3 = SumNode(frozenset([1]))
    sum_node_3.add_child(ind_x_10, 0.3)
    sum_node_3.add_child(ind_x_11, 0.7)

    sum_node_4 = SumNode(frozenset([1]))
    sum_node_4.add_child(ind_x_10, 0.6)
    sum_node_4.add_child(ind_x_11, 0.4)

    sum_node_5 = SumNode(frozenset([2]))
    sum_node_5.add_child(ind_x_20, 0.5)
    sum_node_5.add_child(ind_x_21, 0.5)

    sum_node_6 = SumNode(frozenset([2]))
    sum_node_6.add_child(ind_x_20, 0.2)
    sum_node_6.add_child(ind_x_21, 0.8)

    sum_layer_1 = SumLayerLinked([
        sum_node_1, sum_node_2, sum_node_3, sum_node_4, sum_node_5, sum_node_6
    ])

    #
    # product nodes

    #
    # xy
    prod_node_7 = ProductNode(frozenset([0, 1]))
    prod_node_7.add_child(sum_node_1)
    prod_node_7.add_child(sum_node_3)

    prod_node_8 = ProductNode(frozenset([0, 1]))
    prod_node_8.add_child(sum_node_2)
    prod_node_8.add_child(sum_node_4)

    prod_node_9 = ProductNode(frozenset([0, 1]))
    prod_node_9.add_child(sum_node_1)
    prod_node_9.add_child(sum_node_3)

    #
    # yz
    prod_node_10 = ProductNode(frozenset([1, 2]))
    prod_node_10.add_child(sum_node_4)
    prod_node_10.add_child(sum_node_5)

    prod_node_11 = ProductNode(frozenset([1, 2]))
    prod_node_11.add_child(sum_node_4)
    prod_node_11.add_child(sum_node_6)

    prod_layer_2 = ProductLayerLinked(
        [prod_node_7, prod_node_8, prod_node_9, prod_node_10, prod_node_11])

    #
    # sum nodes
    #
    # xy
    sum_node_12 = SumNode(frozenset([0, 1]))
    sum_node_12.add_child(prod_node_7, 0.1)
    sum_node_12.add_child(prod_node_8, 0.9)

    sum_node_13 = SumNode(frozenset([0, 1]))
    sum_node_13.add_child(prod_node_8, 0.7)
    sum_node_13.add_child(prod_node_9, 0.3)

    #
    # yz
    sum_node_14 = SumNode(frozenset([1, 2]))
    sum_node_14.add_child(prod_node_10, 0.6)
    sum_node_14.add_child(prod_node_11, 0.4)

    sum_layer_3 = SumLayerLinked([sum_node_12, sum_node_13, sum_node_14])

    #
    # product nodes
    prod_node_15 = ProductNode(frozenset([0, 1, 2]))
    prod_node_15.add_child(sum_node_12)
    prod_node_15.add_child(sum_node_6)

    prod_node_16 = ProductNode(frozenset([0, 1, 2]))
    prod_node_16.add_child(sum_node_13)
    prod_node_16.add_child(sum_node_5)

    prod_node_17 = ProductNode(frozenset([0, 1, 2]))
    prod_node_17.add_child(sum_node_2)
    prod_node_17.add_child(sum_node_14)

    prod_layer_4 = ProductLayerLinked(
        [prod_node_15, prod_node_16, prod_node_17])

    #
    # root
    sum_node_18 = SumNode(frozenset([0, 1, 2]))
    sum_node_18.add_child(prod_node_15, 0.2)
    sum_node_18.add_child(prod_node_16, 0.2)
    sum_node_18.add_child(prod_node_17, 0.6)

    sum_layer_5 = SumLayerLinked([sum_node_18])

    #
    # creating the spn
    layers = [
        sum_layer_1, prod_layer_2, sum_layer_3, prod_layer_4, sum_layer_5
    ]
    nodes = [node for layer in layers for node in layer.nodes()]

    spn = SpnLinked(input_layer=input_layer, layers=layers)

    if print_spn:
        print(spn)

    return spn, layers, nodes
Exemple #11
0
def test_layered_linked_spn():
    # creating single nodes
    # this code is replicated TODO: make a function
    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)

    spn = SpnFactory.layered_linked_spn(root)

    print(spn)
    print(spn.stats())
Exemple #12
0
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)
Exemple #13
0
def build_linked_spn_from_scope_graph(scope_graph,
                                      k,
                                      root_scope=None,
                                      feature_values=None):
    """
    Turning a ScopeGraph into an SPN by puttin k sum nodes for each scope
    and a combinatorial number of product nodes to wire the partition nodes

    This is the algorithm used in Poon2011 and is shown (and used) as BuildSPN in Dennis2012
    """

    if not root_scope:
        root_scope = scope_graph.root

    n_vars = len(root_scope.vars)
    if not feature_values:
        #
        # assuming binary r.v.s
        feature_values = [2 for _i in range(n_vars)]

    #
    # adding leaves
    leaves_dict = defaultdict(list)
    leaves_list = []
    for var in sorted(root_scope.vars):
        for var_val in range(feature_values[var]):
            leaf = CategoricalIndicatorNode(var, var_val)
            leaves_list.append(leaf)
            leaves_dict[var].append(leaf)

    input_layer = CategoricalIndicatorLayer(nodes=leaves_list,
                                            vars=list(sorted(root_scope.vars)))

    #
    # in a first pass we need to assign each scope/region k sum nodes
    sum_nodes_assoc = {}
    for r in scope_graph.traverse_scopes(root_scope=root_scope):

        num_sum_nodes = k

        if r == root_scope:
            num_sum_nodes = 1

        added_sum_nodes = [
            SumNode(var_scope=r.vars) for i in range(num_sum_nodes)
        ]
        #
        # creating a sum layer
        sum_layer = SumLayer(added_sum_nodes)
        sum_nodes_assoc[r] = sum_layer

        #
        # if this is a univariate scope, we link it to leaves corresponding to its r.v.
        if r.is_atomic():
            single_rv = set(r.vars).pop()
            rv_leaves = leaves_dict[single_rv]
            uniform_weight = 1.0 / len(rv_leaves)
            for s in added_sum_nodes:
                for leaf in rv_leaves:
                    s.add_child(leaf, uniform_weight)
            #
            # linking to input layer
            sum_layer.add_input_layer(input_layer)
            input_layer.add_output_layer(sum_layer)

    layers = []
    #
    # looping again to add and wire product nodes
    for r in scope_graph.traverse_scopes(root_scope=root_scope):

        sum_layer = sum_nodes_assoc[r]
        layers.append(sum_layer)

        for p in r.partitions:

            sum_layer_descs = [sum_nodes_assoc[r_p] for r_p in p.scopes]
            sum_nodes_lists = [
                list(layer.nodes()) for layer in sum_layer_descs
            ]
            num_prod_nodes = numpy.prod([len(r_p) for r_p in sum_nodes_lists])

            #
            # adding product nodes
            added_prod_nodes = [
                ProductNode(var_scope=r.vars) for i in range(num_prod_nodes)
            ]
            #
            # adding product layer and linking
            prod_layer = ProductLayer(added_prod_nodes)
            sum_layer.add_input_layer(prod_layer)
            prod_layer.add_output_layer(sum_layer)
            for desc in sum_layer_descs:
                prod_layer.add_input_layer(desc)
                desc.add_output_layer(prod_layer)
            layers.append(prod_layer)

            #
            # linking to parents
            sum_nodes_parents = sum_layer.nodes()
            for sum_node in sum_nodes_parents:
                uniform_weight = 1.0 / (len(added_prod_nodes) *
                                        len(r.partitions))
                for prod_node in added_prod_nodes:
                    sum_node.add_child(prod_node, uniform_weight)
            #
            # linking to children
            sum_nodes_to_wire = list(itertools.product(*sum_nodes_lists))

            assert len(added_prod_nodes) == len(sum_nodes_to_wire)

            for prod_node, sum_nodes in zip(added_prod_nodes,
                                            sum_nodes_to_wire):
                for sum_node in sum_nodes:
                    prod_node.add_child(sum_node)

    #
    # toposort
    layers = topological_layer_sort(layers)

    spn = LinkedSpn(layers=layers, input_layer=input_layer)

    return spn
Exemple #14
0
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)