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:
LeafInitialize 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
- Returns
The conditional probability tables (CPTs) in a tensorized form.
- Return type
- em_init(random_state)[source]
Random initialize the leaf’s parameters for Expectation-Maximization (EM).
- Parameters
random_state (RandomState) – The random state.
- 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
- Raises
ValueError – If the random state is not valid.
ValueError – If a parameter is out of domain.
- 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
- log_likelihood(x)[source]
Compute the logarithmic likelihood of the distribution leaf given some input.
- params_count()[source]
Get the number of parameters of the distribution leaf.
- Returns
The number of parameters.
- Return type
- params_dict()[source]
Get a dictionary representation of the distribution parameters.
- Returns
A dictionary containing the distribution parameters.
- Return type
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.
- deeprob.spn.structure.io.load_digraph_json(f)[source]
Load a NetworkX directed graph by using the JSON format.
- deeprob.spn.structure.io.load_spn_json(f, leaves=None)[source]
Load SPN from file by using the JSON format.
- Parameters
- 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
- deeprob.spn.structure.io.save_binary_clt_json(clt, f)[source]
Save Binary Chow-Liu Tree (CLT) to file by using the JSON format.
- deeprob.spn.structure.io.load_binary_clt_json(f)[source]
Load Binary Chow-Liu Tree (CLT) from file by using the JSON format.
- 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.
- 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
- deeprob.spn.structure.io.plot_spn(root, f)[source]
Plot a SPN into file.
- Parameters
- Raises
ValueError – If an unknown node type is found.
ValueError – If the SPN structure is not a DAG.
deeprob.spn.structure.leaf module
- class deeprob.spn.structure.leaf.LeafType(value)[source]
Bases:
EnumThe 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:
NodeInitialize a leaf node given its scope.
- 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.
- abstract fit(data, domain, **kwargs)[source]
Fit the distribution parameters given the domain and some training data.
- Parameters
- Raises
ValueError – If a parameter is out of domain.
- abstract log_likelihood(x)[source]
Compute the logarithmic likelihood of the distribution leaf given some input.
- class deeprob.spn.structure.leaf.Bernoulli(scope, p=0.5)[source]
Bases:
LeafInitialize a Bernoulli leaf node given its scope.
- Parameters
- 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
- 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.
- log_likelihood(x)[source]
Compute the logarithmic likelihood of the distribution leaf given some input.
- class deeprob.spn.structure.leaf.Categorical(scope, categories=None, probabilities=None)[source]
Bases:
LeafInitialize 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
- 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.
- log_likelihood(x)[source]
Compute the logarithmic likelihood of the distribution leaf given some input.
- class deeprob.spn.structure.leaf.Isotonic(scope, densities=None, breaks=None)[source]
Bases:
LeafInitialize 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
- 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.
- log_likelihood(x)[source]
Compute the logarithmic likelihood of the distribution leaf given some input.
- class deeprob.spn.structure.leaf.Uniform(scope, start=0.0, width=1.0)[source]
Bases:
LeafInitialize an Uniform leaf node given its scope.
- Parameters
- LEAF_TYPE = 2
- fit(data, domain, **kwargs)[source]
Fit the distribution parameters given the domain and some training data.
- 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.
- log_likelihood(x)[source]
Compute the logarithmic likelihood of the distribution leaf given some input.
- 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
- class deeprob.spn.structure.leaf.Gaussian(scope, mean=0.0, stddev=1.0)[source]
Bases:
LeafInitialize a Gaussian leaf node given its scope.
- Parameters
- 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
- 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.
- log_likelihood(x)[source]
Compute the logarithmic likelihood of the distribution leaf given some input.
deeprob.spn.structure.node module
- class deeprob.spn.structure.node.Node(scope, children=None)[source]
Bases:
ABCInitialize a SPN node given the children list and its scope.
- Parameters
- Raises
ValueError – If the scope is empty.
ValueError – If the scope contains duplicates.
- class deeprob.spn.structure.node.Sum(scope=None, children=None, weights=None)[source]
Bases:
NodeInitialize 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.
- class deeprob.spn.structure.node.Product(scope=None, children=None)[source]
Bases:
NodeInitialize a product node given a list of children and its scope.
- Parameters
- Raises
ValueError – If both scope and children are None.
ValueError – If children nodes don’t have disjointed scopes.
- 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
- deeprob.spn.structure.node.bfs(root)[source]
Compute the Breadth First Search (BFS) ordering for a SPN.
- deeprob.spn.structure.node.dfs_post_order(root)[source]
Compute Depth First Search (DFS) Post-Order ordering for a SPN.
- deeprob.spn.structure.node.topological_order(root)[source]
Compute the Topological Ordering for a SPN, using the Kahn’s Algorithm.