Skip to content

Fix change_rv_size bug #4718

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
May 25, 2021
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
2 changes: 1 addition & 1 deletion pymc3/aesaraf.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def change_rv_size(
if expand:
if rv_node.op.ndim_supp == 0 and at.get_vector_length(size) == 0:
size = rv_node.op._infer_shape(size, dist_params)
new_size = tuple(np.atleast_1d(new_size)) + tuple(size)
new_size = tuple(at.atleast_1d(new_size)) + tuple(size)

# Make sure the new size is a tensor. This helps to not unnecessarily pick
# up a `Cast` in some cases
Expand Down
20 changes: 16 additions & 4 deletions pymc3/tests/test_aesaraf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def test_change_rv_size():
loc = at.as_tensor_variable([1, 2])
rv = normal(loc=loc)
assert rv.ndim == 1
assert rv.eval().shape == (2,)
assert tuple(rv.shape.eval()) == (2,)

rv_new = change_rv_size(rv, new_size=(3,), expand=True)
assert rv_new.ndim == 2
assert rv_new.eval().shape == (3, 2)
assert tuple(rv_new.shape.eval()) == (3, 2)

# Make sure that the shape used to determine the expanded size doesn't
# depend on the old `RandomVariable`.
Expand All @@ -65,7 +65,7 @@ def test_change_rv_size():

rv_newer = change_rv_size(rv_new, new_size=(4,), expand=True)
assert rv_newer.ndim == 3
assert rv_newer.eval().shape == (4, 3, 2)
assert tuple(rv_newer.shape.eval()) == (4, 3, 2)

# Make sure we avoid introducing a `Cast` by converting the new size before
# constructing the new `RandomVariable`
Expand All @@ -74,7 +74,19 @@ def test_change_rv_size():
rv_newer = change_rv_size(rv, new_size=new_size, expand=False)
assert rv_newer.ndim == 2
assert isinstance(rv_newer.owner.inputs[1], Constant)
assert rv_newer.eval().shape == (4, 3)
assert tuple(rv_newer.shape.eval()) == (4, 3)

rv = normal(0, 1)
new_size = at.as_tensor(np.array([4, 3], dtype="int32"))
rv_newer = change_rv_size(rv, new_size=new_size, expand=True)
assert rv_newer.ndim == 2
assert tuple(rv_newer.shape.eval()) == (4, 3)

rv = normal(0, 1)
new_size = at.as_tensor(2, dtype="int32")
rv_newer = change_rv_size(rv, new_size=new_size, expand=True)
assert rv_newer.ndim == 1
assert tuple(rv_newer.shape.eval()) == (2,)


class TestBroadcasting:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
aesara>=2.0.8
aesara>=2.0.9
arviz>=0.11.2
cachetools>=4.2.1
dill
Expand Down