deeprob.spn.models package

Submodules

deeprob.spn.models.dgcspn module

class deeprob.spn.models.dgcspn.DgcSpn(in_features, out_classes=1, n_batch=8, sum_channels=8, depthwise=False, n_pooling=0, optimize_scale=False, in_dropout=None, sum_dropout=None, quantiles_loc=None, uniform_loc=None)[source]

Bases: ProbabilisticModel

Initialize a Deep Generalized Convolutional Sum-Product Network (DGC-SPN).

Parameters
  • in_features (Tuple[int, int, int]) – The input size as a (C, D, D) tuple.

  • out_classes (int) – The number of output classes. Specify 1 in case of plain density estimation.

  • n_batch (int) – The number of output channels of the base layer.

  • sum_channels (int) – The number of output channels of spatial sum layers.

  • depthwise (Union[bool, List[bool]]) – Whether to use depthwise convolutions as product layers at each depth level. The last flag of the list will be considered as the one for the rest of the network. If a single boolean is passed, it will be used for all the network’s product layers.

  • n_pooling (int) – The number of initial pooling spatial product layers.

  • optimize_scale (bool) – Whether to train scale and location jointly.

  • in_dropout (Optional[float]) – The dropout rate for probabilistic dropout at distributions layer outputs. It can be None.

  • sum_dropout (Optional[float]) – The dropout rate for probabilistic dropout at sum layers. It can be None.

  • quantiles_loc (Optional[ndarray]) – The mean quantiles for location initialization. It can be None.

  • uniform_loc (Optional[Tuple[float, float]]) – The uniform range for location initialization. It can be None.

Raises

ValueError – If a parameter is out of domain.

forward(x)[source]

Compute the log-likelihood given some evidence. Random variables can be marginalized using NaN values.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

mpe(x)[source]

Compute the maximum at posteriori estimation. Random variables can be marginalized using NaN values.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

sample(n_samples, y=None)[source]

Sample some values from the modeled distribution.

Parameters
  • n_samples (int) – The number of samples.

  • y (Optional[Tensor]) – The samples labels. It can be None.

Returns

The samples.

Return type

Tensor

loss(x, y=None)[source]

Compute the loss of the model.

Parameters
  • x (Tensor) – The outputs of the model.

  • y (Optional[Tensor]) – The ground-truth. It can be None.

Returns

The loss.

Return type

Tensor

apply_constraints()[source]

Apply the constraints specified by the model.

training: bool

deeprob.spn.models.ratspn module

class deeprob.spn.models.ratspn.RatSpn(in_features, base_cls, base_kwargs=None, out_classes=1, rg_depth=2, rg_repetitions=1, rg_batch=2, rg_sum=2, in_dropout=None, sum_dropout=None, random_state=None)[source]

Bases: ProbabilisticModel

Initialize a RAT-SPN.

Parameters
  • in_features (int) – The number of input features.

  • base_cls (Type[RegionGraphLayer]) – The base distribution’s class. It must be a sub-class of RegionGraphLayer.

  • base_kwargs (Optional[dict]) – Optional additiona parameters to pass to the base distribution’s class constructor.

  • out_classes (int) – The number of output classes. Specify 1 in case of plain density estimation.

  • rg_depth (int) – The depth of the region graph.

  • rg_repetitions (int) – The number of independent repetitions of the region graph.

  • rg_batch (int) – The number of base distribution batches.

  • rg_sum (int) – The number of sum nodes per region.

  • in_dropout (Optional[float]) – The dropout rate for probabilistic dropout at distributions layer outputs. It can be None.

  • sum_dropout (Optional[float]) – The dropout rate for probabilistic dropout at sum layers. It can be None.

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

Raises

ValueError – If a parameter is out of domain.

forward(x)[source]

Compute the log-likelihood given some evidence. Random variables can be marginalized using NaN values.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

mpe(x, y=None)[source]

Compute the maximum at posteriori estimation. Random variables can be marginalized using NaN values.

Parameters
  • x (Tensor) – The inputs tensor.

  • y (Optional[Tensor]) – The target classes tensor. It can be None for unlabeled maximum at posteriori estimation.

Returns

The output of the model.

Return type

Tensor

sample(n_samples, y=None)[source]

Sample some values from the modeled distribution.

Parameters
  • n_samples (int) – The number of samples.

  • y (Optional[Tensor]) – The samples labels. It can be None.

Returns

The samples.

Return type

Tensor

loss(x, y=None)[source]

Compute the loss of the model.

Parameters
  • x (Tensor) – The outputs of the model.

  • y (Optional[Tensor]) – The ground-truth. It can be None.

Returns

The loss.

Return type

Tensor

training: bool
class deeprob.spn.models.ratspn.GaussianRatSpn(in_features, out_classes=1, rg_depth=2, rg_repetitions=1, rg_batch=2, rg_sum=2, in_dropout=None, sum_dropout=None, random_state=None, uniform_loc=None, optimize_scale=False)[source]

Bases: RatSpn

Initialize a Gaussian RAT-SPN.

Parameters
  • in_features (int) – The number of input features.

  • out_classes (int) – The number of output classes. Specify 1 in case of plain density estimation.

  • rg_depth (int) – The depth of the region graph.

  • rg_repetitions (int) – The number of independent repetitions of the region graph.

  • rg_batch (int) – The number of base distributions batches.

  • rg_sum (int) – The number of sum nodes per region.

  • in_dropout (Optional[float]) – The dropout rate for probabilistic dropout at distributions layer outputs. It can be None.

  • sum_dropout (Optional[float]) – The dropout rate for probabilistic dropout at sum layers. It can be None.

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

  • uniform_loc (Optional[Tuple[float, float]]) – The optional uniform distribution parameters for location initialization.

  • optimize_scale (bool) – Whether to train scale and location jointly.

apply_constraints()[source]

Apply the constraints specified by the model.

training: bool
class deeprob.spn.models.ratspn.BernoulliRatSpn(in_features, out_classes=1, rg_depth=2, rg_repetitions=1, rg_batch=2, rg_sum=2, in_dropout=None, sum_dropout=None, random_state=None)[source]

Bases: RatSpn

Initialize a Bernoulli RAT-SPN.

Parameters
  • in_features (int) – The number of input features.

  • out_classes (int) – The number of output classes. Specify 1 in case of plain density estimation.

  • rg_depth (int) – The depth of the region graph.

  • rg_repetitions (int) – The number of independent repetitions of the region graph.

  • rg_batch (int) – The number of base distributions batches.

  • rg_sum (int) – The number of sum nodes per region.

  • in_dropout (Optional[float]) – The dropout rate for probabilistic dropout at distributions layer outputs. It can be None.

  • sum_dropout (Optional[float]) – The dropout rate for probabilistic dropout at product layer outputs. It can be None.

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

training: bool

deeprob.spn.models.sklearn module

class deeprob.spn.models.sklearn.SPNEstimator(distributions, domains=None, **kwargs)[source]

Bases: BaseEstimator, DensityMixin

Scikit-learn density estimator model for Sum Product Networks (SPNs).

Parameters
  • distributions (List[Type[Leaf]]) – A list of distribution classes (one for each feature).

  • domains (Optional[List[Union[list, tuple]]]) – A list of domains (one for each feature).

  • kwargs – Additional arguments to pass to the SPN learner.

fit(X, y=None)[source]

Fit the SPN density estimator.

Parameters
Returns

Itself.

predict_log_proba(X)[source]

Predict using the SPN density estimator, i.e. compute the log-likelihood.

Parameters

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

Returns

The log-likelihood of the inputs.

Return type

ndarray

mpe(X)[source]

Predict the un-observed variable by maximum at posterior estimation (MPE).

Parameters

X (ndarray) – The inputs having some NaN values.

Returns

The MPE assignment to un-observed variables.

Return type

ndarray

sample(n=None, X=None)[source]

Sample from the modeled distribution.

Parameters
  • n (Optional[int]) – The number of samples. It must be None if X is not None. If None, n=1 is assumed.

  • X (Optional[ndarray]) – Data used for conditional sampling. It can be None for full sampling.

Returns

The samples.

Raises

ValueError – If both parameters ‘n’ and ‘X’ are passed by.

Return type

ndarray

score(X, y=None)[source]

Return the mean log-likelihood and two standard deviations on the given test data.

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

  • y (Optional[ndarray]) – Ignored. Specified only for scikit-learn API compatibility.

Returns

A dictionary consisting of two keys “mean_ll” and “stddev_ll”, representing respectively the mean log-likelihood and two standard deviations.

Return type

dict

class deeprob.spn.models.sklearn.SPNClassifier(distributions, domains=None, **kwargs)[source]

Bases: BaseEstimator, ClassifierMixin

Scikit-learn classifier model for Sum Product Networks (SPNs).

Parameters
  • distributions (List[Type[Leaf]]) – A list of distribution classes (one for each feature).

  • domains (Optional[List[Union[list, tuple]]]) – A list of domains (one for each feature).

  • kwargs – Additional arguments to pass to the SPN learner.

fit(X, y)[source]

Fit the SPN density estimator.

Parameters
Returns

Itself.

predict(X)[source]

Predict using the SPN classifier.

Parameters

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

Returns

The predicted classes.

Return type

ndarray

predict_proba(X)[source]

Predict using the SPN classifier, using probabilities.

Parameters

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

Returns

The prediction probabilities for each class.

Return type

ndarray

predict_log_proba(X)[source]

Predict using the SPN classifier, using log-probabilities.

Parameters

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

Returns

The prediction log-probabilities for each class.

Return type

ndarray

sample(n=None, y=None)[source]

Sample from the modeled conditional distribution.

Parameters
  • n (Optional[int]) – The number of samples. It must be None if y is not None. If None, n=1 is assumed.

  • y (Optional[ndarray]) – Labels used for conditional sampling. It can be None for un-conditional sampling.

Returns

The samples.

Return type

ndarray

Module contents