Skip to content

Add warning if a Minibatched variable is used without total_size #7742

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 4 commits into from
Apr 1, 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
11 changes: 10 additions & 1 deletion pymc/model/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from pytensor.tensor.variable import TensorConstant, TensorVariable

from pymc.blocking import DictToArrayBijection, RaveledVars
from pymc.data import is_valid_observed
from pymc.data import MinibatchOp, is_valid_observed
from pymc.exceptions import (
BlockModelAccessError,
ImputationWarning,
Expand Down Expand Up @@ -1241,6 +1241,15 @@ def register_rv(
self.add_named_variable(rv_var, dims)
self.set_initval(rv_var, initval)
else:
if (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should have checked if minibatch is anywhere in the ancestors of value not just immediately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do that? This check is only for observed random variables. I thought for those we don't allow the value to depend on another node

isinstance(observed, TensorVariable)
and observed.owner is not None
and isinstance(observed.owner.op, MinibatchOp)
and total_size is None
):
warnings.warn(
f"total_size not provided for observed variable `{name}` that uses pm.Minibatch"
)
if not is_valid_observed(observed):
raise TypeError(
"Variables that depend on other nodes cannot be used for observed data."
Expand Down
7 changes: 7 additions & 0 deletions tests/variational/test_minibatch_rv.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def test_random(self):
assert mx is not x
np.testing.assert_array_equal(draw(mx, random_seed=1), draw(x, random_seed=1))

def test_warning_on_missing_total_size(self):
total_size = 1000
with pytest.warns(match="total_size not provided"):
with pm.Model() as m:
MB = pm.Minibatch(np.arange(total_size, dtype="float64"), batch_size=100)
pm.Normal("n", observed=MB)

@pytest.mark.filterwarnings("error")
def test_minibatch_parameter_and_value(self):
rng = np.random.default_rng(161)
Expand Down
Loading