deeprob.spn.algorithms package

Submodules

deeprob.spn.algorithms.evaluation module

deeprob.spn.algorithms.evaluation.parallel_layerwise_eval(layers, eval_func, reverse=False, n_jobs=- 1)[source]

Execute a function per node layerwise in parallel.

Parameters
  • layers (List[List[Node]]) – The layers, i.e., the layered topological ordering.

  • eval_func (Callable[[Node], None]) – The evaluation function for a given node.

  • reverse (bool) – Whether to reverse the layered topological ordering.

  • n_jobs (int) – The number of parallel jobs. It follows the joblib’s convention.

deeprob.spn.algorithms.evaluation.eval_bottom_up(root, x, leaf_func, node_func, leaf_func_kwargs=None, node_func_kwargs=None, return_results=False, n_jobs=0)[source]

Evaluate the SPN bottom up given some inputs and leaves and nodes evaluation functions.

Parameters
  • root (Node) – The root of the SPN.

  • x (ndarray) – The inputs.

  • leaf_func (Callable[[Leaf, ndarray, Any], ndarray]) – The function to compute at each leaf node.

  • node_func (Callable[[Node, ndarray, Any], ndarray]) – The function to compute at each inner node.

  • leaf_func_kwargs (Optional[dict]) – The optional parameters of the leaf evaluation function.

  • node_func_kwargs (Optional[dict]) – The optional parameters of the inner nodes evaluation function.

  • return_results (bool) – A flag indicating if this function must return the log likelihoods of each node of the SPN.

  • n_jobs (int) – The number of parallel jobs. It follows the joblib’s convention. Set to 0 to disable.

Returns

The outputs. Additionally, it returns the output of each node.

Raises

ValueError – If a parameter is out of domain.

Return type

Union[ndarray, Tuple[ndarray, ndarray]]

deeprob.spn.algorithms.evaluation.eval_top_down(root, x, lls, leaf_func, sum_func, leaf_func_kwargs=None, sum_func_kwargs=None, inplace=False, n_jobs=0)[source]

Evaluate the SPN top down given some inputs, the likelihoods of each node and a leaves evaluation function. The leaves to evaluate are chosen by following the nodes given by the sum nodes evaluation function.

Parameters
  • root (Node) – The root of the SPN.

  • x (ndarray) – The inputs with some NaN values.

  • lls (ndarray) – The log-likelihoods of each node.

  • leaf_func (Callable[[Leaf, ndarray, Any], ndarray]) – The leaves evaluation function.

  • sum_func (Callable[[Sum, ndarray, Any], ndarray]) – The sum nodes evaluation function.

  • leaf_func_kwargs (Optional[dict]) – The optional parameters of the leaf evaluation function.

  • sum_func_kwargs (Optional[dict]) – The optional parameters of the sum nodes evaluation function.

  • inplace (bool) – Whether to make inplace assignments.

  • n_jobs (int) – The number of parallel jobs. It follows the joblib’s convention. Set to 0 to disable.

Returns

The NaN-filled inputs.

Raises

ValueError – If a parameter is out of domain.

Return type

ndarray

deeprob.spn.algorithms.gradient module

deeprob.spn.algorithms.gradient.eval_backward(root, lls)[source]

Compute the log-gradients at each SPN node.

Parameters
  • root (Node) – The root of the SPN.

  • lls (ndarray) – The log-likelihoods at each node.

Returns

The log-gradients w.r.t. the nodes.

Raises

ValueError – If a parameter is out of domain.

Return type

ndarray

deeprob.spn.algorithms.inference module

deeprob.spn.algorithms.inference.likelihood(root, x, return_results=False, n_jobs=0)[source]

Compute the likelihoods of the SPN given some inputs.

Parameters
  • root (Node) – The root of the SPN.

  • x (ndarray) – The inputs. They can be marginalized using NaNs.

  • return_results (bool) – A flag indicating if this function must return the likelihoods of each node of the SPN.

  • n_jobs (int) – The number of parallel jobs. It follows the joblib’s convention. Set to 0 to disable.

Returns

The likelihood values. Additionally, it returns the likelihood values of each node.

Return type

Union[ndarray, Tuple[ndarray, ndarray]]

deeprob.spn.algorithms.inference.log_likelihood(root, x, return_results=False, n_jobs=0)[source]

Compute the logarithmic likelihoods of the SPN given some inputs.

Parameters
  • root (Node) – The root of the SPN.

  • x (ndarray) – The inputs. They can be marginalized using NaNs.

  • return_results (bool) – A flag indicating if this function must return the log likelihoods of each node of the SPN.

  • n_jobs (int) – The number of parallel jobs. It follows the joblib’s convention. Set to 0 to disable.

Returns

The log likelihood values. Additionally, it returns the log likelihood values of each node.

Return type

Union[ndarray, Tuple[ndarray, ndarray]]

deeprob.spn.algorithms.inference.mpe(root, x, inplace=False, n_jobs=0)[source]

Compute the Most Probable Explanation of a SPN given some inputs.

Parameters
  • root (Node) – The root of the SPN.

  • x (ndarray) – The inputs. They can be marginalized using NaNs.

  • inplace (bool) – Whether to make inplace assignments.

  • n_jobs (int) – The number of parallel jobs. It follows the joblib’s convention. Set to 0 to disable.

Returns

The NaN-filled inputs.

Return type

ndarray

deeprob.spn.algorithms.inference.node_likelihood(node, x)[source]

Compute the likelihood of a node given the list of likelihoods of its children.

Parameters
  • node (Node) – The internal node.

  • x (ndarray) – The array of likelihoods of the children.

Returns

The likelihoods of the node given the inputs.

Return type

ndarray

deeprob.spn.algorithms.inference.node_log_likelihood(node, x)[source]

Compute the log-likelihood of a node given the list of log-likelihoods of its children.

Parameters
  • node (Node) – The internal node.

  • x (ndarray) – The array of log-likelihoods of the children.

Returns

The log-likelihoods of the node given the inputs.

Return type

ndarray

deeprob.spn.algorithms.inference.leaf_mpe(node, x)[source]

Compute the maximum likelihood estimate of a leaf node.

Parameters
  • node (Leaf) – The leaf node.

  • x (ndarray) – The inputs with some NaN values.

Returns

The most proable explanation.

Return type

ndarray

deeprob.spn.algorithms.inference.sum_mpe(node, lls)[source]

Choose the branch that maximize the posterior estimate likelihood.

Parameters
  • node (Sum) – The sum node.

  • lls (ndarray) – The log-likelihoods of the children nodes.

Returns

The branch that maximize the posterior estimate likelihood.

Return type

ndarray

deeprob.spn.algorithms.moments module

deeprob.spn.algorithms.moments.moment(root, order=1)[source]

Compute non-central moments of a given order of a smooth and decomposable SPN.

Parameters
  • root (Node) – The root of the SPN.

  • order (int) – The order of the moment. If scalar, it will be used for all the random variables.

Returns

The non-central moments with respect to each variable in the scope.

Raises

ValueError – If the order of the moment is negative.

Return type

ndarray

deeprob.spn.algorithms.moments.leaf_moment(node, x, order)[source]

Compute the moment of a leaf node.

Parameters
  • node (Leaf) – The leaf node.

  • x (ndarray) – The inputs of the leaf. Actually, it’s used only to infer the output shape.

  • order (int) – The order of the moment.

Returns

The moment of the leaf node.

Return type

ndarray

deeprob.spn.algorithms.moments.expectation(root)[source]

Compute the expectation values of a SPN w.r.t. each of the random variables.

Parameters

root (Node) – The root of the SPN.

Returns

The expectation w.r.t. each of the random variables.

Return type

ndarray

deeprob.spn.algorithms.moments.variance(root)[source]

Compute the variance values of a SPN w.r.t. each of the random variables.

Parameters

root (Node) – The root of the SPN.

Returns

The variance w.r.t. each of the random variables.

Return type

ndarray

deeprob.spn.algorithms.moments.skewness(root)[source]

Compute the skewness values of a SPN w.r.t. each of the random variables.

Parameters

root (Node) – The root of the SPN.

Returns

The skewness w.r.t. each of the random variables.

Return type

ndarray

deeprob.spn.algorithms.moments.kurtosis(root)[source]

Compute the kurtosis values of a SPN w.r.t. each of the random variables. This function returns the kurtosis based on Fisher’s definition, i.e. 3.0 is subtracted from the result to give 0.0 for a normal distribution.

Parameters

root (Node) – The root of the SPN.

Returns

The kurtosis w.r.t. each of the random variables.

Return type

ndarray

deeprob.spn.algorithms.sampling module

deeprob.spn.algorithms.sampling.sample(root, x, inplace=False, n_jobs=0)[source]

Sample some features from the distribution represented by the SPN.

Parameters
  • root (Node) – The root of the SPN.

  • x (ndarray) – The inputs with possible NaN values to fill with sampled values.

  • inplace (bool) – Whether to make inplace assignments.

  • n_jobs (int) – The number of parallel jobs. It follows the joblib’s convention. Set to 0 to disable. Warning: disrupts seed determinism.

Returns

The inputs that are NaN-filled with samples from appropriate distributions.

Return type

ndarray

deeprob.spn.algorithms.sampling.leaf_sample(node, x)[source]

Sample some values from the distribution leaf.

Parameters
  • node (Leaf) – The distribution leaf node.

  • x (ndarray) – The inputs with possible NaN values to fill with sampled values.

Returns

The completed samples.

Return type

ndarray

deeprob.spn.algorithms.sampling.sum_sample(node, lls)[source]

Choose the sub-distribution from which sample.

Parameters
  • node (Sum) – The sum node.

  • lls (ndarray) – The log-likelihoods of the children nodes.

Returns

The index of the sub-distribution to follow.

Return type

ndarray

deeprob.spn.algorithms.structure module

deeprob.spn.algorithms.structure.prune(root, copy=True)[source]

Prune (or simplify) the given SPN to a minimal and equivalent SPN.

Parameters
  • root (Node) – The root of the SPN.

  • copy (bool) – Whether to copy the SPN before pruning it.

Returns

A minimal and equivalent SPN.

Raises
  • ValueError – If the SPN structure is not a directed acyclic graph (DAG).

  • ValueError – If an unknown node type is found.

Return type

Node

deeprob.spn.algorithms.structure.marginalize(root, keep_scope, copy=True)[source]

Marginalize some random variables of a SPN, obtaining the compilation of a marginal query.

Parameters
  • root (Node) – The root of the SPN to marginalize.

  • keep_scope (List[int]) – The scope of the random variables to keep. All the other random variables will be marginalized.

  • copy (bool) – Whether to copy the SPN before marginalizing it.

Returns

A SPN in which an EVI query is equivalent to a MAR query under the given scope.

Raises
  • ValueError – If the scope of the random variables to keep is not valid.

  • ValueError – If the SPN structure is not a directed acyclic graph (DAG).

  • ValueError – If an unknown node type is found.

  • NotImplementedError – If non-BinaryCLT multivariate leaves are found.

Return type

Node

Module contents