TypeError with Theano/pymc (比较操作)



我运行的问题与最初在 https://github.com/pymc-devs/pymc3/issues/1209 上报告的问题相同,即在比较 Theano 对象和 numpy 数组时出现 TypeError。我的代码是使用运算符左侧的 Theano 对象编写的,并使用 Numpy 1.13.1 编写

调查我测试的一点

import pymc3
with pymc3.Model() as model:
a = pymc3.Uniform("a", 1,2)
print 1 < a

输出,没有抱怨:

Elemwise{gt,no_inplace}.0

正在运行

with pymc3.Model() as model:
a = pymc3.Uniform("a", 1,2)
if 1 < a:
print "bingo"

产生类型错误

/usr/local/lib/python2.7/dist-packages/theano/tensor/var.pyc in __nonzero__(self)
73     def __nonzero__(self):
74         # Python 2.x
---> 75         return self.__bool__()
76 
77     def __bool__(self):
/usr/local/lib/python2.7/dist-packages/theano/tensor/var.pyc in __bool__(self)
89         else:
90             raise TypeError(
---> 91                 "Variables do not support boolean operations."
92             )
93 
TypeError: Variables do not support boolean operations.

所以我的问题是,我应该如何进行这种类型的测试?我想保留我的代码通用,因为在大多数情况下它不会在 Theano 对象上运行(当然,我可以在 pymc3/Theano 上下文中使用这个函数的一个版本(。因为值得冒犯的代码是

......./refsans_tools/abeles/abeles.py in guess_optimal_x(self, thickness, roughness)
1303                                                safety=self.safety
1304                                               )
-> 1305         if this_xmin < self._xmin:
1306             self._xmin = this_xmin
1307             self._xmin = - self.shift_orig
/usr/local/lib/python2.7/dist-packages/theano/tensor/var.pyc in nonzero(self)
73     def nonzero(self):
74         # Python 2.x
---> 75         return self.bool()
76 
77     def bool(self):
/usr/local/lib/python2.7/dist-packages/theano/tensor/var.pyc in bool(self)
89         else:
90             raise TypeError(
---> 91                 "Variables do not support boolean operations."
92             )
93 
TypeError: Variables do not support boolean operations.

试试这个:

with pymc3.Model() as model:
a = pymc3.Uniform("a", 1,2)
if tt.lt(1, a):
print "bingo"

最新更新