提交 353b0688 authored 作者: abergeron's avatar abergeron

Merge pull request #1813 from nouiz/assert_

Fix tests in python3.
...@@ -36,13 +36,11 @@ def test_unpickle_cudandarray_as_numpy_ndarray_flag0(): ...@@ -36,13 +36,11 @@ def test_unpickle_cudandarray_as_numpy_ndarray_flag0():
with open(os.path.join(testfile_dir, fname), 'rb') as fp: with open(os.path.join(testfile_dir, fname), 'rb') as fp:
if cuda_available: if cuda_available:
mat = cPickle.load(fp) mat = cPickle.load(fp)
assert isinstance(mat, CudaNdarray)
assert numpy.asarray(mat)[0] == -42.0
else: else:
assert_raises(ImportError, cPickle.load, fp) assert_raises(ImportError, cPickle.load, fp)
if cuda_available:
assert isinstance(mat, CudaNdarray)
assert numpy.asarray(mat)[0] == -42.0
finally: finally:
config.experimental.unpickle_gpu_on_cpu = oldflag config.experimental.unpickle_gpu_on_cpu = oldflag
...@@ -53,8 +51,11 @@ def test_unpickle_cudandarray_as_numpy_ndarray_flag1(): ...@@ -53,8 +51,11 @@ def test_unpickle_cudandarray_as_numpy_ndarray_flag1():
try: try:
testfile_dir = os.path.dirname(os.path.realpath(__file__)) testfile_dir = os.path.dirname(os.path.realpath(__file__))
fname = 'CudaNdarray.pkl'
if sys.version_info[0] == 3:
fname = 'CudaNdarray_py3.pkl'
with open(os.path.join(testfile_dir, 'CudaNdarray.pkl')) as fp: with open(os.path.join(testfile_dir, fname), 'rb') as fp:
mat = cPickle.load(fp) mat = cPickle.load(fp)
assert isinstance(mat, numpy.ndarray) assert isinstance(mat, numpy.ndarray)
......
...@@ -1411,6 +1411,10 @@ class Assert(T.Op): ...@@ -1411,6 +1411,10 @@ class Assert(T.Op):
return [input_shapes[0]] return [input_shapes[0]]
assert_ = Assert() assert_ = Assert()
#Unittest.assert_ is a deprecated name for assertTrue.
#2to3 convert theano.tensor.opt.assert_ to theano.tensor.opt.assertTrue
#So I define a new name as a work around.
assert_op = assert_
@register_specialize @register_specialize
......
...@@ -419,7 +419,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -419,7 +419,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
try: try:
f_xv = f(xv) f_xv = f(xv)
self.assertTrue((f_xv.shape == zv.shape), (f_xv, zv)) self.assertTrue((f_xv.shape == zv.shape), (f_xv, zv))
self.assertTrue(numpy.allclose(f_xv, zv), (f_xv, zv)) self.assertTrue(numpy.allclose(f_xv, zv), (f_xv, zv, xsh, tosum))
except NotImplementedError: except NotImplementedError:
# GpuCAReduce don't implement all cases when size is 0 # GpuCAReduce don't implement all cases when size is 0
assert xv.size == 0 assert xv.size == 0
......
...@@ -2789,7 +2789,7 @@ class test_assert(utt.InferShapeTester): ...@@ -2789,7 +2789,7 @@ class test_assert(utt.InferShapeTester):
def test0(self): def test0(self):
x = T.scalar() x = T.scalar()
y = T.scalar() y = T.scalar()
f = theano.function([x, y], theano.tensor.opt.assert_(x, T.eq(x, y))) f = theano.function([x, y], theano.tensor.opt.assert_op(x, T.eq(x, y)))
f(1, 1) f(1, 1)
self.assertRaises(AssertionError, f, 1, 0) self.assertRaises(AssertionError, f, 1, 0)
...@@ -2801,7 +2801,7 @@ class test_assert(utt.InferShapeTester): ...@@ -2801,7 +2801,7 @@ class test_assert(utt.InferShapeTester):
mode = compile.mode.get_mode(mode) mode = compile.mode.get_mode(mode)
x = T.scalar() x = T.scalar()
f = theano.function([x], theano.tensor.opt.assert_(x, 1), mode=mode) f = theano.function([x], theano.tensor.opt.assert_op(x, 1), mode=mode)
assert f(1) == 1 assert f(1) == 1
assert f(5) == 5 assert f(5) == 5
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
...@@ -2817,7 +2817,7 @@ class test_assert(utt.InferShapeTester): ...@@ -2817,7 +2817,7 @@ class test_assert(utt.InferShapeTester):
x = T.scalar() x = T.scalar()
y = T.scalar() y = T.scalar()
f = theano.function([x, y], theano.tensor.opt.assert_(x, y, 1), f = theano.function([x, y], theano.tensor.opt.assert_op(x, y, 1),
mode=mode) mode=mode)
assert f(1, 1) == 1 assert f(1, 1) == 1
assert f(5, 1) == 5 assert f(5, 1) == 5
...@@ -2835,7 +2835,7 @@ class test_assert(utt.InferShapeTester): ...@@ -2835,7 +2835,7 @@ class test_assert(utt.InferShapeTester):
x = T.scalar() x = T.scalar()
y = T.scalar() y = T.scalar()
f = theano.function([x, y], theano.tensor.opt.assert_(x, y, 0), f = theano.function([x, y], theano.tensor.opt.assert_op(x, y, 0),
mode=mode) mode=mode)
self.assertRaises(AssertionError, f, 1, 0) self.assertRaises(AssertionError, f, 1, 0)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
...@@ -2849,14 +2849,14 @@ class test_assert(utt.InferShapeTester): ...@@ -2849,14 +2849,14 @@ class test_assert(utt.InferShapeTester):
bdscal = dscalar() bdscal = dscalar()
adscal_val = numpy.random.rand() adscal_val = numpy.random.rand()
bdscal_val = numpy.random.rand() + 1 bdscal_val = numpy.random.rand() + 1
out = theano.tensor.opt.assert_(adscal, bdscal) out = theano.tensor.opt.assert_op(adscal, bdscal)
self._compile_and_check([adscal, bdscal], [out], self._compile_and_check([adscal, bdscal], [out],
[adscal_val, bdscal_val], Assert) [adscal_val, bdscal_val], Assert)
admat = dmatrix() admat = dmatrix()
admat_val = numpy.random.rand(3, 4) admat_val = numpy.random.rand(3, 4)
adscal_val += 1 adscal_val += 1
out = theano.tensor.opt.assert_(admat, adscal, bdscal) out = theano.tensor.opt.assert_op(admat, adscal, bdscal)
self._compile_and_check([admat, adscal, bdscal], [out], self._compile_and_check([admat, adscal, bdscal], [out],
[admat_val, adscal_val, bdscal_val], Assert) [admat_val, adscal_val, bdscal_val], Assert)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论