.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/language/plot_spn_object_hierarchy.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_language_plot_spn_object_hierarchy.py: .. _composing_spn_object_hierarchy: ===================================== Composing SPNs from Sums and Products ===================================== The alternative to the DSL is to construct SPNs directly from ``Sum`` and ``Product`` objects, demonstrated here: .. GENERATED FROM PYTHON SOURCE LINES 11-32 .. image-sg:: /auto_examples/language/images/sphx_glr_plot_spn_object_hierarchy_001.png :alt: plot spn object hierarchy :srcset: /auto_examples/language/images/sphx_glr_plot_spn_object_hierarchy_001.png :class: sphx-glr-single-img .. code-block:: default from spn.structure.leaves.parametric.Parametric import Categorical from spn.structure.Base import Sum, Product from spn.structure.Base import assign_ids, rebuild_scopes_bottom_up from spn.io.Graphics import draw_spn import matplotlib.pyplot as plt p0 = Product(children=[Categorical(p=[0.3, 0.7], scope=1), Categorical(p=[0.4, 0.6], scope=2)]) p1 = Product(children=[Categorical(p=[0.5, 0.5], scope=1), Categorical(p=[0.6, 0.4], scope=2)]) s1 = Sum(weights=[0.3, 0.7], children=[p0, p1]) p2 = Product(children=[Categorical(p=[0.2, 0.8], scope=0), s1]) p3 = Product(children=[Categorical(p=[0.2, 0.8], scope=0), Categorical(p=[0.3, 0.7], scope=1)]) p4 = Product(children=[p3, Categorical(p=[0.4, 0.6], scope=2)]) spn = Sum(weights=[0.4, 0.6], children=[p2, p4]) assign_ids(spn) rebuild_scopes_bottom_up(spn) ax = draw_spn(spn) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.254 seconds) .. _sphx_glr_download_auto_examples_language_plot_spn_object_hierarchy.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_spn_object_hierarchy.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_spn_object_hierarchy.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_