Skip to content

Commit a7410c2

Browse files
notoraptortwiecki
authored andcommitted
Fix the TypeError issue #994 on Windows. (#1550)
* Suggestion to fix the TypeError issue #994 on Windows. Errors occurs because some ndarrays with dtype=int32 are passed to the function delta_logp which expects ndarrays with dtype=int64 as inputs. Inputs are casted with `ndarray.astype(int)`, but on Windows, the Python 'int' type is treated as a 32-bit integer by NumPy. I suggest to remove the line with `f.trust_input = True` to ensure that Theano always checks the inputs and casts it if necessary and when possible. This fix resolves the issue on my computer: ``` Python version: 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] OS version: Windows-8.1-6.3.9600 ``` * Discrete super-class raises TypeError if dtype != 'int64'. Arguments for delta_logp in Metropolis.astep() are now casted to 'int64' when Metropolis handles a discrete model.
1 parent f61d8cd commit a7410c2

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

pymc3/distributions/distribution.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class Discrete(Distribution):
105105
"""Base class for discrete distributions"""
106106

107107
def __init__(self, shape=(), dtype='int64', defaults=['mode'], *args, **kwargs):
108+
if dtype != 'int64':
109+
raise TypeError('Discrete classes expect dtype to be int64.')
108110
super(Discrete, self).__init__(
109111
shape, dtype, defaults=defaults, *args, **kwargs)
110112

pymc3/step_methods/metropolis.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ def astep(self, q0):
111111

112112
if self.any_discrete:
113113
if self.all_discrete:
114-
delta = np.round(delta, 0).astype(int)
115-
q0 = q0.astype(int)
116-
q = (q0 + delta).astype(int)
114+
delta = np.round(delta, 0).astype('int64')
115+
q0 = q0.astype('int64')
116+
q = (q0 + delta).astype('int64')
117117
else:
118118
delta[self.discrete] = np.round(
119-
delta[self.discrete], 0).astype(int)
120-
q = q0 + delta
119+
delta[self.discrete], 0).astype('int64')
120+
q = (q0 + delta).astype('int64')
121121
else:
122122
q = q0 + delta
123123

@@ -431,5 +431,5 @@ def delta_logp(logp, vars, shared):
431431
logp1 = pm.CallableTensor(logp0)(inarray1)
432432

433433
f = theano.function([inarray1, inarray0], logp1 - logp0)
434-
f.trust_input = True
434+
# f.trust_input = True
435435
return f

0 commit comments

Comments
 (0)