Skip to content

Commit e46b1b4

Browse files
brandonwillardtwiecki
authored andcommitted
Update as_op, theano.gof imports and stack_search usage
1 parent a607709 commit e46b1b4

File tree

12 files changed

+40
-27
lines changed

12 files changed

+40
-27
lines changed

docs/source/Advanced_usage_of_Theano_in_PyMC3.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ We can now use `scipy.optimize.newton` to find the root::
158158
def mu_from_theta(theta):
159159
return optimize.newton(func, 1, fprime=jac, args=(theta,))
160160

161-
We could wrap `mu_from_theta` with `tt.as_op` and use gradient-free
161+
We could wrap `mu_from_theta` with `theano.compile.ops.as_op` and use gradient-free
162162
methods like Metropolis, but to get NUTS and ADVI working, we also
163163
need to define the derivative of `mu_from_theta`. We can find this
164164
derivative using the implicit function theorem, or equivalently we
@@ -186,8 +186,9 @@ Now, we use this to define a theano op, that also computes the gradient::
186186
import theano
187187
import theano.tensor as tt
188188
import theano.tests.unittest_tools
189+
from theano.gof.op import Op
189190

190-
class MuFromTheta(tt.Op):
191+
class MuFromTheta(Op):
191192
itypes = [tt.dscalar]
192193
otypes = [tt.dscalar]
193194

pymc3/data.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import theano
2727
import theano.tensor as tt
2828

29+
from theano.gof.graph import Apply
30+
2931
import pymc3 as pm
3032

3133
__all__ = [
@@ -320,7 +322,7 @@ def __init__(
320322
minibatch = tt.patternbroadcast(minibatch, broadcastable)
321323
self.minibatch = minibatch
322324
super().__init__(self.minibatch.type, None, None, name=name)
323-
theano.Apply(theano.compile.view_op, inputs=[self.minibatch], outputs=[self])
325+
Apply(theano.compile.view_op, inputs=[self.minibatch], outputs=[self])
324326
self.tag.test_value = copy(self.minibatch.tag.test_value)
325327

326328
def rslice(self, total, size, seed):

pymc3/distributions/dist_math.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
from theano import scan
2929
from theano.compile.builders import OpFromGraph
30+
from theano.gof.graph import Apply
31+
from theano.gof.op import Op
3032
from theano.scalar import UnaryScalarOp, upgrade_to_float_no_complex
3133
from theano.scan import until
3234
from theano.tensor.slinalg import Cholesky
@@ -312,7 +314,7 @@ def dlogp(inputs, gradients):
312314
return OpFromGraph([cov, delta], [logp], grad_overrides=dlogp, inline=True)
313315

314316

315-
class SplineWrapper(theano.Op):
317+
class SplineWrapper(Op):
316318
"""
317319
Creates a theano operation from scipy.interpolate.UnivariateSpline
318320
"""
@@ -324,7 +326,7 @@ def __init__(self, spline):
324326

325327
def make_node(self, x):
326328
x = tt.as_tensor_variable(x)
327-
return tt.Apply(self, [x], [x.type()])
329+
return Apply(self, [x], [x.type()])
328330

329331
@property
330332
def grad_op(self):

pymc3/distributions/multivariate.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import theano.tensor as tt
2424

2525
from scipy import linalg, stats
26-
from theano.gof.op import get_test_value
26+
from theano.gof.graph import Apply
27+
from theano.gof.op import Op, get_test_value
2728
from theano.gof.utils import TestValueError
2829
from theano.tensor.nlinalg import det, eigh, matrix_inverse, trace
2930
from theano.tensor.slinalg import Cholesky
@@ -835,7 +836,7 @@ def posdef(AA):
835836
return 0
836837

837838

838-
class PosDefMatrix(theano.Op):
839+
class PosDefMatrix(Op):
839840
"""
840841
Check if input is positive definite. Input should be a square matrix.
841842
@@ -850,7 +851,7 @@ def make_node(self, x):
850851
x = tt.as_tensor_variable(x)
851852
assert x.ndim == 2
852853
o = tt.TensorType(dtype="int8", broadcastable=[])()
853-
return theano.Apply(self, [x], [o])
854+
return Apply(self, [x], [o])
854855

855856
# Python implementation:
856857
def perform(self, node, inputs, outputs):

pymc3/math.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import theano.tensor.slinalg # pylint: disable=unused-import
2626

2727
from scipy.linalg import block_diag as scipy_block_diag
28-
from theano.gof import Apply, Op
28+
from theano.gof.graph import Apply
29+
from theano.gof.op import Op
2930

3031
# pylint: disable=unused-import
3132
from theano.tensor import (
@@ -340,7 +341,7 @@ def expand_packed_triangular(n, packed, lower=True, diagonal_only=False):
340341
return tt.set_subtensor(out[idxs], packed)
341342

342343

343-
class BatchedDiag(tt.Op):
344+
class BatchedDiag(Op):
344345
"""
345346
Fast BatchedDiag allocation
346347
"""
@@ -352,7 +353,7 @@ def make_node(self, diag):
352353
if diag.type.ndim != 2:
353354
raise TypeError("data argument must be a matrix", diag.type)
354355

355-
return tt.Apply(self, [diag], [tt.tensor3(dtype=diag.dtype)])
356+
return Apply(self, [diag], [tt.tensor3(dtype=diag.dtype)])
356357

357358
def perform(self, node, ins, outs, params=None):
358359
(C,) = ins
@@ -408,7 +409,7 @@ def make_node(self, *matrices):
408409
out_type = theano.sparse.matrix(self.format, dtype=largest_common_dtype(matrices))
409410
else:
410411
out_type = theano.tensor.matrix(dtype=largest_common_dtype(matrices))
411-
return tt.Apply(self, matrices, [out_type])
412+
return Apply(self, matrices, [out_type])
412413

413414
def perform(self, node, inputs, output_storage, params=None):
414415
dtype = largest_common_dtype(inputs)

pymc3/model.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from pandas import Series
3030
from theano.compile import SharedVariable
31+
from theano.gof.graph import Apply
3132
from theano.tensor.var import TensorVariable
3233

3334
import pymc3 as pm
@@ -1817,7 +1818,7 @@ def __init__(
18171818
self.distribution = distribution
18181819

18191820
# make this RV a view on the combined missing/nonmissing array
1820-
theano.gof.Apply(theano.compile.view_op, inputs=[data], outputs=[self])
1821+
Apply(theano.compile.view_op, inputs=[data], outputs=[self])
18211822
self.tag.test_value = theano.compile.view_op(data).tag.test_value.astype(self.dtype)
18221823
self.scaling = _get_scaling(total_size, data.shape, data.ndim)
18231824

@@ -1997,7 +1998,7 @@ def __init__(
19971998

19981999
normalRV = transform.backward(self.transformed)
19992000

2000-
theano.Apply(theano.compile.view_op, inputs=[normalRV], outputs=[self])
2001+
Apply(theano.compile.view_op, inputs=[normalRV], outputs=[self])
20012002
self.tag.test_value = normalRV.tag.test_value
20022003
self.scaling = _get_scaling(total_size, self.shape, self.ndim)
20032004
incorporate_methods(

pymc3/model_graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _expand(node) -> Optional[Iterator[Tensor]]:
6969
else:
7070
return None
7171

72-
stack_search(start=deque([func]), expand=_expand, mode="bfs")
72+
list(stack_search(deque([func]), _expand, bfs=True))
7373
return retval
7474

7575
def _filter_parents(self, var, parents) -> Set[VarName]:

pymc3/ode/ode.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import theano
2020
import theano.tensor as tt
2121

22-
from theano.gof.op import get_test_value
22+
from theano.gof.graph import Apply
23+
from theano.gof.op import Op, get_test_value
2324

2425
from pymc3.exceptions import DtypeError, ShapeError
2526
from pymc3.ode import utils
@@ -28,7 +29,7 @@
2829
floatX = theano.config.floatX
2930

3031

31-
class DifferentialEquation(theano.Op):
32+
class DifferentialEquation(Op):
3233
r"""
3334
Specify an ordinary differential equation
3435
@@ -141,7 +142,7 @@ def make_node(self, y0, theta):
141142

142143
# store symbolic output in dictionary such that it can be accessed in the grad method
143144
self._output_sensitivities[hash(inputs)] = sens
144-
return theano.Apply(self, inputs, (states, sens))
145+
return Apply(self, inputs, (states, sens))
145146

146147
def __call__(self, y0, theta, return_sens=False, **kwargs):
147148
if isinstance(y0, (list, tuple)) and not len(y0) == self.n_states:
@@ -162,7 +163,7 @@ def __call__(self, y0, theta, return_sens=False, **kwargs):
162163
)
163164

164165
# use default implementation to prepare symbolic outputs (via make_node)
165-
states, sens = super(theano.Op, self).__call__(y0, theta, **kwargs)
166+
states, sens = super().__call__(y0, theta, **kwargs)
166167

167168
if theano.config.compute_test_value != "off":
168169
# compute test values from input test values

pymc3/tests/test_parallel_sampling.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import theano
2020
import theano.tensor as tt
2121

22+
from theano.compile.ops import as_op
23+
2224
import pymc3 as pm
2325
import pymc3.parallel_sampling as ps
2426

@@ -61,7 +63,7 @@ def test_bad_unpickle():
6163
tt_vector = tt.TensorType(theano.config.floatX, [False])
6264

6365

64-
@theano.as_op([tt_vector, tt.iscalar], [tt_vector])
66+
@as_op([tt_vector, tt.iscalar], [tt_vector])
6567
def _crash_remote_process(a, master_pid):
6668
if os.getpid() != master_pid:
6769
os.exit(0)

pymc3/tests/test_step.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import theano.tensor as tt
2727

2828
from numpy.testing import assert_array_almost_equal
29+
from theano.compile.ops import as_op
30+
from theano.gof.op import Op
2931

3032
from pymc3.data import Data
3133
from pymc3.distributions import (
@@ -719,7 +721,7 @@ def test_normal_nograd_op(self):
719721
itypes = [tt.dscalar] if is_64 else [tt.fscalar]
720722
otypes = [tt.dscalar] if is_64 else [tt.fscalar]
721723

722-
@theano.as_op(itypes, otypes)
724+
@as_op(itypes, otypes)
723725
def kill_grad(x):
724726
return x
725727

@@ -1456,7 +1458,7 @@ def test_aem_mu_sigma(self):
14561458
np.fill_diagonal(s, sigma ** 2)
14571459

14581460
# forward model Op - here, just the regression equation
1459-
class ForwardModel(tt.Op):
1461+
class ForwardModel(Op):
14601462
if theano.config.floatX == "float32":
14611463
itypes = [tt.fvector]
14621464
otypes = [tt.fvector]
@@ -1598,7 +1600,7 @@ def test_variance_reduction(self):
15981600
nchains = 1
15991601

16001602
# define likelihoods with different Q
1601-
class Likelihood1(tt.Op):
1603+
class Likelihood1(Op):
16021604
if theano.config.floatX == "float32":
16031605
itypes = [tt.fvector]
16041606
otypes = [tt.fscalar]
@@ -1621,7 +1623,7 @@ def perform(self, node, inputs, outputs):
16211623
-(0.5 / s ** 2) * np.sum((temp - self.y) ** 2, dtype=p), dtype=p
16221624
)
16231625

1624-
class Likelihood2(tt.Op):
1626+
class Likelihood2(Op):
16251627
if theano.config.floatX == "float32":
16261628
itypes = [tt.fvector]
16271629
otypes = [tt.fscalar]

pymc3/theanof.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from theano import change_flags, scalar
1919
from theano import tensor as tt
2020
from theano.gof import Op
21-
from theano.gof.graph import inputs
21+
from theano.gof.graph import Apply, inputs
2222
from theano.sandbox.rng_mrg import MRG_RandomStream as RandomStream
2323

2424
from pymc3.blocking import ArrayOrdering
@@ -340,7 +340,7 @@ def __init__(self, gen, default=None):
340340

341341
def make_node(self, *inputs):
342342
gen_var = self.generator.make_variable(self)
343-
return theano.Apply(self, [], [gen_var])
343+
return Apply(self, [], [gen_var])
344344

345345
def perform(self, node, inputs, output_storage, params=None):
346346
if self.default is not None:

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ numpy>=1.15.0
55
pandas>=0.24.0
66
patsy>=0.5.1
77
scipy>=1.2.0
8-
theano-pymc==1.0.14
8+
theano-pymc==1.1.0
99
typing-extensions>=3.7.4

0 commit comments

Comments
 (0)