提交 c3227a52 authored 作者: Nicolas Bouchard's avatar Nicolas Bouchard

Rewrite CastTester.

上级 b68c23d6
...@@ -1934,85 +1934,66 @@ class Test_getitem(unittest.TestCase): ...@@ -1934,85 +1934,66 @@ class Test_getitem(unittest.TestCase):
assert numpy.all(t1 == r1) assert numpy.all(t1 == r1)
class TestCast(utt.InferShapeTester): class CastTester(utt.InferShapeTester):
compatible_types = (tensor.int_dtypes +
tensor.continuous_dtypes)
x_csc = [theano.sparse.csc_matrix(dtype=t) for t in compatible_types]
x_csr = [theano.sparse.csr_matrix(dtype=t) for t in compatible_types]
indptr = numpy.array([0, 2, 3, 6])
indices = numpy.array([0, 2, 2, 0, 1, 2])
data = numpy.array([1, 2, 3, 4, 5, 6])
properties = (data, indices, indptr)
def setUp(self): def setUp(self):
super(TestCast, self).setUp() super(CastTester, self).setUp()
self.op_class = Cast
def test_cast(self): def test_cast(self):
cast_csc = dict([ for format in sparse.sparse_formats:
(x, [theano.function([x], x.astype(t)) for i_dtype in sparse.all_dtypes:
for t in self.compatible_types]) for o_dtype in sparse.all_dtypes:
for x in self.x_csc]) (variable, ), (data, ) = sparse_random_inputs(
format,
cast_csr = dict([ shape=(4, 7),
(x, [theano.function([x], Cast(t)(x)) out_dtype=i_dtype)
for t in self.compatible_types])
for x in self.x_csr])
cast_csr_func = dict([
(x, [theano.function([x], cast(x, t))
for t in self.compatible_types])
for x in self.x_csr])
for x in self.x_csc:
for f, t in zip(cast_csc[x], self.compatible_types):
a = sp.csc_matrix(self.properties, dtype=x.dtype).copy()
assert f(a).dtype == t
for x in self.x_csr:
for f, t in zip(cast_csr[x], self.compatible_types):
a = sp.csr_matrix(self.properties, dtype=x.dtype)
assert f(a).dtype == t
for x in self.x_csr:
for f, t in zip(cast_csr_func[x], self.compatible_types):
a = sp.csr_matrix(self.properties, dtype=x.dtype)
assert f(a).dtype == t
def test_infer_shape(self): func = theano.function([variable], cast(variable, o_dtype))
for x in self.x_csc: cls = theano.function([variable], Cast(o_dtype)(variable))
for t in self.compatible_types: prop = theano.function([variable], variable.astype(o_dtype))
a = sp.csc_matrix(self.properties, dtype=x.dtype)
self._compile_and_check([x],
[Cast(t)(x)],
[a],
self.op_class)
for x in self.x_csr: t_func, t_cls, t_prop = func(data), cls(data), prop(data)
for t in self.compatible_types:
a = sp.csr_matrix(self.properties, dtype=x.dtype) expected = data.toarray().astype(o_dtype)
self._compile_and_check([x],
[Cast(t)(x)], assert t_func.format == format
[a], assert t_cls.format == format
self.op_class) assert t_prop.format == format
t_func = t_func.toarray()
t_cls = t_cls.toarray()
t_prop = t_prop.toarray()
assert numpy.allclose(t_func, expected)
assert numpy.allclose(t_cls, expected)
assert numpy.allclose(t_prop, expected)
def test_infer_shape(self):
for format in sparse.sparse_formats:
for i_dtype in sparse.all_dtypes:
for o_dtype in sparse.all_dtypes:
variable, data = sparse_random_inputs(
format,
shape=(4, 7),
out_dtype=i_dtype)
self._compile_and_check(variable,
[Cast(o_dtype)(*variable)],
data,
Cast)
def test_grad(self): def test_grad(self):
for dtype in tensor.float_dtypes: for format in sparse.sparse_formats:
for t in tensor.float_dtypes: for i_dtype in sparse.float_dtypes:
eps = None for o_dtype in tensor.float_dtypes:
if t == 'float32': _, data = sparse_random_inputs(
eps = 7e-4 format,
a = sp.csc_matrix(self.properties, dtype=dtype) shape=(4, 7),
verify_grad_sparse(Cast(t), [a], eps=eps) out_dtype=i_dtype)
for dtype in tensor.float_dtypes: eps = None
for t in tensor.float_dtypes: if o_dtype == 'float32':
eps = None eps = 7e-4
if t == 'float32':
eps = 7e-4 verify_grad_sparse(Cast(o_dtype), data, eps=eps)
a = sp.csr_matrix(self.properties, dtype=dtype)
verify_grad_sparse(Cast(t), [a], eps=eps)
class _HVStackTester(utt.InferShapeTester): class _HVStackTester(utt.InferShapeTester):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论