提交 19ec1bfd authored 作者: abalkin's avatar abalkin

Issue #783: python3 compatible - fixed remaining test failures.

Python 3 port now passes default "theano-nose" tests under pythobn 3.2.
上级 c4441bd4
from unittest import TestCase from unittest import TestCase
from six import PY3
if PY3:
# In python 3.x, when an exception is reraised it saves original
# exception in its args, therefore in order to find the actual
# message, we need to unpack arguments recurcively.
def exc_message(e):
msg = e.args[0]
if isinstance(msg, Exception):
return exc_message(msg)
return msg
else:
def exc_message(e):
return e[0]
from theano.gof.optdb import opt, DB from theano.gof.optdb import opt, DB
...@@ -21,7 +34,7 @@ class Test_DB(TestCase): ...@@ -21,7 +34,7 @@ class Test_DB(TestCase):
db.register('c', Opt()) # name taken db.register('c', Opt()) # name taken
self.fail() self.fail()
except ValueError, e: except ValueError, e:
if e[0].startswith("The name"): if exc_message(e).startswith("The name"):
pass pass
else: else:
raise raise
...@@ -32,7 +45,7 @@ class Test_DB(TestCase): ...@@ -32,7 +45,7 @@ class Test_DB(TestCase):
db.register('z', Opt()) # name collides with tag db.register('z', Opt()) # name collides with tag
self.fail() self.fail()
except ValueError, e: except ValueError, e:
if e[0].startswith("The name"): if exc_message(e).startswith("The name"):
pass pass
else: else:
raise raise
...@@ -43,7 +56,7 @@ class Test_DB(TestCase): ...@@ -43,7 +56,7 @@ class Test_DB(TestCase):
db.register('u', Opt(), 'b') # name new but tag collides with name db.register('u', Opt(), 'b') # name new but tag collides with name
self.fail() self.fail()
except ValueError, e: except ValueError, e:
if e[0].startswith("The tag"): if exc_message(e).startswith("The tag"):
pass pass
else: else:
raise raise
......
...@@ -5,6 +5,11 @@ import theano ...@@ -5,6 +5,11 @@ import theano
from theano import tensor from theano import tensor
from theano.gof.graph import io_toposort from theano.gof.graph import io_toposort
from theano.gof.python25 import any from theano.gof.python25 import any
try:
cmp
except NameError:
def cmp(a, b):
return (a > b) - (a < b)
def test_dependence(): def test_dependence():
......
...@@ -743,7 +743,7 @@ class MRG_RandomStreams(object): ...@@ -743,7 +743,7 @@ class MRG_RandomStreams(object):
msg = "size must be a tuple of int or a Theano variable" msg = "size must be a tuple of int or a Theano variable"
assert all([isinstance(i, (numpy.integer, int)) or isinstance(i,Variable) assert all([isinstance(i, (numpy.integer, int)) or isinstance(i,Variable)
for i in size]), msg for i in size]), msg
if any([isinstance(i, int) and i <= 0 for i in size]): if any([isinstance(i, (numpy.integer, int)) and i <= 0 for i in size]):
raise ValueError( raise ValueError(
"The specified size contains a dimension with value <= 0", "The specified size contains a dimension with value <= 0",
size) size)
...@@ -873,7 +873,7 @@ class MRG_RandomStreams(object): ...@@ -873,7 +873,7 @@ class MRG_RandomStreams(object):
evened = False evened = False
constant = False constant = False
if isinstance(size, tuple) and all([isinstance(i,int) for i in size]): if isinstance(size, tuple) and all([isinstance(i, (numpy.integer, int)) for i in size]):
constant = True constant = True
n_samples = numpy.prod(size) n_samples = numpy.prod(size)
......
...@@ -2,6 +2,7 @@ from nose.plugins.skip import SkipTest ...@@ -2,6 +2,7 @@ from nose.plugins.skip import SkipTest
import sys import sys
import time import time
import unittest import unittest
from six import next
import theano.sparse import theano.sparse
if not theano.sparse.enable_sparse: if not theano.sparse.enable_sparse:
...@@ -63,7 +64,7 @@ class TestSP(unittest.TestCase): ...@@ -63,7 +64,7 @@ class TestSP(unittest.TestCase):
it = reversed(filters[k, :]) it = reversed(filters[k, :])
for i in range(kshp[0]): for i in range(kshp[0]):
for j in range(kshp[1]): for j in range(kshp[1]):
filtersflipped[k,i,j] = it.next() filtersflipped[k,i,j] = next(it)
# compute output with convolve2d # compute output with convolve2d
if conv_mode == 'valid': if conv_mode == 'valid':
...@@ -314,14 +315,14 @@ class TestSP(unittest.TestCase): ...@@ -314,14 +315,14 @@ class TestSP(unittest.TestCase):
# numeric verification # numeric verification
my_output_val = numpy.zeros((imval.shape[0], imval.shape[1], my_output_val = numpy.zeros((imval.shape[0], imval.shape[1],
imval.shape[2]/maxpoolshp[0], imval.shape[2] // maxpoolshp[0],
imval.shape[3]/maxpoolshp[1])) imval.shape[3] // maxpoolshp[1]))
assert numpy.prod(my_output_val.shape[1:]) == numpy.prod(numpy.r_[imval.shape[1],outshp]) assert numpy.prod(my_output_val.shape[1:]) == numpy.prod(numpy.r_[imval.shape[1],outshp])
for n in range(imval.shape[0]): for n in range(imval.shape[0]):
for k in range(imval.shape[1]): for k in range(imval.shape[1]):
for i in range(imval.shape[2]/maxpoolshp[0]): for i in range(imval.shape[2] // maxpoolshp[0]):
for j in range(imval.shape[3]/maxpoolshp[1]): for j in range(imval.shape[3] // maxpoolshp[1]):
ii,jj = i*maxpoolshp[0], j*maxpoolshp[1] ii,jj = i*maxpoolshp[0], j*maxpoolshp[1]
patch = imval[n,k,ii:ii+maxpoolshp[0],jj:jj+maxpoolshp[1]] patch = imval[n,k,ii:ii+maxpoolshp[0],jj:jj+maxpoolshp[1]]
my_output_val[n,k,i,j] = numpy.max(patch) my_output_val[n,k,i,j] = numpy.max(patch)
......
...@@ -1774,6 +1774,15 @@ class Remove0Tester(utt.InferShapeTester): ...@@ -1774,6 +1774,15 @@ class Remove0Tester(utt.InferShapeTester):
# list of apply nodes in the optimized graph. # list of apply nodes in the optimized graph.
nodes = f.maker.fgraph.toposort() nodes = f.maker.fgraph.toposort()
v = [True for node in nodes] v = [True for node in nodes]
# In python 3, list comprehention variables do not leak
# in the outside scope, so we bind node varible below
# to make the code behave the same under all
# versions. However, the logic here does not look
# right: the length of v is always the same as that of
# nodes and the only result of the assert is to check
# that nodes is not empty. The intent was probably to
# keep if clause inside the [] and check every node.
node = nodes[-1]
if isinstance(node.op, Remove0) and node.op.inplace: if isinstance(node.op, Remove0) and node.op.inplace:
assert len(v), \ assert len(v), \
'Inplacing optimization should have been applied.' 'Inplacing optimization should have been applied.'
...@@ -2060,12 +2069,7 @@ class CastTester(utt.InferShapeTester): ...@@ -2060,12 +2069,7 @@ class CastTester(utt.InferShapeTester):
verify_grad_sparse(Cast(o_dtype), data, eps=eps) verify_grad_sparse(Cast(o_dtype), data, eps=eps)
def _format_info(nb):
class _HVStackTester(utt.InferShapeTester):
"""Test for both HStack and VStack.
"""
nb = 3 # Number of sparse matrix to stack
x = {} x = {}
mat = {} mat = {}
...@@ -2077,6 +2081,15 @@ class _HVStackTester(utt.InferShapeTester): ...@@ -2077,6 +2081,15 @@ class _HVStackTester(utt.InferShapeTester):
mat[format] = [spa(numpy.random.random_integers(5, size=(3, 4)) - 1, mat[format] = [spa(numpy.random.random_integers(5, size=(3, 4)) - 1,
dtype=theano.config.floatX) dtype=theano.config.floatX)
for t in range(nb)] for t in range(nb)]
return x, mat
class _HVStackTester(utt.InferShapeTester):
"""Test for both HStack and VStack.
"""
nb = 3 # Number of sparse matrix to stack
x, mat = _format_info(nb)
def test_op(self): def test_op(self):
for format in sparse.sparse_formats: for format in sparse.sparse_formats:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论