Skip to content

Backport PR 4492 to v3 #4526

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 1 commit into from
Mar 10, 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
4 changes: 2 additions & 2 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
+ ...

### Maintenance
- ⚠ Our memoization mechanism wasn't robust against hash collisions (#4506), sometimes resulting in incorrect values in, for example, posterior predictives. The `pymc3.memoize` module was removed and replaced with `cachetools`. The `hashable` function and `WithMemoization` class were moved to `pymc3.util`.
- ...
- ⚠ Our memoization mechanism wasn't robust against hash collisions (#4506), sometimes resulting in incorrect values in, for example, posterior predictives. The `pymc3.memoize` module was removed and replaced with `cachetools`. The `hashable` function and `WithMemoization` class were moved to `pymc3.util` (see #4525).
- `pm.make_shared_replacements` now retains broadcasting information which fixes issues with Metropolis samplers (see [#4492](https://github.com/pymc-devs/pymc3/pull/4492)).

## PyMC3 3.11.1 (12 February 2021)

Expand Down
25 changes: 25 additions & 0 deletions pymc3/tests/test_theanof.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,38 @@
import theano
import theano.tensor as tt

import pymc3 as pm

from pymc3.theanof import _conversion_map, take_along_axis
from pymc3.vartypes import int_types

FLOATX = str(theano.config.floatX)
INTX = str(_conversion_map[FLOATX])


class TestBroadcasting:
def test_make_shared_replacements(self):
"""Check if pm.make_shared_replacements preserves broadcasting."""

with pm.Model() as test_model:
test1 = pm.Normal("test1", mu=0.0, sigma=1.0, shape=(1, 10))
test2 = pm.Normal("test2", mu=0.0, sigma=1.0, shape=(10, 1))

# Replace test1 with a shared variable, keep test 2 the same
replacement = pm.make_shared_replacements([test_model.test2], test_model)
assert test_model.test1.broadcastable == replacement[test_model.test1].broadcastable

def test_metropolis_sampling(self):
"""Check if the Metropolis sampler can handle broadcasting."""
with pm.Model() as test_model:
test1 = pm.Normal("test1", mu=0.0, sigma=1.0, shape=(1, 10))
test2 = pm.Normal("test2", mu=test1, sigma=1.0, shape=(10, 10))

step = pm.Metropolis()
# This should fail immediately if broadcasting does not work.
pm.sample(tune=5, draws=7, cores=1, step=step, compute_convergence_checks=False)


def _make_along_axis_idx(arr_shape, indices, axis):
# compute dimensions to iterate over
if str(indices.dtype) not in int_types:
Expand Down
7 changes: 6 additions & 1 deletion pymc3/theanof.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ def make_shared_replacements(vars, model):
Dict of variable -> new shared variable
"""
othervars = set(model.vars) - set(vars)
return {var: theano.shared(var.tag.test_value, var.name + "_shared") for var in othervars}
return {
var: theano.shared(
var.tag.test_value, var.name + "_shared", broadcastable=var.broadcastable
)
for var in othervars
}


def join_nonshared_inputs(xs, vars, shared, make_shared=False):
Expand Down