deeprob.spn.structure package

Submodules

deeprob.spn.structure.cltree module

class deeprob.spn.structure.cltree.BinaryCLT(scope, root=None, tree=None, params=None)[source]

Bases: Leaf

Initialize Binary Chow-Liu Tree (CLT) multi-variate leaf node.

Parameters
  • scope (List[int]) – The scope of the leaf.

  • root (Optional[int]) – The root node of the CLT. If None it will be chosen randomly.

  • tree (Optional[Union[List[int], np.ndarray]]) – A sequence of variable ids predecessors (encoding the tree structure).

  • params (Optional[Union[List[List[List[float]]], np.ndarray]]) – The CLT conditional probability tables (CPTs), as a (N, 2, 2) Numpy array in logarithmic scale. Note that params[i, l, k] = log P(X_i=k | Pa(X_i)=l).

Raises
  • ValueError – If the root variable is not in scope.

  • ValueError – If the tree structure is not compatible with the number of variables and root node.

  • ValueError – If the CPTs parameters are invalid.

LEAF_TYPE = 1
static compute_clt_parameters(bfs, tree, priors, joints)[source]

Compute the parameters of the CLTree given the tree structure and the priors and joints distributions.

This function returns the conditional probability tables (CPTs) in a tensorized form. Note that params[i, l, k] = P(X_i=k | Pa(X_i)=l). A special case is made for the root distribution which is not conditioned. Note that params[root, :, k] = P(X_root=k).

Parameters
  • bfs (ndarray) – The bfs structure, i.e. a sequence of successors in a breadth-first traversal.

  • tree (ndarray) – The tree structure, i.e. a sequence of predecessors in a tree structure.

  • priors (ndarray) – The priors distributions.

  • joints (ndarray) – The joints distributions.

Returns

The conditional probability tables (CPTs) in a tensorized form.

Return type

ndarray

em_init(random_state)[source]

Random initialize the leaf’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

em_step(stats, data, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • data (ndarray) – The data regarding random variables of the leaf.

  • step_size (float) – The step size of update.

fit(data, domain, alpha=0.1, random_state=None, **kwargs)[source]

Fit the distribution parameters (and structure if necessary) given the domain and some training data.

Parameters
  • data (ndarray) – The training data.

  • domain (List[list]) – The domain of the distribution leaf.

  • alpha (float) – The Laplace smoothing factor.

  • random_state (Optional[Union[int, RandomState]]) – The random state. It can be either None, a seed integer or a Numpy RandomState.

  • kwargs – Optional parameters.

Raises
message_passing(x, obs_mask, return_lls=True, reduce='mar')[source]

Compute the messages passed from the leaves to the root node.

Parameters
  • x (ndarray) – The input data.

  • obs_mask (ndarray) – The mask of observed values.

  • return_lls (bool) – Whether to compute and return the log-likelihoods.

  • reduce (str) – The method used to reduce the messages of missing values. It can be either ‘mar’ (marginalize the message) or ‘mpe’ (maximum probable explanation).

Returns

The messages array if return_lls is False. The log-likelihoods if return_lls is True.

Return type

ndarray

likelihood(x)[source]

Compute the likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

mpe(x)[source]

Compute the maximum at posteriori values.

Parameters

x (ndarray) – The inputs.

Returns

The distribution’s maximum at posteriori values.

Return type

ndarray

sample(x)[source]

Sample from the leaf distribution.

Parameters

x (ndarray) – The samples with possible NaN values.

Returns

The completed samples.

Return type

ndarray

moment(k=1)[source]

Compute the moment of a given order.

Parameters

k (int) – The order of the moment.

Returns

The moment of order k.

Return type

float

params_count()[source]

Get the number of parameters of the distribution leaf.

Returns

The number of parameters.

Return type

int

params_dict()[source]

Get a dictionary representation of the distribution parameters.

Returns

A dictionary containing the distribution parameters.

Return type

dict

to_pc()[source]

Convert a Chow-Liu Tree into a smooth, deterministic and structured-decomposable PC

Returns

A smooth, deterministic and structured-decomposable PC.

Return type

Node

get_scopes()[source]

Return a list containing the scope of every node in the PC equivalent to the current CLTree (see to_pc() method). Every scope occurs once in the list.

Returns

The list of scopes.

deeprob.spn.structure.io module

deeprob.spn.structure.io.save_digraph_json(graph, f)[source]

Save a NetworkX directed graph by using the JSON format.

Parameters
  • graph (DiGraph) – The NetworkX directed graph.

  • f (Union[IO, PathLike, str]) – A file-like object or a filepath of the output JSON file.

deeprob.spn.structure.io.load_digraph_json(f)[source]

Load a NetworkX directed graph by using the JSON format.

Parameters

f (Union[IO, PathLike, str]) – A file-like object or a filepath of the input JSON file.

Returns

The NetworkX directed graph.

Return type

DiGraph

deeprob.spn.structure.io.save_spn_json(root, f)[source]

Save SPN to file by using the JSON format.

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

  • f (Union[IO, PathLike, str]) – A file-like object or a filepath of the output JSON file.

deeprob.spn.structure.io.load_spn_json(f, leaves=None)[source]

Load SPN from file by using the JSON format.

Parameters
  • f (Union[IO, PathLike, str]) – A file-like object or a filepath of the input JSON file.

  • leaves (Optional[List[Type[Leaf]]]) – An optional list of custom leaf classes. Useful when dealing with user-defined leaves.

Returns

The loaded SPN with initialied ids for each node.

Raises

ValueError – If multiple custom leaf classes with the same name are defined.

Return type

Node

deeprob.spn.structure.io.save_binary_clt_json(clt, f)[source]

Save Binary Chow-Liu Tree (CLT) to file by using the JSON format.

Parameters
deeprob.spn.structure.io.load_binary_clt_json(f)[source]

Load Binary Chow-Liu Tree (CLT) from file by using the JSON format.

Parameters

f (Union[IO, PathLike, str]) – A file-like object or a filepath of the input JSON file.

Returns

The loaded binary CLT.

Return type

BinaryCLT

deeprob.spn.structure.io.spn_to_digraph(root)[source]

Convert a SPN to a NetworkX directed graph.

Parameters

root (Node) – The root node of the SPN.

Returns

The corresponding NetworkX directed graph.

Raises

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

Return type

DiGraph

deeprob.spn.structure.io.digraph_to_spn(graph, leaf_map)[source]

Convert a NetworkX directed graph to a SPN.

Parameters
  • graph (DiGraph) – The NetworkX directed graph.

  • leaf_map (Dict[str, Type[Leaf]]) – The leaf distributions mapper dictionary.

Returns

The corresponding SPN.

Raises

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

Return type

Node

deeprob.spn.structure.io.binary_clt_to_digraph(clt)[source]

Convert a binary Chow-Liu Tree (CLT) to a NetworkX directed graph.

Parameters

clt (BinaryCLT) – The binary CLT.

Returns

The corresponding NetworkX directed graph.

Raises

ValueError – If the CLT is not initialized.

Return type

DiGraph

deeprob.spn.structure.io.digraph_to_binary_clt(graph)[source]

Convert a NetworkX directed graph to a binary Chow-Liu Tree (CLT).

Parameters

graph (DiGraph) – The NetworkX directed graph.

Returns

The corresponding Chow-Liu Tree.

Raises

ValueError – If the graph is not a tree.

Return type

BinaryCLT

deeprob.spn.structure.io.plot_spn(root, f)[source]

Plot a SPN into file.

Parameters
  • root (Node) – The SPN root node.

  • f (Union[IO, PathLike, str]) – A file-like object or a filepath of the output file.

Raises
  • ValueError – If an unknown node type is found.

  • ValueError – If the SPN structure is not a DAG.

deeprob.spn.structure.io.plot_binary_clt(clt, f, show_weights=True)[source]

Plot a binary Chow-Liu Tree (CLT) into file.

Parameters
  • clt (BinaryCLT) – The binary CLT.

  • f (Union[IO, PathLike, str]) – A file-like object or a filepath of the output file.

  • show_weights (bool) – Whether to show the conditional probability tables (CPTs).

deeprob.spn.structure.leaf module

class deeprob.spn.structure.leaf.LeafType(value)[source]

Bases: Enum

The type of the distribution leaf. It can be either discrete or continuous.

DISCRETE = 1
CONTINUOUS = 2
class deeprob.spn.structure.leaf.Leaf(scope)[source]

Bases: Node

Initialize a leaf node given its scope.

Parameters
  • scope (Union[int, List[int]]) – The scope of the leaf.

  • kwargs – Additional arguments.

LEAF_TYPE = None
abstract em_init(random_state)[source]

Random initialize the leaf’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

abstract em_step(stats, data, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • data (ndarray) – The data regarding random variables of the leaf.

  • step_size (float) – The step size of update.

abstract fit(data, domain, **kwargs)[source]

Fit the distribution parameters given the domain and some training data.

Parameters
  • data (ndarray) – The training data.

  • domain (Union[list, tuple]) – The domain of the distribution leaf.

  • kwargs – Optional parameters.

Raises

ValueError – If a parameter is out of domain.

abstract likelihood(x)[source]

Compute the likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

abstract log_likelihood(x)[source]

Compute the logarithmic likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

abstract mpe(x)[source]

Compute the maximum at posteriori values.

Parameters

x (ndarray) – The inputs.

Returns

The distribution’s maximum at posteriori values.

Return type

ndarray

abstract sample(x)[source]

Sample from the leaf distribution.

Parameters

x (ndarray) – The samples with possible NaN values.

Returns

The completed samples.

Return type

ndarray

abstract moment(k=1)[source]

Compute the moment of a given order.

Parameters

k (int) – The order of the moment.

Returns

The moment of order k.

Return type

float

abstract params_count()[source]

Get the number of parameters of the distribution leaf.

Returns

The number of parameters.

Return type

int

abstract params_dict()[source]

Get a dictionary representation of the distribution parameters.

Returns

A dictionary containing the distribution parameters.

Return type

dict

class deeprob.spn.structure.leaf.Bernoulli(scope, p=0.5)[source]

Bases: Leaf

Initialize a Bernoulli leaf node given its scope.

Parameters
  • scope (int) – The scope of the leaf.

  • p (float) – The Bernoulli probability.

Raises

ValueError – If a parameter is out of domain.

LEAF_TYPE = 1
fit(data, domain, alpha=0.1, **kwargs)[source]

Fit the distribution parameters given the domain and some training data.

Parameters
  • data (ndarray) – The training data.

  • domain (list) – The domain of the distribution leaf.

  • alpha (float) – The Laplace smoothing factor.

  • kwargs – Optional parameters.

Raises

ValueError – If a parameter is out of domain.

em_init(random_state)[source]

Random initialize the leaf’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

em_step(stats, data, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • data (ndarray) – The data regarding random variables of the leaf.

  • step_size (float) – The step size of update.

likelihood(x)[source]

Compute the likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

mpe(x)[source]

Compute the maximum at posteriori values.

Parameters

x (ndarray) – The inputs.

Returns

The distribution’s maximum at posteriori values.

Return type

ndarray

sample(x)[source]

Sample from the leaf distribution.

Parameters

x (ndarray) – The samples with possible NaN values.

Returns

The completed samples.

Return type

ndarray

moment(k=1)[source]

Compute the moment of a given order.

Parameters

k (int) – The order of the moment.

Returns

The moment of order k.

Return type

float

params_count()[source]

Get the number of parameters of the distribution leaf.

Returns

The number of parameters.

params_dict()[source]

Get a dictionary representation of the distribution parameters.

Returns

A dictionary containing the distribution parameters.

class deeprob.spn.structure.leaf.Categorical(scope, categories=None, probabilities=None)[source]

Bases: Leaf

Initialize a Categorical leaf node given its scope.

Parameters
LEAF_TYPE = 1
fit(data, domain, alpha=0.1, **kwargs)[source]

Fit the distribution parameters given the domain and some training data.

Parameters
  • data (ndarray) – The training data.

  • domain (list) – The domain of the distribution leaf.

  • alpha (float) – The Laplace smoothing factor.

  • kwargs – Optional parameters.

Raises

ValueError – If a parameter is out of domain.

em_init(random_state)[source]

Random initialize the leaf’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

Raises

ValueError – If the categories are not initialized.

em_step(stats, data, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • data (ndarray) – The data regarding random variables of the leaf.

  • step_size (float) – The step size of update.

likelihood(x)[source]

Compute the likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

mpe(x)[source]

Compute the maximum at posteriori values.

Parameters

x (ndarray) – The inputs.

Returns

The distribution’s maximum at posteriori values.

Return type

ndarray

sample(x)[source]

Sample from the leaf distribution.

Parameters

x (ndarray) – The samples with possible NaN values.

Returns

The completed samples.

Return type

ndarray

moment(k=1)[source]

Compute the moment of a given order.

Parameters

k (int) – The order of the moment.

Returns

The moment of order k.

Return type

float

params_count()[source]

Get the number of parameters of the distribution leaf.

Returns

The number of parameters.

Return type

int

params_dict()[source]

Get a dictionary representation of the distribution parameters.

Returns

A dictionary containing the distribution parameters.

Return type

dict

class deeprob.spn.structure.leaf.Isotonic(scope, densities=None, breaks=None)[source]

Bases: Leaf

Initialize a histogram-Isotonic leaf node given its scope.

Parameters
Raises

ValueError – If a parameter is out of domain.

LEAF_TYPE = 2
fit(data, domain, alpha=0.1, **kwargs)[source]

Fit the distribution parameters given the domain and some training data.

Parameters
  • data (ndarray) – The training data.

  • domain (tuple) – The domain of the distribution leaf.

  • alpha (float) – The Laplace smoothing factor.

  • kwargs – Optional parameters.

Raises

ValueError – If a parameter is out of domain.

em_init(random_state)[source]

Random initialize the leaf’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

em_step(stats, data, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • data (ndarray) – The data regarding random variables of the leaf.

  • step_size (float) – The step size of update.

likelihood(x)[source]

Compute the likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

mpe(x)[source]

Compute the maximum at posteriori values.

Parameters

x (ndarray) – The inputs.

Returns

The distribution’s maximum at posteriori values.

Return type

ndarray

sample(x)[source]

Sample from the leaf distribution.

Parameters

x (ndarray) – The samples with possible NaN values.

Returns

The completed samples.

Return type

ndarray

moment(k=1)[source]

Compute the moment of a given order.

Parameters

k (int) – The order of the moment.

Returns

The moment of order k.

Return type

ndarray

params_count()[source]

Get the number of parameters of the distribution leaf.

Returns

The number of parameters.

Return type

int

params_dict()[source]

Get a dictionary representation of the distribution parameters.

Returns

A dictionary containing the distribution parameters.

Return type

dict

class deeprob.spn.structure.leaf.Uniform(scope, start=0.0, width=1.0)[source]

Bases: Leaf

Initialize an Uniform leaf node given its scope.

Parameters
  • scope (int) – The scope of the leaf.

  • start (float) – The start of the uniform distribution.

  • width (float) – The width of the uniform distribution.

LEAF_TYPE = 2
fit(data, domain, **kwargs)[source]

Fit the distribution parameters given the domain and some training data.

Parameters
  • data (ndarray) – The training data.

  • domain (tuple) – The domain of the distribution leaf.

  • kwargs – Optional parameters.

Raises

ValueError – If a parameter is out of domain.

em_init(random_state)[source]

Random initialize the leaf’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

em_step(stats, data, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • data (ndarray) – The data regarding random variables of the leaf.

  • step_size (float) – The step size of update.

likelihood(x)[source]

Compute the likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

mpe(x)[source]

Compute the maximum at posteriori values.

Parameters

x (ndarray) – The inputs.

Returns

The distribution’s maximum at posteriori values.

Return type

array

sample(x)[source]

Sample from the leaf distribution.

Parameters

x (ndarray) – The samples with possible NaN values.

Returns

The completed samples.

Return type

ndarray

moment(k=1)[source]

Compute the moment of a given order.

Parameters

k (int) – The order of the moment.

Returns

The moment of order k.

Return type

float

params_count()[source]

Get the number of parameters of the distribution leaf.

Returns

The number of parameters.

Return type

int

params_dict()[source]

Get a dictionary representation of the distribution parameters.

Returns

A dictionary containing the distribution parameters.

Return type

dict

class deeprob.spn.structure.leaf.Gaussian(scope, mean=0.0, stddev=1.0)[source]

Bases: Leaf

Initialize a Gaussian leaf node given its scope.

Parameters
  • scope (int) – The scope of the leaf.

  • mean (float) – The mean parameter.

  • stddev (float) – The standard deviation parameter.

Raises

ValueError – If a parameter is out of domain.

LEAF_TYPE = 2
fit(data, domain, **kwargs)[source]

Fit the distribution parameters given the domain and some training data.

Parameters
  • data (ndarray) – The training data.

  • domain (tuple) – The domain of the distribution leaf.

  • kwargs – Optional parameters.

Raises

ValueError – If a parameter is out of domain.

em_init(random_state)[source]

Random initialize the leaf’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

em_step(stats, data, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • data (ndarray) – The data regarding random variables of the leaf.

  • step_size (float) – The step size of update.

likelihood(x)[source]

Compute the likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the distribution leaf given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

mpe(x)[source]

Compute the maximum at posteriori values.

Parameters

x (ndarray) – The inputs.

Returns

The distribution’s maximum at posteriori values.

Return type

ndarray

sample(x)[source]

Sample from the leaf distribution.

Parameters

x (ndarray) – The samples with possible NaN values.

Returns

The completed samples.

Return type

ndarray

moment(k=1)[source]

Compute the moment of a given order.

Parameters

k (int) – The order of the moment.

Returns

The moment of order k.

Return type

float

params_count()[source]

Get the number of parameters of the distribution leaf.

Returns

The number of parameters.

Return type

int

params_dict()[source]

Get a dictionary representation of the distribution parameters.

Returns

A dictionary containing the distribution parameters.

Return type

dict

deeprob.spn.structure.node module

class deeprob.spn.structure.node.Node(scope, children=None)[source]

Bases: ABC

Initialize a SPN node given the children list and its scope.

Parameters
  • scope (List[int]) – The scope.

  • children (Optional[List[Node]]) – A list of nodes. If None, children are initialized as an empty list.

Raises
abstract likelihood(x)[source]

Compute the likelihood of the node given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

abstract log_likelihood(x)[source]

Compute the logarithmic likelihood of the node given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

class deeprob.spn.structure.node.Sum(scope=None, children=None, weights=None)[source]

Bases: Node

Initialize a SPN sum node given a list of children and their weights and a scope.

Parameters
  • scope (Optional[List[int]]) – The scope. If None, the scope is initialized based on children scopes.

  • children (Optional[List[Node]]) – A list of nodes. If None, children are initialized as an empty list.

  • weights (Optional[Union[List[float], np.ndarray]]) – The weights associated to each children node. It can be None.

Raises
  • ValueError – If both scope and children are None.

  • ValueError – If children nodes have different scopes.

  • ValueError – If the length of weights and children are different.

  • ValueError – If weights don’t sum up to 1.

em_init(random_state)[source]

Random initialize the node’s parameters for Expectation-Maximization (EM).

Parameters

random_state (RandomState) – The random state.

em_step(stats, step_size)[source]

Compute a batch Expectation-Maximization (EM) step.

Parameters
  • stats (ndarray) – The sufficient statistics of each sample.

  • step_size (float) – The step size of update.

likelihood(x)[source]

Compute the likelihood of the node given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the node given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

class deeprob.spn.structure.node.Product(scope=None, children=None)[source]

Bases: Node

Initialize a product node given a list of children and its scope.

Parameters
  • scope (Optional[List[int]]) – The scope. If None, the scope is initialized based on children scopes.

  • children (Optional[List[Node]]) – A list of nodes. If None, children are initialized as an empty list.

Raises
  • ValueError – If both scope and children are None.

  • ValueError – If children nodes don’t have disjointed scopes.

likelihood(x)[source]

Compute the likelihood of the node given some input.

Parameters

x (ndarray) – The inputs.

Returns

The resulting likelihoods.

Return type

ndarray

log_likelihood(x)[source]

Compute the logarithmic likelihood of the node given some input.

Parameters

x (append) – The inputs.

Returns

The resulting log-likelihoods.

Return type

ndarray

deeprob.spn.structure.node.assign_ids(root)[source]

Assign the ids to the nodes of a SPN.

Parameters

root (Node) – The root of the SPN.

Returns

The same SPN with each node having modified ids.

Raises

ValueError – If the SPN structure is not a DAG.

Return type

Node

deeprob.spn.structure.node.bfs(root)[source]

Compute the Breadth First Search (BFS) ordering for a SPN.

Parameters

root (Node) – The root of the SPN.

Returns

The BFS nodes iterator.

Return type

Iterator[Node]

deeprob.spn.structure.node.dfs_post_order(root)[source]

Compute Depth First Search (DFS) Post-Order ordering for a SPN.

Parameters

root (Node) – The root of the SPN.

Returns

The DFS Post-Order nodes iterator.

Return type

Iterator[Node]

deeprob.spn.structure.node.topological_order(root)[source]

Compute the Topological Ordering for a SPN, using the Kahn’s Algorithm.

Parameters

root (Node) – The root of the SPN.

Returns

A list of nodes that form a topological ordering. If the SPN graph is not acyclic, it returns None.

Return type

Optional[List[Node]]

deeprob.spn.structure.node.topological_order_layered(root)[source]

Compute the Topological Ordering Layered for a SPN, using the Kahn’s Algorithm.

Parameters

root (Node) – The root of the SPN.

Returns

A list of layers that form a topological ordering. If the SPN graph is not acyclic, it returns None.

Return type

Optional[List[List[Node]]]

Module contents