Product Modules

Product

Multiplies over the feature dimension of input modules.

class spflow.modules.products.Product(inputs)[source]

Bases: Module

Product node implementing factorization via conditional independence.

Computes joint distribution as product of child distributions. Multiple inputs are automatically concatenated along the feature dimension.

inputs

Input module(s), concatenated if multiple.

Type:

Module

__init__(inputs)[source]

Initialize product node.

Parameters:

inputs (Module | list[Module]) – Single module or list of modules (concatenated along features).

expectation_maximization(data, bias_correction=True, cache=None)[source]

EM step (delegates to input, no learnable parameters).

Parameters:
  • data (Tensor) – Input data tensor for EM step.

  • bias_correction (bool) – Whether to apply bias correction. Defaults to True.

  • cache (Cache | None) – Optional cache for storing intermediate results.

Return type:

None

log_likelihood(data, cache=None)[source]

Compute log likelihood by summing child log-likelihoods across features.

Parameters:
  • data (Tensor) – Input data tensor.

  • cache (Cache | None) – Optional cache for storing intermediate results.

Returns:

Log likelihood values.

Return type:

Tensor

marginalize(marg_rvs, prune=True, cache=None)[source]

Marginalize out specified random variables.

Parameters:
  • marg_rvs (list[int]) – List of random variable indices to marginalize.

  • prune (bool) – Whether to prune unnecessary nodes.

  • cache (Cache | None) – Optional cache for storing intermediate results.

Returns:

Marginalized module or None if fully marginalized.

Return type:

Product | Module | None

maximum_likelihood_estimation(data, weights=None, cache=None)[source]

MLE step (delegates to input, no learnable parameters).

Parameters:
  • data (Tensor) – Input data tensor for MLE step.

  • weights (Tensor | None) – Optional weights for weighted MLE.

  • cache (Cache | None) – Optional cache for storing intermediate results.

Return type:

None

sample(num_samples=None, data=None, is_mpe=False, cache=None, sampling_ctx=None)[source]

Generate samples by delegating to input module.

Parameters:
  • num_samples (int | None) – Number of samples to generate.

  • data (Tensor | None) – Optional data tensor to fill with samples.

  • is_mpe (bool) – Whether to perform most probable explanation.

  • cache (Cache | None) – Optional cache for storing intermediate results.

  • sampling_ctx (SamplingContext | None) – Optional sampling context.

Returns:

Generated samples.

Return type:

Tensor

property feature_to_scope: ndarray

Mapping from output features to their respective scopes.

Returns:

2D-array of scopes. Each row corresponds to an output feature,

each column to a repetition.

Return type:

np.ndarray[Scope]

ElementwiseProduct

Element-wise product over inputs with disjoint scopes.

class spflow.modules.products.ElementwiseProduct(inputs, num_splits=2, split_mode=None)[source]

Bases: BaseProduct

Elementwise product with automatic broadcasting.

Multiplies inputs element-wise with broadcasting support. All input scopes must be pairwise disjoint. Commonly used in RAT-SPN architectures.

__init__(inputs, num_splits=2, split_mode=None)[source]

Initialize elementwise product.

Parameters:
  • inputs (Module | tuple[Module, Module] | list[Module]) –

    List of Modules. The scopes for all child modules need to be pair-wise disjoint.

    1. If inputs is a list of Modules, they have to be of disjoint scopes and have equal number of features or a single feature wich will the be broadcast and an equal number of channels or a single channel which will be broadcast.

    Example shapes:

    inputs = ((3, 4), (3, 4)) output = (3, 4)

    inputs = ((3, 4), (3, 1)) output = (3, 4) # broadcasted

    inputs = ((3, 4), (1, 4)) output = (3, 4) # broadcasted

    inputs = ((3, 1), (1, 4)) output = (3, 4) # broadcasted

  • num_splits (int | None) – Number of splits when wrapping single input in Split.

  • split_mode (SplitMode | None) – Optional split configuration for single input mode. Use SplitMode.consecutive() or SplitMode.interleaved(). Defaults to SplitMode.consecutive(num_splits=num_splits) if not specified.

Raises:

ValueError – Invalid arguments.

check_shapes()[source]

Check if input shapes are compatible for broadcasting.

log_likelihood(data, cache=None)[source]

Compute log likelihood by element-wise summing inputs.

Parameters:
  • data (Tensor) – Input data tensor.

  • cache (Cache | None) – Optional cache for storing intermediate computations.

Returns:

Computed log likelihood values.

Return type:

Tensor

map_out_channels_to_in_channels(output_ids)[source]

Map output channel indices to input channel indices.

Parameters:

output_ids (Tensor) – Tensor of output channel indices to map.

Returns:

Mapped input channel indices.

Return type:

Tensor

map_out_mask_to_in_mask(mask)[source]

Map output mask to input mask.

Parameters:

mask (Tensor) – Output mask tensor to map.

Returns:

Mapped input mask tensor.

Return type:

Tensor

property feature_to_scope: ndarray

Mapping from output features to their respective scopes.

Returns:

2D-array of scopes. Each row corresponds to an output feature,

each column to a repetition.

Return type:

np.ndarray[Scope]

OuterProduct

Outer (Cartesian) product over input channels, creating all combinations.

class spflow.modules.products.OuterProduct(inputs, num_splits=2, split_mode=None)[source]

Bases: BaseProduct

Outer product creating all pairwise channel combinations.

Computes Cartesian product of input channels. Output channels equal product of input channels. All input scopes must be pairwise disjoint.

unraveled_channel_indices

Mapping from output to input channel pairs.

Type:

Tensor

__init__(inputs, num_splits=2, split_mode=None)[source]

Initialize outer product.

Parameters:
  • inputs (list[Module]) – Modules with pairwise disjoint scopes.

  • num_splits (int | None) – Number of splits for input operations.

  • split_mode (SplitMode | None) – Optional split configuration for single input mode. Use SplitMode.consecutive() or SplitMode.interleaved(). Defaults to SplitMode.consecutive(num_splits=num_splits) if not specified.

check_shapes()[source]

Check if input shapes are compatible for outer product.

Returns:

True if shapes are compatible, False if no shapes to check.

Return type:

bool

Raises:

ValueError – If input shapes are not broadcastable.

log_likelihood(data, cache=None)[source]

Compute log likelihood via outer sum of pairwise combinations.

Parameters:
  • data (Tensor) – Input data tensor for computing log likelihood.

  • cache (Cache | None) – Optional cache for storing intermediate computations.

Returns:

Log likelihood values with shape [batch_size, out_features, out_channels, num_repetitions].

Return type:

Tensor

Raises:

ValueError – If output tensor has invalid number of dimensions.

map_out_channels_to_in_channels(output_ids)[source]

Map output channel indices to input channel indices.

Parameters:

output_ids (Tensor) – Tensor of output channel indices to map.

Returns:

Mapped input channel indices corresponding to the output channels.

Return type:

Tensor

Raises:

NotImplementedError – If split type is not supported.

map_out_mask_to_in_mask(mask)[source]

Map output mask to input masks.

Parameters:

mask (Tensor) – Output mask tensor to map to input masks.

Returns:

Mapped input masks corresponding to the output mask.

Return type:

Tensor

Raises:

NotImplementedError – If split type is not supported.

property feature_to_scope: list[Scope]

Mapping from output features to their respective scopes.

Returns:

2D-array of scopes. Each row corresponds to an output feature,

each column to a repetition.

Return type:

np.ndarray[Scope]