提交 62e82051 authored 作者: abalkin's avatar abalkin

Issue #783: python3 compatible - more test fixed.

上级 6213cb92
from theano.gof.graph import list_of_nodes
from theano.gof.python25 import any, defaultdict
try:
cmp
except NameError:
def cmp(a, b):
return (a > b) - (a < b)
## {{{ http://code.activestate.com/recipes/578231/ (r1)
# Copyright (c) Oren Tirosh 2012
......
......@@ -615,7 +615,7 @@ def guess_n_streams(size, warn=True):
for s in size:
r *= s
if r > 6:
r = r/6 # chosen as fastest for rbm_benchmark
r = r//6 # chosen as fastest for rbm_benchmark
# The purpose of sampling from many streams is to be able to use
# the GPU to its full capacity. It just wastes RAM and stream-initialization time to
......@@ -741,7 +741,7 @@ class MRG_RandomStreams(object):
if isinstance(size, tuple):
msg = "size must be a tuple of int or a Theano variable"
assert all([isinstance(i,int) or isinstance(i,Variable)
assert all([isinstance(i, (numpy.integer, int)) or isinstance(i,Variable)
for i in size]), msg
if any([isinstance(i, int) and i <= 0 for i in size]):
raise ValueError(
......
......@@ -4670,7 +4670,7 @@ class Subtensor(Op):
return pos[1]
def init_entry(entry, depth=0):
if isinstance(entry, int):
if isinstance(entry, (numpy.integer, int)):
init_cmds.append(
"subtensor_spec[%i] = %i;" % (spec_pos(),
entry))
......
......@@ -354,7 +354,7 @@ class ConvOp(OpenMPOp):
all_shape = imshp is not None and kshp is not None and \
nkern is not None and bsize is not None
if (unroll_batch > 0 or unroll_kern > 0) and not all_shape:
if (unroll_batch or unroll_kern) and not all_shape:
raise Exception("In ConvOp, when using unroll_batch and"
" unroll_nkern, all shape are needed")
......@@ -409,8 +409,8 @@ class ConvOp(OpenMPOp):
if self.unroll_kern and not self.unroll_batch:
self.unroll_batch = 1
#downcast unroll_batch if not a divisor of batch size
if self.unroll_batch > 0 and self.bsize % self.unroll_batch != 0:
# downcast unroll_batch if not a divisor of batch size
if self.unroll_batch is not None and self.unroll_batch > 0 and self.bsize % self.unroll_batch != 0:
if self.bsize <= self.unroll_batch:
self.unroll_batch = self.bsize
......@@ -430,7 +430,7 @@ class ConvOp(OpenMPOp):
self.unroll_batch = new
#downcast unroll_kern if not a divisor of nb of kernel
if self.unroll_kern > 0 and self.nkern % self.unroll_kern != 0:
if self.unroll_kern is not None and self.unroll_kern > 0 and self.nkern % self.unroll_kern != 0:
if self.nkern <= self.unroll_kern:
self.unroll_kern = self.nkern
......@@ -1217,7 +1217,8 @@ if(kerns_dim[3] %% %(self_kshp1)s!=0){
_logger.debug("return unroll patch version. all_shape=%s",
all_shape)
return _conv_op_code_unroll_patch % d
if self.unroll_batch > 0 or self.unroll_kern > 0:
if ((self.unroll_batch is not None and self.unroll_batch > 0) or
(self.unroll_kern is not None and self.unroll_kern > 0)):
assert self.unroll_batch > 0
assert self.unroll_kern > 0
if self.verbose:
......
......@@ -2,6 +2,21 @@
#import traceback
import itertools
import sys
# Copied from tensor/tests/test_basic.py.
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]
import theano.tensor as T
from theano import tensor
from theano.gof.python25 import product as itertools_product
......@@ -112,7 +127,7 @@ class t_gemm(TestCase):
try:
g = gemm_inplace([1.], 1., [1.], [1.], 1.)
except TypeError, e:
if e[0] is Gemm.E_rank:
if exc_message(e) is Gemm.E_rank:
return
self.fail()
......@@ -120,7 +135,7 @@ class t_gemm(TestCase):
try:
self.cmp(1., 0., 1.0, 1.0, 1.0)
except TypeError, e:
if e[0] is Gemm.E_rank:
if exc_message(e) is Gemm.E_rank:
return
self.fail()
......@@ -128,7 +143,7 @@ class t_gemm(TestCase):
try:
self.cmp(2., 1.0, [3, 2, 1.], [[1], [2], [3.]], 1.0)
except TypeError, e:
self.assertTrue(e[0] == Gemm.E_rank)
self.assertTrue(exc_message(e) == Gemm.E_rank)
return
self.fail()
......@@ -220,7 +235,7 @@ class t_gemm(TestCase):
try:
gemm_inplace(Z, 1.0, Z, Z, 1.0)
except InconsistencyError, e:
if e[0] == Gemm.E_z_uniq:
if exc_message(e) == Gemm.E_z_uniq:
return
self.fail()
......@@ -231,7 +246,7 @@ class t_gemm(TestCase):
try:
gemm_inplace(Z, 1.0, A, inplace.transpose_inplace(Z), 1.0)
except InconsistencyError, e:
if e[0] == Gemm.E_z_uniq:
if exc_message(e) == Gemm.E_z_uniq:
return
self.fail()
......@@ -242,7 +257,7 @@ class t_gemm(TestCase):
try:
gemm_inplace(Z, 1.0, inplace.transpose_inplace(Z), A, 1.0)
except InconsistencyError, e:
if e[0] == Gemm.E_z_uniq:
if exc_message(e) == Gemm.E_z_uniq:
return
self.fail()
......@@ -253,7 +268,7 @@ class t_gemm(TestCase):
try:
gemm_inplace(Z, 1.0, Z, A, 1.0)
except InconsistencyError, e:
if e[0] == Gemm.E_z_uniq:
if exc_message(e) == Gemm.E_z_uniq:
return
self.fail()
......@@ -325,7 +340,7 @@ class t_gemm(TestCase):
try:
t(C.T, A[:2, :], B[:, :2].T)
except ValueError, e:
if e[0].find('aligned') >= 0:
if exc_message(e).find('aligned') >= 0:
return
self.fail()
......
......@@ -17,5 +17,5 @@ def disturb_mem():
ms = now.microsecond
ms = int(ms)
n = ms % 1000
m = ms / 1000
m = ms // 1000
l = [[0]*m for i in xrange(n)]
......@@ -732,12 +732,12 @@ class T_loading_and_saving(unittest.TestCase):
tmpdir = mkdtemp()
os.chdir(tmpdir)
f = file('obj.save', 'wb')
f = open('obj.save', 'wb')
cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
f.close()
f = file('obj.save', 'rb')
f = open('obj.save', 'rb')
loaded_obj = cPickle.load(f)
f.close()
......@@ -745,12 +745,12 @@ class T_loading_and_saving(unittest.TestCase):
obj2 = my_obj
obj3 = my_obj
f = file('objects.save', 'wb')
f = open('objects.save', 'wb')
for obj in [obj1, obj2, obj3]:
cPickle.dump(obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
f.close()
f = file('objects.save', 'rb')
f = open('objects.save', 'rb')
loaded_objects = []
for i in range(3):
loaded_objects.append(cPickle.load(f))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论