Skip to content

Commit

Permalink
Upgrade to Theano-PyMC 1.1.0
Browse files Browse the repository at this point in the history
Many gof imports were changed to explicit imports.
This commit also changes the usage of change_flags because of pending deprecation.
  • Loading branch information
michaelosthege committed Jan 11, 2021
1 parent 8186958 commit 9b884cb
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 91 deletions.
2 changes: 1 addition & 1 deletion docs/source/Advanced_usage_of_Theano_in_PyMC3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Now, we use this to define a theano op, that also computes the gradient::
import theano
import theano.tensor as tt
import theano.tests.unittest_tools
from theano.gof.op import Op
from theano.graph.op import Op

class MuFromTheta(Op):
itypes = [tt.dscalar]
Expand Down
4 changes: 2 additions & 2 deletions pymc3/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import theano
import theano.tensor as tt

from theano.gof.graph import Apply
from theano.graph.basic import Apply

import pymc3 as pm

Expand Down Expand Up @@ -296,7 +296,7 @@ class Minibatch(tt.TensorVariable):

RNG = collections.defaultdict(list) # type: Dict[str, List[Any]]

@theano.configparser.change_flags(compute_test_value="raise")
@theano.config.change_flags(compute_test_value="raise")
def __init__(
self,
data,
Expand Down
4 changes: 2 additions & 2 deletions pymc3/distributions/dist_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

from theano import scan
from theano.compile.builders import OpFromGraph
from theano.gof.graph import Apply
from theano.gof.op import Op
from theano.graph.basic import Apply
from theano.graph.op import Op
from theano.scalar import UnaryScalarOp, upgrade_to_float_no_complex
from theano.scan import until
from theano.tensor.slinalg import Cholesky
Expand Down
7 changes: 4 additions & 3 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import numpy as np
import theano
import theano.graph.basic
import theano.tensor as tt

from theano import function
Expand Down Expand Up @@ -790,7 +791,7 @@ def draw_values(params, point=None, size=None):
value = _draw_value(next_, point=point, givens=temp_givens, size=size)
givens[next_.name] = (next_, value)
drawn[(next_, size)] = value
except theano.gof.fg.MissingInputError:
except theano.graph.fg.MissingInputError:
# The node failed, so we must add the node's parents to
# the stack of nodes to try to draw from. We exclude the
# nodes in the `params` list.
Expand Down Expand Up @@ -833,7 +834,7 @@ def draw_values(params, point=None, size=None):
value = _draw_value(param, point=point, givens=givens.values(), size=size)
evaluated[param_idx] = drawn[(param, size)] = value
givens[param.name] = (param, value)
except theano.gof.fg.MissingInputError:
except theano.graph.fg.MissingInputError:
missing_inputs.add(param_idx)

return [evaluated[j] for j in params] # set the order back
Expand Down Expand Up @@ -994,7 +995,7 @@ def _draw_value(param, point=None, givens=None, size=None):
variables = values = []
# We only truly care if the ancestors of param that were given
# value have the matching dshape and val.shape
param_ancestors = set(theano.gof.graph.ancestors([param], blockers=list(variables)))
param_ancestors = set(theano.graph.basic.ancestors([param], blockers=list(variables)))
inputs = [(var, val) for var, val in zip(variables, values) if var in param_ancestors]
if inputs:
input_vars, input_vals = list(zip(*inputs))
Expand Down
2 changes: 1 addition & 1 deletion pymc3/distributions/mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def _comp_logp(self, value):
val_shape = tuple(value.shape.eval())
except AttributeError:
val_shape = value.shape
except theano.gof.MissingInputError:
except theano.graph.fg.MissingInputError:
val_shape = None
try:
self_shape = tuple(self.shape)
Expand Down
6 changes: 3 additions & 3 deletions pymc3/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import theano.tensor as tt

from scipy import linalg, stats
from theano.gof.graph import Apply
from theano.gof.op import Op, get_test_value
from theano.gof.utils import TestValueError
from theano.graph.basic import Apply
from theano.graph.op import Op, get_test_value
from theano.graph.utils import TestValueError
from theano.tensor.nlinalg import det, eigh, matrix_inverse, trace
from theano.tensor.slinalg import Cholesky

Expand Down
11 changes: 7 additions & 4 deletions pymc3/distributions/posterior_predictive.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
)

import numpy as np
import theano
import theano.graph.basic
import theano.graph.fg
import theano.tensor as tt

from arviz import InferenceData
Expand Down Expand Up @@ -414,7 +415,7 @@ def draw_values(self) -> List[np.ndarray]:
assert isinstance(value, np.ndarray)
givens[next_.name] = (next_, value)
drawn[(next_, samples)] = value
except theano.gof.fg.MissingInputError:
except theano.graph.fg.MissingInputError:
# The node failed, so we must add the node's parents to
# the stack of nodes to try to draw from. We exclude the
# nodes in the `params` list.
Expand Down Expand Up @@ -459,7 +460,7 @@ def draw_values(self) -> List[np.ndarray]:
assert isinstance(value, np.ndarray)
self.evaluated[param_idx] = drawn[(param, samples)] = value
givens[param.name] = (param, value)
except theano.gof.fg.MissingInputError:
except theano.graph.fg.MissingInputError:
missing_inputs.add(param_idx)
return [self.evaluated[j] for j in params]

Expand Down Expand Up @@ -653,7 +654,9 @@ def random_sample(
variables = values = []
# We only truly care if the ancestors of param that were given
# value have the matching dshape and val.shape
param_ancestors = set(theano.gof.graph.ancestors([param], blockers=list(variables)))
param_ancestors = set(
theano.graph.basic.ancestors([param], blockers=list(variables))
)
inputs = [
(var, val) for var, val in zip(variables, values) if var in param_ancestors
]
Expand Down
4 changes: 2 additions & 2 deletions pymc3/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import theano.tensor.slinalg # pylint: disable=unused-import

from scipy.linalg import block_diag as scipy_block_diag
from theano.gof.graph import Apply
from theano.gof.op import Op
from theano.graph.basic import Apply
from theano.graph.op import Op

# pylint: disable=unused-import
from theano.tensor import (
Expand Down
9 changes: 5 additions & 4 deletions pymc3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
import numpy as np
import scipy.sparse as sps
import theano
import theano.graph.basic
import theano.sparse as sparse
import theano.tensor as tt

from pandas import Series
from theano.compile import SharedVariable
from theano.gof.graph import Apply
from theano.graph.basic import Apply
from theano.tensor.var import TensorVariable

import pymc3 as pm
Expand Down Expand Up @@ -284,7 +285,7 @@ def __enter__(self):
# self._theano_config is set in Model.__new__
self._config_context = None
if hasattr(self, "_theano_config"):
self._config_context = theano.change_flags(**self._theano_config)
self._config_context = theano.config.change_flags(**self._theano_config)
self._config_context.__enter__()
return self

Expand Down Expand Up @@ -1704,7 +1705,7 @@ def pandas_to_array(data):
ret = data
else: # empty mask
ret = data.filled()
elif isinstance(data, theano.gof.graph.Variable):
elif isinstance(data, theano.graph.basic.Variable):
ret = data
elif sps.issparse(data):
ret = data
Expand Down Expand Up @@ -1795,7 +1796,7 @@ def __init__(

if type is None:
data = pandas_to_array(data)
if isinstance(data, theano.gof.graph.Variable):
if isinstance(data, theano.graph.basic.Variable):
type = data.type
else:
type = TensorType(distribution.dtype, data.shape)
Expand Down
4 changes: 2 additions & 2 deletions pymc3/model_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
VarName = str

from theano.compile import SharedVariable
from theano.gof.graph import stack_search
from theano.graph.basic import walk
from theano.tensor import Tensor

import pymc3 as pm
Expand Down Expand Up @@ -69,7 +69,7 @@ def _expand(node) -> Optional[Iterator[Tensor]]:
else:
return None

list(stack_search(deque([func]), _expand, bfs=True))
list(walk(deque([func]), _expand, bfs=True))
return retval

def _filter_parents(self, var, parents) -> Set[VarName]:
Expand Down
4 changes: 2 additions & 2 deletions pymc3/ode/ode.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import theano
import theano.tensor as tt

from theano.gof.graph import Apply
from theano.gof.op import Op, get_test_value
from theano.graph.basic import Apply
from theano.graph.op import Op, get_test_value

from pymc3.exceptions import DtypeError, ShapeError
from pymc3.ode import utils
Expand Down
6 changes: 3 additions & 3 deletions pymc3/sampling_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jax
import numpy as np
import pandas as pd
import theano
import theano.graph.fg

from theano.link.jax.jax_dispatch import jax_funcify

Expand Down Expand Up @@ -45,7 +45,7 @@ def sample_tfp_nuts(

seed = jax.random.PRNGKey(random_seed)

fgraph = theano.gof.FunctionGraph(model.free_RVs, [model.logpt])
fgraph = theano.graph.fg.FunctionGraph(model.free_RVs, [model.logpt])
fns = jax_funcify(fgraph)
logp_fn_jax = fns[0]

Expand Down Expand Up @@ -130,7 +130,7 @@ def sample_numpyro_nuts(

seed = jax.random.PRNGKey(random_seed)

fgraph = theano.gof.FunctionGraph(model.free_RVs, [model.logpt])
fgraph = theano.graph.fg.FunctionGraph(model.free_RVs, [model.logpt])
fns = jax_funcify(fgraph)
logp_fn_jax = fns[0]

Expand Down
4 changes: 2 additions & 2 deletions pymc3/step_methods/gibbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
searchsorted,
)
from numpy.random import uniform
from theano.gof.graph import inputs
from theano.graph.basic import graph_inputs
from theano.tensor import add

from pymc3.distributions.discrete import Categorical
Expand Down Expand Up @@ -80,7 +80,7 @@ def competence(var, has_grad):


def elemwise_logp(model, var):
terms = [v.logp_elemwiset for v in model.basic_RVs if var in inputs([v.logpt])]
terms = [v.logp_elemwiset for v in model.basic_RVs if var in graph_inputs([v.logpt])]
return model.fn(add(*terms))


Expand Down
6 changes: 3 additions & 3 deletions pymc3/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@

@pytest.fixture(scope="function", autouse=True)
def theano_config():
config = theano.configparser.change_flags(compute_test_value="raise")
config = theano.config.change_flags(compute_test_value="raise")
with config:
yield


@pytest.fixture(scope="function", autouse=True)
def exception_verbosity():
config = theano.configparser.change_flags(exception_verbosity="high")
config = theano.config.change_flags(exception_verbosity="high")
with config:
yield


@pytest.fixture(scope="function", autouse=False)
def strict_float32():
if theano.config.floatX == "float32":
config = theano.configparser.change_flags(warn_float64="raise")
config = theano.config.change_flags(warn_float64="raise")
with config:
yield
else:
Expand Down
10 changes: 5 additions & 5 deletions pymc3/tests/test_dist_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_logp(self):
logp = logp_f(cov_val, delta_val)
npt.assert_allclose(logp, expect)

@theano.configparser.change_flags(compute_test_value="ignore")
@theano.config.change_flags(compute_test_value="ignore")
def test_grad(self):
np.random.seed(42)

Expand All @@ -190,7 +190,7 @@ def func(chol_vec, delta):
verify_grad(func, [chol_vec_val, delta_val])

@pytest.mark.skip(reason="Fix in theano not released yet: Theano#5908")
@theano.configparser.change_flags(compute_test_value="ignore")
@theano.config.change_flags(compute_test_value="ignore")
def test_hessian(self):
chol_vec = tt.vector("chol_vec")
chol_vec.tag.test_value = np.array([0.1, 2, 3])
Expand All @@ -209,14 +209,14 @@ def test_hessian(self):


class TestSplineWrapper:
@theano.configparser.change_flags(compute_test_value="ignore")
@theano.config.change_flags(compute_test_value="ignore")
def test_grad(self):
x = np.linspace(0, 1, 100)
y = x * x
spline = SplineWrapper(interpolate.InterpolatedUnivariateSpline(x, y, k=1))
verify_grad(spline, [0.5])

@theano.configparser.change_flags(compute_test_value="ignore")
@theano.config.change_flags(compute_test_value="ignore")
def test_hessian(self):
x = np.linspace(0, 1, 100)
y = x * x
Expand All @@ -228,7 +228,7 @@ def test_hessian(self):


class TestI0e:
@theano.configparser.change_flags(compute_test_value="ignore")
@theano.config.change_flags(compute_test_value="ignore")
def test_grad(self):
verify_grad(i0e, [0.5])
verify_grad(i0e, [-2.0])
Expand Down
2 changes: 1 addition & 1 deletion pymc3/tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def setup_method(self):
self.op_class = LogDet
self.op = logdet

@theano.configparser.change_flags(compute_test_value="ignore")
@theano.config.change_flags(compute_test_value="ignore")
def validate(self, input_mat):
x = theano.tensor.matrix()
f = theano.function([x], self.op(x))
Expand Down
4 changes: 2 additions & 2 deletions pymc3/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ def test_observed_type(self):

class TestTheanoConfig:
def test_set_testval_raise(self):
with theano.configparser.change_flags(compute_test_value="off"):
with theano.config.change_flags(compute_test_value="off"):
with pm.Model():
assert theano.config.compute_test_value == "raise"
assert theano.config.compute_test_value == "off"

def test_nested(self):
with theano.configparser.change_flags(compute_test_value="off"):
with theano.config.change_flags(compute_test_value="off"):
with pm.Model(theano_config={"compute_test_value": "ignore"}):
assert theano.config.compute_test_value == "ignore"
with pm.Model(theano_config={"compute_test_value": "warn"}):
Expand Down
3 changes: 2 additions & 1 deletion pymc3/tests/test_model_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def test_pandas_to_array(self):

# Check function behavior with Theano graph variable
theano_output = func(theano_graph_input)
assert isinstance(theano_output, theano.gof.graph.Variable)
assert isinstance(theano_output, theano.graph.basic.Variable)
assert theano_output == theano_graph_input
assert theano_output.owner.inputs[0].name == input_name

# Check function behavior with generator data
Expand Down
4 changes: 2 additions & 2 deletions pymc3/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def test_dep_vars(self):
]
)

def test_gof_constant(self):
def test_graph_constant(self):
# Issue 3595 pointed out that slice(None) can introduce
# theano.gof.graph.Constant into the compute graph, which wasn't
# theano.graph.basic.Constant into the compute graph, which wasn't
# handled correctly by draw_values
n_d = 500
n_x = 2
Expand Down
2 changes: 1 addition & 1 deletion pymc3/tests/test_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from numpy.testing import assert_array_almost_equal
from theano.compile.ops import as_op
from theano.gof.op import Op
from theano.graph.op import Op

from pymc3.data import Data
from pymc3.distributions import (
Expand Down
Loading

0 comments on commit 9b884cb

Please sign in to comment.