提交 6a936ae7 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Have the type constructor be part of the test class.

上级 98af764b
...@@ -27,6 +27,7 @@ def FunctionGraph(i, o): ...@@ -27,6 +27,7 @@ def FunctionGraph(i, o):
class test_DimShuffle(unittest_tools.InferShapeTester): class test_DimShuffle(unittest_tools.InferShapeTester):
op = DimShuffle op = DimShuffle
type = TensorType
def with_linker(self, linker): def with_linker(self, linker):
for xsh, shuffle, zsh in [((2, 3), (1, 'x', 0), (3, 1, 2)), for xsh, shuffle, zsh in [((2, 3), (1, 'x', 0), (3, 1, 2)),
...@@ -40,12 +41,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -40,12 +41,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
((1, 1, 1), (), ()), ((1, 1, 1), (), ()),
((1,), ('x', 'x'), (1, 1))]: ((1,), ('x', 'x'), (1, 1))]:
ib = [(entry == 1) for entry in xsh] ib = [(entry == 1) for entry in xsh]
x = TensorType('float64', ib)('x') x = self.type('float64', ib)('x')
e = self.op(ib, shuffle)(x) e = self.op(ib, shuffle)(x)
f = copy(linker).accept(FunctionGraph([x], [e])).make_function() f = copy(linker).accept(FunctionGraph([x], [e])).make_function()
assert f(numpy.ones(xsh)).shape == zsh assert f(numpy.ones(xsh)).shape == zsh
# test that DimShuffle.infer_shape work correctly # test that DimShuffle.infer_shape work correctly
x = TensorType('float64', ib)('x') x = self.type('float64', ib)('x')
e = self.op(ib, shuffle)(x) e = self.op(ib, shuffle)(x)
f = copy(linker).accept(FunctionGraph([x], f = copy(linker).accept(FunctionGraph([x],
[e.shape])).make_function() [e.shape])).make_function()
...@@ -53,12 +54,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -53,12 +54,12 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
# Test when we drop a axis that is not broadcastable # Test when we drop a axis that is not broadcastable
ib = [False, True, False] ib = [False, True, False]
x = TensorType('float64', ib)('x') x = self.type('float64', ib)('x')
self.assertRaises(ValueError, self.op, ib, shuffle) self.assertRaises(ValueError, self.op, ib, shuffle)
# Test when we drop a axis that don't have shape 1 # Test when we drop a axis that don't have shape 1
ib = [True, True, False] ib = [True, True, False]
x = TensorType('float64', ib)('x') x = self.type('float64', ib)('x')
e = self.op(ib, (1, 2))(x) e = self.op(ib, (1, 2))(x)
f = copy(linker).accept(FunctionGraph([x], [e.shape])).make_function() f = copy(linker).accept(FunctionGraph([x], [e.shape])).make_function()
self.assertRaises(TypeError, f, numpy.ones((2, 1, 4))) self.assertRaises(TypeError, f, numpy.ones((2, 1, 4)))
...@@ -66,7 +67,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -66,7 +67,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
# Test that we can't take a dimensions multiple time # Test that we can't take a dimensions multiple time
xsh, shuffle, zsh = ((1, 1, 4), (0, 1, 2, 0), (1, 4)) xsh, shuffle, zsh = ((1, 1, 4), (0, 1, 2, 0), (1, 4))
ib = [False, True, False] ib = [False, True, False]
x = TensorType('float64', ib)('x') x = self.type('float64', ib)('x')
self.assertRaises(ValueError, DimShuffle, ib, shuffle) self.assertRaises(ValueError, DimShuffle, ib, shuffle)
def test_perform(self): def test_perform(self):
...@@ -89,7 +90,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -89,7 +90,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
((1, 1, 1), ()), ((1, 1, 1), ()),
((1,), ('x', 'x'))]: ((1,), ('x', 'x'))]:
ib = [(entry == 1) for entry in xsh] ib = [(entry == 1) for entry in xsh]
adtens = TensorType('float64', ib)('x') adtens = self.type('float64', ib)('x')
adtens_val = numpy.ones(xsh) adtens_val = numpy.ones(xsh)
self._compile_and_check([adtens], self._compile_and_check([adtens],
[self.op(ib, shuffle)(adtens)], [self.op(ib, shuffle)(adtens)],
...@@ -97,7 +98,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -97,7 +98,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
warn=False) warn=False)
def test_too_big_rank(self): def test_too_big_rank(self):
x = tensor.dscalar() x = self.type('float64', broadcastable=())()
y = x.dimshuffle(('x',) * (numpy.MAXDIMS + 1)) y = x.dimshuffle(('x',) * (numpy.MAXDIMS + 1))
self.assertRaises(ValueError, y.eval, {x: 0}) self.assertRaises(ValueError, y.eval, {x: 0})
...@@ -328,6 +329,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -328,6 +329,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
((), None), ((), None),
((), ()) ((), ())
] ]
type = TensorType
def with_linker(self, linker, scalar_op=scalar.add, dtype="floatX", def with_linker(self, linker, scalar_op=scalar.add, dtype="floatX",
pre_scalar_op=None, pre_scalar_op=None,
...@@ -335,7 +337,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -335,7 +337,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
for xsh, tosum in self.cases: for xsh, tosum in self.cases:
if dtype == "floatX": if dtype == "floatX":
dtype = theano.config.floatX dtype = theano.config.floatX
x = TensorType(dtype, [(entry == 1) for entry in xsh])('x') x = self.type(dtype, [(entry == 1) for entry in xsh])('x')
d = {} d = {}
if pre_scalar_op is not None: if pre_scalar_op is not None:
d = {"pre_scalar_op": pre_scalar_op} d = {"pre_scalar_op": pre_scalar_op}
...@@ -438,7 +440,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -438,7 +440,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
if test_nan: if test_nan:
try: try:
self.assertTrue( self.assertTrue(
theano.tensor.TensorType.values_eq(f(xv), zv), self.type.values_eq(f(xv), zv),
(f(xv), zv)) (f(xv), zv))
except NotImplementedError: except NotImplementedError:
# GpuCAReduce don't implement all cases when size is 0 # GpuCAReduce don't implement all cases when size is 0
...@@ -453,7 +455,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -453,7 +455,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
# 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
x = TensorType(dtype, [(entry == 1) for entry in xsh])('x') x = self.type(dtype, [(entry == 1) for entry in xsh])('x')
if tensor_op is None: if tensor_op is None:
e = self.op(scalar_op, axis=tosum)(x) e = self.op(scalar_op, axis=tosum)(x)
else: else:
...@@ -538,7 +540,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -538,7 +540,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
for xsh, tosum in self.cases: for xsh, tosum in self.cases:
x = TensorType(dtype, [(entry == 1) for entry in xsh])('x') x = self.type(dtype, [(entry == 1) for entry in xsh])('x')
if pre_scalar_op is not None: if pre_scalar_op is not None:
x = pre_scalar_op(x) x = pre_scalar_op(x)
if tosum is None: if tosum is None:
......
...@@ -49,6 +49,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -49,6 +49,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
adv_incsub1=tensor.AdvancedIncSubtensor1, adv_incsub1=tensor.AdvancedIncSubtensor1,
mode=None, mode=None,
dtype=theano.config.floatX, dtype=theano.config.floatX,
type=tensor.TensorType,
ignore_topo=DeepCopyOp): ignore_topo=DeepCopyOp):
self.shared = shared self.shared = shared
self.sub = sub self.sub = sub
...@@ -59,6 +60,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -59,6 +60,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
mode = theano.compile.mode.get_default_mode() mode = theano.compile.mode.get_default_mode()
self.mode = mode self.mode = mode
self.dtype = dtype self.dtype = dtype
self.type = type
self.ignore_topo = ignore_topo self.ignore_topo = ignore_topo
self.fast_compile = theano.config.mode == 'FAST_COMPILE' self.fast_compile = theano.config.mode == 'FAST_COMPILE'
self.ops = (sub, inc_sub, adv_sub1, adv_incsub1) self.ops = (sub, inc_sub, adv_sub1, adv_incsub1)
...@@ -88,8 +90,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -88,8 +90,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
Subtensor.debug = False Subtensor.debug = False
utt.seed_rng() utt.seed_rng()
def eval_output_and_check(self, t, list=False): def eval_output_and_check(self, t, list=False, mode=None):
f = inplace_func([], t, mode=self.mode) if mode is None:
mode = self.mode
f = inplace_func([], t, mode=mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
topo_ = [node for node in topo if not isinstance(node.op, topo_ = [node for node in topo if not isinstance(node.op,
self.ignore_topo)] self.ignore_topo)]
...@@ -167,12 +171,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -167,12 +171,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
n = self.shared(numpy.ones((), dtype=self.dtype)) n = self.shared(numpy.ones((), dtype=self.dtype))
t = self.sub([])(n) t = self.sub([])(n)
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
mode = self.mode self.eval_output_and_check(
self.mode = mode.excluding("local_useless_subtensor") t, mode=self.mode.excluding("local_useless_subtensor"))
try:
self.eval_output_and_check(t)
finally:
self.mode = mode
def test1_err_invalid(self): def test1_err_invalid(self):
n = self.shared(numpy.ones(1, dtype=self.dtype)) n = self.shared(numpy.ones(1, dtype=self.dtype))
...@@ -885,16 +885,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -885,16 +885,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
Test increment and set with broadcast Test increment and set with broadcast
""" """
X = tensor.matrix(dtype=self.dtype) X = self.shared(numpy.ones((9, 9)).astype(self.dtype))
y = set_subtensor(X[1::, 1::], 0) y = set_subtensor(X[1::, 1::], 0)
f = self.function([X], [y], f = self.function([], [y],
op=self.inc_sub, op=self.inc_sub,
N=1) N=1)
out = f()
x_ = numpy.ones((9, 9)) res = numpy.ones((9, 9))
out = f(x_.astype('float32'))
res = x_.copy()
res[1::, 1::] = 0 res[1::, 1::] = 0
assert numpy.allclose(out, res) assert numpy.allclose(out, res)
...@@ -925,9 +923,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -925,9 +923,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# Symbolic variable to be incremented. # Symbolic variable to be incremented.
# We create a new one every time in order not to # We create a new one every time in order not to
# have duplicated variables in the function's inputs # have duplicated variables in the function's inputs
data_var = tensor.tensor( data_var = self.type(
broadcastable=[False] * data_n_dims, broadcastable=[False] * data_n_dims,
dtype=self.dtype) dtype=self.dtype)()
# Symbolic variable with rows to be incremented. # Symbolic variable with rows to be incremented.
idx_var = theano.tensor.vector(dtype='int64') idx_var = theano.tensor.vector(dtype='int64')
n_to_inc = rng.randint(data_shape[0]) n_to_inc = rng.randint(data_shape[0])
...@@ -935,9 +933,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -935,9 +933,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
idx_num = rng.randint(0, data_shape[0], n_to_inc) idx_num = rng.randint(0, data_shape[0], n_to_inc)
idx_num = idx_num.astype('int64') idx_num = idx_num.astype('int64')
# Symbolic variable with increment value. # Symbolic variable with increment value.
inc_var = tensor.tensor( inc_var = self.type(
broadcastable=[False] * inc_n_dims, broadcastable=[False] * inc_n_dims,
dtype=self.dtype) dtype=self.dtype)()
# Trick for the case where `inc_shape` is the same as # Trick for the case where `inc_shape` is the same as
# `data_shape`: what we actually want is the first # `data_shape`: what we actually want is the first
# shape element to be equal to the number of rows to # shape element to be equal to the number of rows to
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论