Convolutional Modules¶
Convolutional layers for modeling spatial structure in image data with probabilistic circuits.
ConvPc¶
High-level convolutional probabilistic circuit architecture that stacks alternating ProdConv and SumConv layers on top of a leaf distribution. Similar to RAT-SPN but designed specifically for image data with spatial structure.
- class spflow.modules.conv.ConvPc(leaf, input_height, input_width, channels, depth, kernel_size=2, num_repetitions=1, use_sum_conv=False)[source]¶
Bases:
ModuleConvolutional Probabilistic Circuit.
Builds a multi-layer circuit with alternating ProdConv and SumConv layers on top of a leaf distribution. The architecture progressively reduces spatial dimensions while learning mixture weights at each level.
The layer ordering is: Leaf -> ProdConv -> SumConv -> ProdConv -> SumConv -> … -> Root Sum
Layers are constructed top-down (from root to leaves), then reversed for proper bottom-up evaluation order.
- leaf¶
Leaf distribution module.
- Type:
Module
- root¶
Final sum layer producing scalar output per sample.
- Type:
Sum
- __init__(leaf, input_height, input_width, channels, depth, kernel_size=2, num_repetitions=1, use_sum_conv=False)[source]¶
Create a ConvPc for image modeling.
- Parameters:
leaf (
Module) – Leaf distribution module (e.g., Normal over pixels).input_height (
int) – Height of input image.input_width (
int) – Width of input image.channels (
int) – Number of channels per sum layer.depth (
int) – Number of (ProdConv, SumConv) layer pairs.kernel_size (
int) – Kernel size for pooling (default 2x2).num_repetitions (
int) – Number of independent repetitions.use_sum_conv (
bool) – If True, use SumConv layers with kernel-based spatial weights. If False (default), use regular Sum layers that treat features independently without spatial awareness.
- Raises:
ValueError – If depth < 1.
- expectation_maximization(data, bias_correction=True, cache=None)[source]¶
Perform EM update throughout the circuit.
- marginalize(marg_rvs, prune=True, cache=None)[source]¶
Marginalize out specified random variables.
- Parameters:
- Returns:
Marginalized module or None if fully marginalized.
- Return type:
- sample(num_samples=None, data=None, is_mpe=False, cache=None, sampling_ctx=None)[source]¶
Generate samples by sampling top-down through layers.
Delegates sampling to the root module (RepetitionMixingLayer when num_repetitions > 1, or Sum when num_repetitions == 1), which then recursively propagates sampling to the leaf.
SumConv¶
Convolutional sum layer that applies learned weighted sums over input channels within spatial patches. Enables mixture modeling with spatial structure.
- class spflow.modules.conv.SumConv(inputs, out_channels, kernel_size, num_repetitions=1)[source]¶
Bases:
ModuleConvolutional sum layer for probabilistic circuits.
Applies weighted sum over input channels within spatial patches. Weights are learned and normalized to sum to one per patch position, maintaining valid probability distributions. Useful for modeling spatial structure in image data.
The layer expects input with spatial structure and applies shared weights across all spatial patches of the same position within the kernel.
- inputs¶
Input module providing log-likelihoods.
- Type:
Module
- logits¶
Unnormalized log-weights for gradient optimization.
- Type:
Parameter
- __init__(inputs, out_channels, kernel_size, num_repetitions=1)[source]¶
Create a SumConv module for spatial mixture modeling.
- Parameters:
- Raises:
ValueError – If out_channels < 1 or kernel_size < 1.
- expectation_maximization(data, bias_correction=True, cache=None)[source]¶
Perform expectation-maximization step to update weights.
Follows the standard EM update pattern for sum nodes: 1. Get cached log-likelihoods for input and this module 2. Compute expectations using: log_weights + log_grads + input_lls - module_lls 3. Normalize to get new log_weights
- Parameters:
- Raises:
MissingCacheError – If required log-likelihoods are not found in cache.
- Return type:
- log_likelihood(data, cache=None)[source]¶
Compute log likelihood using convolutional weighted sum.
Applies weighted sum over input channels within spatial patches. Each kernel position gets its own set of mixture weights. Uses logsumexp for numerical stability.
- marginalize(marg_rvs, prune=True, cache=None)[source]¶
Marginalize out specified random variables.
- Parameters:
- Returns:
Marginalized module or None if fully marginalized.
- Return type:
- sample(num_samples=None, data=None, is_mpe=False, cache=None, sampling_ctx=None)[source]¶
Generate samples from sum conv module.
Each spatial position samples from its per-position kernel weights.
ProdConv¶
Convolutional product layer that computes products over spatial patches, reducing spatial dimensions by the kernel size factor. Aggregates scopes within patches while maintaining proper probabilistic semantics.
- class spflow.modules.conv.ProdConv(inputs, kernel_size_h, kernel_size_w, padding_h=0, padding_w=0)[source]¶
Bases:
ModuleConvolutional product layer for probabilistic circuits.
Computes products over spatial patches, reducing spatial dimensions by the kernel size factor. This is equivalent to summing log-likelihoods within patches. No learnable parameters.
Scopes are aggregated per patch: a 2×2 patch containing Scope(0), Scope(1), Scope(2), Scope(3) produces Scope([0,1,2,3]).
- inputs¶
Input module providing log-likelihoods.
- Type:
Module
- __init__(inputs, kernel_size_h, kernel_size_w, padding_h=0, padding_w=0)[source]¶
Create a ProdConv module for spatial product operations.
- Parameters:
inputs (
Module) – Input module providing log-likelihoods with spatial structure.kernel_size_h (
int) – Height of the pooling kernel.kernel_size_w (
int) – Width of the pooling kernel.padding_h (
int) – Padding in height dimension (added on both sides).padding_w (
int) – Padding in width dimension (added on both sides).
- Raises:
ValueError – If kernel sizes are < 1.
- expectation_maximization(data, bias_correction=True, cache=None)[source]¶
EM step (delegates to input, no learnable parameters).
- log_likelihood(data, cache=None)[source]¶
Compute log likelihood by summing within patches.
Uses depthwise convolution with ones kernel to efficiently sum log-probabilities within patches.
- marginalize(marg_rvs, prune=True, cache=None)[source]¶
Marginalize out specified random variables.
- Parameters:
- Returns:
Marginalized module or None if fully marginalized.
- Return type:
- sample(num_samples=None, data=None, is_mpe=False, cache=None, sampling_ctx=None)[source]¶
Generate samples by delegating to input.
ProdConv has no learnable parameters, so sampling simply expands the sampling context to match input features and delegates.