deeprob.spn.layers package

Submodules

deeprob.spn.layers.dgcspn module

class deeprob.spn.layers.dgcspn.SpatialGaussianLayer(in_features, out_channels, optimize_scale=False, dropout=None, quantiles_loc=None, uniform_loc=None)[source]

Bases: Module

Initialize a Spatial Gaussian input layer.

Parameters
  • in_features (Tuple[int, int, int]) – The number of input features.

  • out_channels (int) – The number of output channels.

  • optimize_scale (bool) – Whether to optimize scale.

  • dropout (Optional[float]) – The leaf nodes dropout rate. 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 both quantiles_loc and uniform_loc are specified.

property in_channels: int
property in_height: int
property in_width: int
property out_channels: int
property out_height: int
property out_width: int
forward(x)[source]

Evaluate the layer given some inputs.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

training: bool
class deeprob.spn.layers.dgcspn.SpatialProductLayer(in_features, kernel_size, padding, stride, dilation, depthwise=True)[source]

Bases: Module

Initialize a Spatial Product layer.

Parameters
  • in_features (Tuple[int, int, int]) – The number of input features.

  • kernel_size (Union[int, Tuple[int, int]]) – The size of the kernels.

  • stride (Union[int, Tuple[int, int]]) – The strides to use.

  • padding (str) – The padding mode to use. It can be ‘valid’, ‘full’ or ‘final’. Valid padding means no padding used. Full padding means padding is used based on effective kernel size. Final padding means one-side padding (

  • dilation (Union[int, Tuple[int, int]]) – The space between the kernel points.

  • depthwise (bool) – Whether to use depthwise convolutions. If False, random sparse kernels are used.

Raises

ValueError – If a parameter is out of domain.

property in_channels: int
property in_height: int
property in_width: int
property out_channels: int
property out_height: int
property out_width: int
forward(x)[source]

Evaluate the layer given some inputs.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

training: bool
class deeprob.spn.layers.dgcspn.SpatialSumLayer(in_features, out_channels, dropout=None)[source]

Bases: Module

Initialize a Spatial Sum layer.

Parameters
  • in_features (Tuple[int, int, int]) – The number of input features.

  • out_channels (int) – The number of output channels.

  • dropout (Optional[float]) – The input nodes dropout rate. It can be None.

property in_channels: int
property in_height: int
property in_width: int
property out_channels: int
property out_height: int
property out_width: int
forward(x)[source]

Evaluate the layer given some inputs.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

training: bool
class deeprob.spn.layers.dgcspn.SpatialRootLayer(in_features, out_channels)[source]

Bases: Module

Initialize a Spatial Root layer.

Parameters
  • in_features (Tuple[int, int, int]) – The number of input features.

  • out_channels (int) – The number of output channels.

training: bool
property in_channels: int
property in_height: int
property in_width: int
forward(x)[source]

Evaluate the layer given some inputs.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

deeprob.spn.layers.ratspn module

class deeprob.spn.layers.ratspn.RegionGraphLayer(in_features, out_channels, regions, rg_depth, dropout=None, **kwargs)[source]

Bases: ABC, Module

Initialize a Region Graph-based base distribution.

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

  • out_channels (int) – The number of channels for each base distribution layer.

  • regions (List[tuple]) – The regions of the distributions.

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

  • dropout (Optional[float]) – The leaf nodes dropout rate. It can be None.

unpad_samples(x, idx_group)[source]

Reorder and unpad some samples.

Parameters
  • x (Tensor) – The samples.

  • idx_group (Tensor) – The group indices.

Returns

The reordered samples with padding dummy variables removed.

Return type

Tensor

forward(x)[source]

Execute the layer on some inputs.

Parameters

x (Tensor) – The inputs.

Returns

The log-likelihoods of each distribution leaf.

Return type

Tensor

abstract distribution_mode()[source]

Get the mode of the distribution.

Returns

The mode of the distribution.

Return type

Tensor

mpe(x, idx_group, idx_offset)[source]

Evaluate the layer given some inputs for maximum at posteriori estimation.

Parameters
  • x (Tensor) – The inputs. Random variables can be marginalized using NaN values.

  • idx_group (Tensor) – The group indices.

  • idx_offset (Tensor) – The offset indices.

Returns

The samples having maximum at posteriori estimates on marginalized random variables.

Return type

Tensor

sample(idx_group, idx_offset)[source]

Sample from a base distribution.

Parameters
  • idx_group (Tensor) – The group indices.

  • idx_offset (Tensor) – The offset indices.

Returns

The computed samples.

Return type

Tensor

training: bool
class deeprob.spn.layers.ratspn.GaussianLayer(in_features, out_channels, regions, rg_depth, dropout=None, uniform_loc=None, optimize_scale=False)[source]

Bases: RegionGraphLayer

Initialize a Gaussian distributions input layer.

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

  • out_channels (int) – The number of channels for each base distribution layer.

  • regions (List[tuple]) – The regions of the distributions.

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

  • dropout (Optional[float]) – The leaf nodes dropout rate. It can be None.

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

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

distribution_mode()[source]

Get the mode of the distribution.

Returns

The mode of the distribution.

Return type

Tensor

training: bool
class deeprob.spn.layers.ratspn.BernoulliLayer(in_features, out_channels, regions, rg_depth, dropout=None)[source]

Bases: RegionGraphLayer

Initialize a Bernoulli distributions input layer.

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

  • out_channels (int) – The number of channels for each base distribution layer.

  • regions (List[tuple]) – The regions of the distributions.

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

  • dropout (Optional[float]) – The leaf nodes dropout rate. It can be None.

distribution_mode()[source]

Get the mode of the distribution.

Returns

The mode of the distribution.

Return type

Tensor

training: bool
class deeprob.spn.layers.ratspn.ProductLayer(in_regions, in_nodes)[source]

Bases: Module

Initialize the Product layer.

Parameters
  • in_regions (int) – The number of input regions.

  • in_nodes (int) – The number of input nodes per region.

forward(x)[source]

Evaluate the layer given some inputs.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

mpe(x, idx_group, idx_offset)[source]

Evaluate the layer given some inputs for maximum at posteriori estimation.

Parameters
  • x (Tensor) – The inputs (not used here).

  • idx_group (Tensor) – The group indices.

  • idx_offset (Tensor) – The offset indices.

Returns

The group and offset indices.

Return type

[<class ‘torch.Tensor’>, <class ‘torch.Tensor’>]

sample(idx_group, idx_offset)[source]

Sample from a product layer.

Parameters
  • idx_group (Tensor) – The group indices.

  • idx_offset (Tensor) – The offset indices.

Returns

The group and offset indices.

Return type

Tuple[Tensor, Tensor]

training: bool
class deeprob.spn.layers.ratspn.SumLayer(in_partitions, in_nodes, out_nodes, dropout=None)[source]

Bases: Module

Initialize the sum layer.

Parameters
  • in_partitions (int) – The number of input partitions.

  • in_nodes (int) – The number of input nodes per partition.

  • out_nodes (int) – The number of output nodes per region.

  • dropout (Optional[float]) – The input nodes dropout rate. It can be None.

forward(x)[source]

Evaluate the layer given some inputs.

Parameters

x (Tensor) – The inputs.

Returns

The outputs.

Return type

Tensor

mpe(x, idx_group, idx_offset)[source]

Evaluate the layer given some inputs for maximum at posteriori estimation.

Parameters
  • x (Tensor) – The inputs.

  • idx_group (Tensor) – The group indices.

  • idx_offset (Tensor) – The offset indices.

Returns

The group and offset indices.

Return type

[<class ‘torch.Tensor’>, <class ‘torch.Tensor’>]

sample(idx_group, idx_offset)[source]

Sample from a sum layer.

Parameters
  • idx_group (Tensor) – The group indices.

  • idx_offset (Tensor) – The offset indices.

Returns

The group and offset indices.

Return type

Tuple[Tensor, Tensor]

training: bool
class deeprob.spn.layers.ratspn.RootLayer(in_partitions, in_nodes, out_classes)[source]

Bases: Module

Initialize the root layer.

Parameters
  • in_partitions (int) – The number of input partitions.

  • in_nodes (int) – The number of input nodes per partition.

  • out_classes (int) – The number of output nodes.

forward(x)[source]

Evaluate the layer given some inputs.

Parameters

x – The inputs.

Returns

The outputs.

mpe(x, y)[source]

Evaluate the layer given some inputs for maximum at posteriori estimation.

Parameters
  • x (Tensor) – The inputs.

  • y (Tensor) – The target classes.

Returns

The group and offset indices.

Return type

Tuple[Tensor, Tensor]

training: bool
sample(y)[source]

Sample from the root layer.

Parameters

y (Tensor) – The target classes.

Returns

The group and offset indices.

Return type

Tuple[Tensor, Tensor]

Module contents