.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "getting-started/tutorials/04_query_pc.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_getting-started_tutorials_04_query_pc.py: Query a PC ========== Assume we have constructed and trained a PC following the previous tutorials. This tutorial demonstrates how to query the PC, i.e., ask probabilistic queries about the distribution encoded by the PC. We will cover how to compute marginal and conditional probabilities. .. GENERATED FROM PYTHON SOURCE LINES 9-12 .. code-block:: Python # sphinx_gallery_thumbnail_path = 'imgs/juice.png' .. GENERATED FROM PYTHON SOURCE LINES 13-15 Generate a PC ------------- .. GENERATED FROM PYTHON SOURCE LINES 17-18 We create a simple PC consisting of two variables :math:`X_1` and :math:`X_2`: .. GENERATED FROM PYTHON SOURCE LINES 18-33 .. code-block:: Python import torch import pyjuice as juice import pyjuice.nodes.distributions as dists ni0 = juice.inputs(0, num_nodes = 2, dist = dists.Categorical(num_cats = 2)) ni1 = juice.inputs(1, num_nodes = 2, dist = dists.Categorical(num_cats = 4)) ms = juice.multiply(ni0, ni1) ns = juice.summate(ms, num_nodes = 1) ns.init_parameters() pc = juice.compile(ns) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Move the PC to a GPU: .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python device = torch.device("cuda:0") pc.to(device) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Compute marginal probabilities ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 44-45 Assume we want to compute the probabilities :math:`P(X_1 = 0)` and :math:`P(X_1 = 1)`. We need to create two tensors: a "data" tensor consisting the values of the observed variables (:math:`X_1` in this case) and another "mask" tensor indicating which variables are missing. .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: Python data = torch.tensor([[0, 0], [1, 0]]).to(device) missing_mask = torch.tensor([[False, True], [False, True]]).to(device) # True for variables to be conditioned on/are missing .. GENERATED FROM PYTHON SOURCE LINES 50-54 In the data tensor, entries corresponding missing variables will be dismissed by PyJuice and will not influence the output. The `missing_mask` can have have shape [batch_size, num_vars] or [num_vars] if for all samples we marginalize out the same subset of variables. We proceed to compute the marginal probabilities using :code:`pyjuice.queries.marginal`: .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: Python lls = juice.queries.marginal( pc, data = data, missing_mask = missing_mask ) .. GENERATED FROM PYTHON SOURCE LINES 60-62 For PCs defined on categorical variables, we can alternatively query for marginal probabilities given *soft* evidence, e.g., :math:`P(X_1 = 0 ~\text{w.p.}~ 0.3 ~\text{and}~ 1 ~\text{w.p.}~ 0.7)`. This can be done by defining `date` as a 3D tensor of size [batch_size, num_vars, num_cats]: .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: Python data = torch.tensor([[[0.4, 0.6, 0, 0], [0, 0, 0, 0]], [[0.3, 0.7, 0, 0], [0, 0, 0, 0]]]).to(device) .. GENERATED FROM PYTHON SOURCE LINES 66-69 Since :math:`X_1` has two categories and :math:`X_2` has four categories, the size of the last dimension of `data` should be 4. The soft marginal probabilities can be similarly computed by: .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: Python lls = juice.queries.marginal( pc, data = data, missing_mask = missing_mask ) .. GENERATED FROM PYTHON SOURCE LINES 75-77 Compute conditional probabilities --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 79-81 Since every conditional probability can be represented as the quotient of two marginal probabilities, one may wonder why do we need a separate function for computing conditional probabilities. In fact, with :code:`pyjuice.queries.conditional`, we can simultaneously compute a *set of* conditional probabilities. Specifically, given evidence :math:`\mathbf{E} = \mathbf{e}`, we can compute :math:`\forall X \not\in \mathbf{E}, x \in \mathrm{val}(X), P(X = x | \mathbf{e})`. .. GENERATED FROM PYTHON SOURCE LINES 83-84 Say we want to compute the conditional probability of :math:`X_2` given evidence :math:`X_1 = 0` and :math:`X_1 = 1`, respectively. We prepare the data and the mask similarly. .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. code-block:: Python data = torch.tensor([[0, 0], [1, 0]]).to(device) missing_mask = torch.tensor([[False, True], [False, True]]).to(device) # True for variables to be conditioned on/are missing .. GENERATED FROM PYTHON SOURCE LINES 89-90 The conditional probabilities are computed as follows: .. GENERATED FROM PYTHON SOURCE LINES 90-95 .. code-block:: Python outputs = juice.queries.conditional( pc, data = data, missing_mask = missing_mask, target_vars = [1] ) .. GENERATED FROM PYTHON SOURCE LINES 96-99 The parameter `target_vars` is used to indicate the subset of variables which we want to compute their conditional probabilities. Probabilities of all variables will be returned if we do not specify `target_vars`. The shape of :math:`\mathrm{outputs}` is [B, num_target_vars, num_categories]. For example, :math:`\mathrm{outputs}[1,0,3]` is the conditional probability :math:`P(X_2 = 3 | X_1 = 1)`. .. GENERATED FROM PYTHON SOURCE LINES 101-102 Similar to the marginal query, for categorical data, we can also feed *soft* evidence: .. GENERATED FROM PYTHON SOURCE LINES 102-108 .. code-block:: Python data = torch.tensor([[[0.4, 0.6, 0, 0], [0, 0, 0, 0]], [[0.3, 0.7, 0, 0], [0, 0, 0, 0]]]).to(device) missing_mask = torch.tensor([[False, True], [False, True]]).to(device) outputs = juice.queries.conditional( pc, data = data, missing_mask = missing_mask, target_vars = [1] ) .. _sphx_glr_download_getting-started_tutorials_04_query_pc.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 04_query_pc.ipynb <04_query_pc.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 04_query_pc.py <04_query_pc.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 04_query_pc.zip <04_query_pc.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_