Skip to content

Commit c396430

Browse files
committed
Revert "MAINT Samplers should keep the same dtype as provided by theano. (#1253)"
This reverts commit 680a8eb.
1 parent 786271c commit c396430

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

pymc3/distributions/distribution.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
22
import theano.tensor as tt
3-
import theano
43
from theano import function
54

65
from ..memoize import memoize
@@ -79,20 +78,20 @@ def TensorType(dtype, shape):
7978
return tt.TensorType(str(dtype), np.atleast_1d(shape) == 1)
8079

8180
class NoDistribution(Distribution):
82-
81+
8382
def __init__(self, shape, dtype, testval=None, defaults=[], transform=None, parent_dist=None, *args, **kwargs):
84-
super(NoDistribution, self).__init__(shape=shape, dtype=dtype,
85-
testval=testval, defaults=defaults,
83+
super(NoDistribution, self).__init__(shape=shape, dtype=dtype,
84+
testval=testval, defaults=defaults,
8685
*args, **kwargs)
8786
self.parent_dist = parent_dist
88-
87+
8988

9089
def __getattr__(self, name):
9190
try:
9291
self.__dict__[name]
9392
except KeyError:
9493
return getattr(self.parent_dist, name)
95-
94+
9695
def logp(self, x):
9796
return 0
9897

@@ -103,12 +102,12 @@ def __init__(self, shape=(), dtype='int64', defaults=['mode'], *args, **kwargs):
103102

104103
class Continuous(Distribution):
105104
"""Base class for continuous distributions"""
106-
def __init__(self, shape=(), dtype=theano.config.floatX, defaults=['median', 'mean', 'mode'], *args, **kwargs):
105+
def __init__(self, shape=(), dtype='float64', defaults=['median', 'mean', 'mode'], *args, **kwargs):
107106
super(Continuous, self).__init__(shape, dtype, defaults=defaults, *args, **kwargs)
108107

109108
class DensityDist(Distribution):
110109
"""Distribution based on a given log density function."""
111-
def __init__(self, logp, shape=(), dtype=theano.config.floatX,testval=0, *args, **kwargs):
110+
def __init__(self, logp, shape=(), dtype='float64',testval=0, *args, **kwargs):
112111
super(DensityDist, self).__init__(shape, dtype, testval, *args, **kwargs)
113112
self.logp = logp
114113

pymc3/step_methods/metropolis.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,9 @@ def astep(self, q0):
121121
q = (q0 + delta).astype(int)
122122
else:
123123
delta[self.discrete] = round(delta[self.discrete], 0).astype(int)
124-
q = (q0 + delta)
124+
q = q0 + delta
125125
else:
126-
q0 = q0.astype(theano.config.floatX)
127-
q = (q0 + delta).astype(theano.config.floatX)
126+
q = q0 + delta
128127

129128
q_new = metrop_select(self.delta_logp(q, q0), q, q0)
130129

@@ -183,7 +182,7 @@ def tune(scale, acc_rate):
183182

184183
class BinaryMetropolis(ArrayStep):
185184
"""Metropolis-Hastings optimized for binary variables
186-
185+
187186
Parameters
188187
----------
189188
vars : list
@@ -196,7 +195,7 @@ class BinaryMetropolis(ArrayStep):
196195
The frequency of tuning. Defaults to 100 iterations.
197196
model : PyMC Model
198197
Optional model for sampling step. Defaults to None (taken from context).
199-
198+
200199
"""
201200

202201
def __init__(self, vars, scaling=1., tune=True, tune_interval=100, model=None):
@@ -295,6 +294,6 @@ def delta_logp(logp, vars, shared):
295294

296295
logp1 = CallableTensor(logp0)(inarray1)
297296

298-
f = theano.function([inarray1, inarray0], logp1 - logp0, allow_input_downcast=True)
297+
f = theano.function([inarray1, inarray0], logp1 - logp0)
299298
f.trust_input = True
300299
return f

pymc3/step_methods/nuts.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ def __init__(self, vars=None, scaling=None, step_scale=0.25, is_cov=False, state
6969
if isinstance(scaling, dict):
7070
scaling = guess_scaling(Point(scaling, model=model), model=model, vars = vars)
7171

72-
scaling = scaling.astype(theano.config.floatX)
72+
7373

7474
n = scaling.shape[0]
7575

7676
self.step_size = step_scale / n**(1/4.)
7777

78+
7879
self.potential = quad_potential(scaling, is_cov, as_cov=False)
7980

8081
if state is None:
@@ -91,26 +92,27 @@ def __init__(self, vars=None, scaling=None, step_scale=0.25, is_cov=False, state
9192
self.u = log(self.step_size*10)
9293
self.m = 1
9394

95+
96+
9497
shared = make_shared_replacements(vars, model)
9598
self.leapfrog1_dE = leapfrog1_dE(model.logpt, vars, shared, self.potential, profile=profile)
9699

97100
super(NUTS, self).__init__(vars, shared, **kwargs)
98101

99102
def astep(self, q0):
100-
q0 = q0.astype(theano.config.floatX)
101103
H = self.leapfrog1_dE #Hamiltonian(self.logp, self.dlogp, self.potential)
102104
Emax = self.Emax
103-
e = array(self.step_size, dtype=theano.config.floatX)
105+
e = self.step_size
104106

105-
p0 = self.potential.random().astype(theano.config.floatX)
107+
p0 = self.potential.random()
106108
u = uniform()
107109
q = qn = qp = q0
108110
p = pn = pp = p0
109111

110112
n, s, j = 1, 1, 0
111113

112114
while s == 1:
113-
v = array(bern(.5) * 2 - 1, dtype=theano.config.floatX)
115+
v = bern(.5) * 2 - 1
114116

115117
if v == -1:
116118
qn, pn, _, _, q1, n1, s1, a, na = buildtree(H, qn, pn, u, v, j, e, Emax, q0, p0)
@@ -194,22 +196,22 @@ def leapfrog1_dE(logp, vars, shared, pot, profile):
194196

195197
H = Hamiltonian(logp, dlogp, pot)
196198

197-
p = tt.vector('p')
199+
p = tt.dvector('p')
198200
p.tag.test_value = q.tag.test_value
199201

200-
q0 = tt.vector('q0')
202+
q0 = tt.dvector('q0')
201203
q0.tag.test_value = q.tag.test_value
202-
p0 = tt.vector('p0')
204+
p0 = tt.dvector('p0')
203205
p0.tag.test_value = p.tag.test_value
204206

205-
e = tt.scalar('e')
206-
e.tag.test_value = 1.
207+
e = tt.dscalar('e')
208+
e.tag.test_value = 1
207209

208210
q1, p1 = leapfrog(H, q, p, 1, e)
209211
E = energy(H, q1, p1)
210212
E0 = energy(H, q0, p0)
211213
dE = E - E0
212214

213-
f = theano.function([q, p, e, q0, p0], [q1, p1, dE], profile=profile, allow_input_downcast=True)
215+
f = theano.function([q, p, e, q0, p0], [q1, p1, dE], profile=profile)
214216
f.trust_input = True
215217
return f

pymc3/vartypes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import theano
2-
31
__all__ = ['bool_types', 'int_types', 'float_types', 'complex_types', 'continuous_types',
42
'discrete_types', 'default_type', 'typefilter']
53

@@ -21,9 +19,9 @@
2119
discrete_types = bool_types | int_types
2220

2321
default_type = {'discrete': 'int64',
24-
'continuous': theano.config.floatX}
22+
'continuous': 'float64'}
2523

2624

2725
def typefilter(vars, types):
2826
# Returns variables of type `types` from `vars`
29-
return [v for v in vars if v.dtype in types]
27+
return [v for v in vars if v.dtype in types]

0 commit comments

Comments
 (0)