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