<jupyter_start><jupyter_text>Tutorial 1c: Building the SPN Graph Using A GeneratorRandomly stuctured SPNs can be generated using only the leaf layer and some parameters to specify the number of sums per scope and the number of decompositions at every product layer.<jupyter_code>import libspn as spn
import tensorflow as tf<jupyter_output><empty_output><jupyter_text>Build the SPN<jupyter_code>indicator_leaves = spn.IndicatorLeaf(
    num_vars=2, num_vals=2, name="indicator_x")

# Generate random structure with 1 decomposition per product layer
# 2 subsets of variables per product (so 2 children) and 2 sums/mixtures per scope
dense_spn_generator = spn.DenseSPNGenerator(num_decomps=1, num_subsets=2, num_mixtures=2)
root = dense_spn_generator.generate(indicator_leaves)

# Connect a latent indicator
indicator_y = root.generate_latent_indicators(name="indicator_y") # Can be added manually

# Generate weights
spn.generate_weights(root, initializer=tf.initializers.random_uniform()) # Can be added manually<jupyter_output><empty_output><jupyter_text>Inspect<jupyter_code># Inspect
print(root.get_num_nodes())
print(root.get_scope())
print(root.is_valid())<jupyter_output>7
[Scope({indicator_x_1:1, indicator_x_1:0, indicator_y:0})]
True<jupyter_text>Visualize the SPN GraphThe visualization below uses `graphviz`. Depending on your setup (e.g. `jupyter lab` vs. `jupyter notebook`) this might fail to show. At least `Chrome` + `jupyter notebook` seems to work.<jupyter_code># Visualize SPN graph
spn.display_spn_graph(root)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08536585365853659, 'fraction_numerical': 0.010269576379974325, 'mean_word_length': 7.758426966292135, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '64790', 'license': 'MIT', 'n_tokens_mistral': 535, 'n_tokens_neox': 519, 'n_words': 141, 'path': 'ipynb/Tutorial 1c.ipynb', 'repo_name': 'pronobis/libspn', 'size': '1558'}