Tutorial 1a: Building the SPN Graph Using Single Op Nodes

In [1]:
import libspn as spn

Build

In [2]:
iv_x = spn.IVs(num_vars=2, num_vals=2, name="iv_x")
sum_11 = spn.Sum((iv_x, [0,1]), name="sum_11")
sum_12 = spn.Sum((iv_x, [0,1]), name="sum_12")
sum_21 = spn.Sum((iv_x, [2,3]), name="sum_21")
sum_22 = spn.Sum((iv_x, [2,3]), name="sum_22")
prod_1 = spn.Product(sum_11, sum_21, name="prod_1")
prod_2 = spn.Product(sum_11, sum_22, name="prod_2")
prod_3 = spn.Product(sum_12, sum_22, name="prod_3")
root = spn.Sum(prod_1, prod_2, prod_3, name="root")
iv_y = root.generate_ivs(name="iv_y") # Can be added manually
spn.generate_weights(root, init_value=spn.ValueType.RANDOM_UNIFORM(0, 1)) # Can be added manually

Inspect

In [3]:
# Inspect
print(root.get_num_nodes())
print(root.get_scope())
print(root.is_valid())
15
[Scope({iv_x:0, iv_x:1, iv_y:0})]
True

Visualize the SPN Graph

In [4]:
# Visualize SPN graph
spn.display_spn_graph(root)