Sum Modules

Sum

Weighted sum over input modules with learnable log-space weights.

class spflow.modules.sums.sum.Sum(inputs, out_channels=1, num_repetitions=1, weights=None)[source]

Bases: Module

Sum module representing mixture operations in probabilistic circuits.

Implements mixture modeling by computing weighted combinations of child distributions. Weights are normalized to sum to one, maintaining valid probability distributions. Supports both single input (mixture over channels) and multiple inputs (mixture over concatenated inputs).

inputs

Input module(s) to the sum node.

Type:

Module

sum_dim

Dimension over which to sum the inputs.

Type:

int

weights

Normalized weights for mixture components.

Type:

Tensor

logits

Unnormalized log-weights for gradient optimization.

Type:

Parameter

__init__(inputs, out_channels=1, num_repetitions=1, weights=None)[source]

Create a Sum module for mixture modeling.

Weights are automatically normalized to sum to one using softmax. Multiple inputs are concatenated along dimension 2 internally.

Parameters:
  • inputs (Module | list[Module]) – Single module or list of modules to mix.

  • out_channels (int, optional) – Number of output mixture components. Defaults to 1.

  • num_repetitions (int | None, optional) – Number of repetitions for structured representations. Inferred from weights if not provided.

  • weights (Tensor | list[float] | None, optional) – Initial mixture weights. Must have compatible shape with inputs and out_channels.

Raises:
log_likelihood(data, cache=None)[source]

Compute log likelihood P(data | module).

Computes log likelihood using logsumexp for numerical stability. Results are cached for parameter learning algorithms.

Parameters:
  • data (Tensor) – Input data of shape (batch_size, num_features). NaN values indicate evidence for conditional computation.

  • cache (Cache | None) – Cache for intermediate computations. Defaults to None.

Returns:

Log-likelihood of shape (batch_size, num_features, out_channels)

or (batch_size, num_features, out_channels, num_repetitions).

Return type:

Tensor

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

Marginalize out specified random variables.

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

  • prune (bool) – Whether to prune the module.

  • cache (Cache | None) – Optional cache dictionary.

Return type:

Sum | None

Returns:

Marginalized Sum module or None.

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]

property log_weights: Tensor

Returns the log weights of all nodes as a tensor.

Returns:

Log weights normalized to sum to one.

Return type:

Tensor

ElementwiseSum

Element-wise summation over multiple inputs with the same scope.

class spflow.modules.sums.elementwise_sum.ElementwiseSum(inputs, out_channels=None, weights=None, num_repetitions=None)[source]

Bases: Module

Elementwise sum operation for mixture modeling.

Computes weighted combinations of input tensors element-wise. Weights are automatically normalized to sum to one. Uses log-domain computations.

logits

Unnormalized log-weights for gradient optimization.

Type:

Parameter

unraveled_channel_indices

Mapping for flattened channel indices.

Type:

Tensor

__init__(inputs, out_channels=None, weights=None, num_repetitions=None)[source]

Initialize elementwise sum module.

Parameters:
  • inputs (list[Module]) – Input modules (same features, compatible channels).

  • out_channels (int | None) – Number of output nodes per sum. Note that this results in a total of out_channels * in_channels (input modules) output channels since we sum over the list of modules.

  • weights (Tensor | None) – Initial weights (if None, randomly initialized).

  • num_repetitions (int | None) – Number of repetitions.

log_likelihood(data, cache=None)[source]

Compute log likelihood via weighted log-sum-exp.

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

  • cache (Cache | None) – Cache for memoization.

Returns:

Computed log likelihood values.

Return type:

Tensor

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

Marginalize out specified random variables.

Parameters:
  • marg_rvs (list[int]) – Random variables to marginalize out.

  • prune (bool) – Whether to prune the resulting module.

  • cache (Cache | None) – Cache for memoization.

Returns:

Marginalized module or None if fully marginalized.

Return type:

Optional[ElementwiseSum]

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]

property log_weights: Tensor
property weights: Tensor

RepetitionMixingLayer

A specialized sum layer used to sum over repetitions.

class spflow.modules.sums.repetition_mixing_layer.RepetitionMixingLayer(inputs, out_channels=1, num_repetitions=1, weights=None)[source]

Bases: Sum

Mixing layer for RAT-SPN region nodes.

Specialized sum node for RAT-SPNs. Creates mixtures over input channels. Extends Sum with RAT-SPN specific optimizations.

__init__(inputs, out_channels=1, num_repetitions=1, weights=None)[source]

Initialize mixing layer for RAT-SPN.

Parameters:
  • inputs (Module) – Input module to mix over channels.

  • out_channels (int) – Number of output mixture components.

  • num_repetitions (int) – Number of parallel repetitions.

  • weights (Tensor | None) – Initial mixing weights (if None, randomly initialized).

log_likelihood(data, cache=None)[source]

Compute log likelihood via weighted log-sum-exp.

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

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

Returns:

Computed log likelihood values.

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]

SignedSum

Linear combination node that allows negative, non-normalized weights.

class spflow.modules.sums.signed_sum.SignedSum(inputs, out_channels=1, num_repetitions=1, weights=None)[source]

Bases: Module

Linear-combination node that allows negative, non-normalized weights.

This node is not a probabilistic mixture node. It represents a real-valued linear combination of input channels:

y = Σ_j w_j * x_j

where weights may be negative and do not need to sum to one.

Notes

  • SignedSum does not implement log_likelihood() because its output may be negative (log is undefined). Use SOCS or signed evaluation utilities for inference.

  • sample() is only supported when all weights are non-negative and no evidence is present, in which case it behaves like an unnormalized mixture over inputs.

log_likelihood(data, cache=None)[source]

Compute log likelihood P(data | module).

Computes log probability of input data under this module’s distribution. Uses log-space for numerical stability. Results should be cached for efficiency.

Parameters:
  • data (Tensor) – Input data of shape (batch_size, num_features). NaN values indicate missing values to marginalize over.

  • cache (Cache | None, optional) – Cache for intermediate computations. Defaults to None.

Returns:

Log-likelihood of shape (batch_size, out_features, out_channels).

Return type:

Tensor

Raises:

ValueError – If input data shape is incompatible with module scope.

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

Structurally marginalize out specified random variables from the module.

Computes a new module representing the marginal distribution by integrating out the specified variables from the structure. For data-level marginalization, use NaNs in log_likelihood inputs.

Parameters:
  • marg_rvs (list[int]) – Random variable indices to marginalize out.

  • prune (bool, optional) – Whether to prune unnecessary modules during marginalization. Defaults to True.

  • cache (Cache | None, optional) – Cache for intermediate computations. Defaults to None.

Returns:

Marginalized module, or None if all variables are marginalized out.

Return type:

Module | None

Raises:

ValueError – If marginalization variables are not in the module’s scope.

signed_logabs_and_sign(data, cache=None)[source]

Evaluate this node in (log|·|, sign) form.

Returns:

Tensor of shape (B, F, OC, R) sign: Tensor of shape (B, F, OC, R) in {-1,0,+1}

Return type:

logabs

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]