提交 441d5a7f authored 作者: nouiz's avatar nouiz

Merge pull request #853 from bouchnic/cast

NEWS: Add astype to SparseVariable (Nicolas B.)
......@@ -267,6 +267,9 @@ class _sparse_py_operators:
T = property(lambda self: transpose(self),
doc="Return aliased transpose of self (read-only)")
def astype(self, dtype):
return cast(self, dtype)
def __neg__(self):
return neg(self)
......@@ -1031,6 +1034,9 @@ ccast = Cast('complex64')
zcast = Cast('complex128')
def cast(variable, dtype):
return Cast(dtype)(variable)
#
# Conversion
#
......
......@@ -33,7 +33,7 @@ from theano.sparse import (
csc_from_dense, csr_from_dense, dense_from_sparse,
Dot, Usmm, UsmmCscDense, sp_ones_like, GetItemScalar,
SparseFromDense,
Cast, HStack, VStack, AddSSData, add_s_s_data,
Cast, cast, HStack, VStack, AddSSData, add_s_s_data,
Poisson, poisson, Binomial, Multinomial, multinomial,
structured_sigmoid, structured_exp, structured_log,
structured_pow, structured_minimum, structured_maximum, structured_add,
......@@ -1934,75 +1934,66 @@ class Test_getitem(unittest.TestCase):
assert numpy.all(t1 == r1)
class TestCast(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)
class CastTester(utt.InferShapeTester):
def setUp(self):
super(TestCast, self).setUp()
self.op_class = Cast
super(CastTester, self).setUp()
def test_cast(self):
cast_csc = dict([
(x, [theano.function([x], Cast(t)(x))
for t in self.compatible_types])
for x in self.x_csc])
cast_csr = dict([
(x, [theano.function([x], Cast(t)(x))
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 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)
def test_infer_shape(self):
for x in self.x_csc:
for t in self.compatible_types:
a = sp.csc_matrix(self.properties, dtype=x.dtype)
self._compile_and_check([x],
[Cast(t)(x)],
[a],
self.op_class)
func = theano.function([variable], cast(variable, o_dtype))
cls = theano.function([variable], Cast(o_dtype)(variable))
prop = theano.function([variable], variable.astype(o_dtype))
for x in self.x_csr:
for t in self.compatible_types:
a = sp.csr_matrix(self.properties, dtype=x.dtype)
self._compile_and_check([x],
[Cast(t)(x)],
[a],
self.op_class)
t_func, t_cls, t_prop = func(data), cls(data), prop(data)
expected = data.toarray().astype(o_dtype)
assert t_func.format == format
assert t_cls.format == format
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):
for dtype in tensor.float_dtypes:
for t in tensor.float_dtypes:
eps = None
if t == 'float32':
eps = 7e-4
a = sp.csc_matrix(self.properties, dtype=dtype)
verify_grad_sparse(Cast(t), [a], eps=eps)
for dtype in tensor.float_dtypes:
for t in tensor.float_dtypes:
eps = None
if t == 'float32':
eps = 7e-4
a = sp.csr_matrix(self.properties, dtype=dtype)
verify_grad_sparse(Cast(t), [a], eps=eps)
for format in sparse.sparse_formats:
for i_dtype in sparse.float_dtypes:
for o_dtype in tensor.float_dtypes:
_, data = sparse_random_inputs(
format,
shape=(4, 7),
out_dtype=i_dtype)
eps = None
if o_dtype == 'float32':
eps = 7e-4
verify_grad_sparse(Cast(o_dtype), data, eps=eps)
class _HVStackTester(utt.InferShapeTester):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论