Skip to content

Support marginalising through a MinibatchRandomVariable #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pymc_extras/model/marginal/graph_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pymc import SymbolicRandomVariable
from pymc.model.fgraph import ModelVar
from pymc.variational.minibatch_rv import MinibatchRandomVariable
from pytensor.graph import Variable, ancestors
from pytensor.graph.basic import io_toposort
from pytensor.tensor import TensorType, TensorVariable
Expand Down Expand Up @@ -313,6 +314,9 @@ def _subgraph_batch_dim_connection(var_dims: VAR_DIMS, input_vars, output_vars)

var_dims[node.outputs[0]] = output_dims

elif isinstance(node.op, MinibatchRandomVariable):
var_dims[node.outputs[0]] = inputs_dims[0]

else:
raise NotImplementedError(f"Marginalization through operation {node} not supported.")

Expand Down
8 changes: 8 additions & 0 deletions tests/model/marginal/test_graph_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from pymc.distributions import CustomDist
from pymc.variational.minibatch_rv import create_minibatch_rv
from pytensor.tensor.type_other import NoneTypeT

from pymc_extras.model.marginal.graph_analysis import (
Expand Down Expand Up @@ -160,6 +161,13 @@ def test_random_variable(self):
with pytest.raises(ValueError, match="Use of known dimensions"):
subgraph_batch_dim_connection(inp, [invalid_out])

def test_minibatched_random_variable(self):
inp = pt.tensor(shape=(4, 3, 2))
out1 = pt.random.normal(loc=inp)
out2 = create_minibatch_rv(out1, total_size=(10, 10, 10))
[dims1] = subgraph_batch_dim_connection(inp, [out2])
assert dims1 == (0, 1, 2)

def test_symbolic_random_variable(self):
inp = pt.tensor(shape=(4, 3, 2))

Expand Down