From 07679ec0bffd664e9477913d4beadfa298ac5d76 Mon Sep 17 00:00:00 2001 From: Kaustubh Date: Sun, 31 Jan 2021 15:59:30 +0530 Subject: [PATCH] Avoid unclear TypeError when using theano.shared variables as input to distribution parameters (#4445) * Added default testvalue support for theano.shared Co-authored-by: Ricardo --- RELEASE-NOTES.md | 1 + pymc3/distributions/distribution.py | 14 +++++++------- pymc3/tests/test_data_container.py | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 939b8d54583..c931ddf2281 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -11,6 +11,7 @@ - We upgraded to `Theano-PyMC v1.1.2` which [includes bugfixes](https://github.com/pymc-devs/aesara/compare/rel-1.1.0...rel-1.1.2) for warning floods and compiledir locking (see [#4444](https://github.com/pymc-devs/pymc3/pull/4444)) - `Theano-PyMC v1.1.2` also fixed an important issue in `tt.switch` that affected the behavior of several PyMC distributions, including at least the `Bernoulli` and `TruncatedNormal` (see[#4448](https://github.com/pymc-devs/pymc3/pull/4448)) - `math.log1mexp_numpy` no longer raises RuntimeWarning when given very small inputs. These were commonly observed during NUTS sampling (see [#4428](https://github.com/pymc-devs/pymc3/pull/4428)). +- `ScalarSharedVariable` can now be used as an input to other RVs directly (see [#4445](https://github.com/pymc-devs/pymc3/pull/4445)). ## PyMC3 3.11.0 (21 January 2021) diff --git a/pymc3/distributions/distribution.py b/pymc3/distributions/distribution.py index 8178ae0d228..c24a9d9df6e 100644 --- a/pymc3/distributions/distribution.py +++ b/pymc3/distributions/distribution.py @@ -148,17 +148,17 @@ def default(self): def get_test_val(self, val, defaults): if val is None: for v in defaults: - if hasattr(self, v) and np.all(np.isfinite(self.getattr_value(v))): - return self.getattr_value(v) - else: - return self.getattr_value(val) - - if val is None: + if hasattr(self, v): + attr_val = self.getattr_value(v) + if np.all(np.isfinite(attr_val)): + return attr_val raise AttributeError( "%s has no finite default value to use, " "checked: %s. Pass testval argument or " "adjust so value is finite." % (self, str(defaults)) ) + else: + return self.getattr_value(val) def getattr_value(self, val): if isinstance(val, string_types): @@ -167,7 +167,7 @@ def getattr_value(self, val): if isinstance(val, tt.TensorVariable): return val.tag.test_value - if isinstance(val, tt.sharedvar.TensorSharedVariable): + if isinstance(val, tt.sharedvar.SharedVariable): return val.get_value() if isinstance(val, theano_constant): diff --git a/pymc3/tests/test_data_container.py b/pymc3/tests/test_data_container.py index d3eaf2fb7ff..966ce47cd6a 100644 --- a/pymc3/tests/test_data_container.py +++ b/pymc3/tests/test_data_container.py @@ -16,6 +16,8 @@ import pandas as pd import pytest +from theano import shared + import pymc3 as pm from pymc3.tests.helpers import SeededTest @@ -156,6 +158,26 @@ def test_shared_data_as_rv_input(self): np.testing.assert_allclose(np.array([2.0, 4.0, 6.0]), x.get_value(), atol=1e-1) np.testing.assert_allclose(np.array([2.0, 4.0, 6.0]), trace["y"].mean(0), atol=1e-1) + def test_shared_scalar_as_rv_input(self): + # See https://github.com/pymc-devs/pymc3/issues/3139 + with pm.Model() as m: + shared_var = shared(5.0) + v = pm.Normal("v", mu=shared_var, shape=1) + + np.testing.assert_allclose( + v.logp({"v": [5.0]}), + -0.91893853, + rtol=1e-5, + ) + + shared_var.set_value(10.0) + + np.testing.assert_allclose( + v.logp({"v": [10.0]}), + -0.91893853, + rtol=1e-5, + ) + def test_creation_of_data_outside_model_context(self): with pytest.raises((IndexError, TypeError)) as error: pm.Data("data", [1.1, 2.2, 3.3])