提交 8d083c1a authored 作者: Frederic's avatar Frederic

Add paramter unsorted_indices and explicit_zero to sparse_random_inputs()

This help test those cases.
上级 fd8d4d32
...@@ -80,7 +80,8 @@ def random_lil(shape, dtype, nnz): ...@@ -80,7 +80,8 @@ def random_lil(shape, dtype, nnz):
return rval return rval
def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None): def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None,
explicit_zero=False, unsorted_indices=False):
"""Return a tuple containing everything needed to """Return a tuple containing everything needed to
perform a test. perform a test.
...@@ -97,9 +98,15 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None): ...@@ -97,9 +98,15 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None):
max, when `gap` = (`a`, `b`) it provide a sample max, when `gap` = (`a`, `b`) it provide a sample
from [a, b[. If `None` is used, it provide [0, 1] from [a, b[. If `None` is used, it provide [0, 1]
for float dtypes and [0, 50[ for integer dtypes. for float dtypes and [0, 50[ for integer dtypes.
:param explicit_zero: When True, we add explicit zero in the
returned sparse matrix
:param unsorted_indices: when True, we make sure there is
unsorted indices in the returned
sparse matrix.
:return: (variable, data) where both `variable` :return: (variable, data) where both `variable`
and `data` are list. and `data` are list.
:note: explicit_zero and unsorted_indices was added in Theano 0.6rc4
""" """
if out_dtype is None: if out_dtype is None:
...@@ -136,6 +143,19 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None): ...@@ -136,6 +143,19 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None):
for k in range(n)] for k in range(n)]
data = [getattr(scipy.sparse, format + '_matrix')(_rand(), dtype=out_dtype) data = [getattr(scipy.sparse, format + '_matrix')(_rand(), dtype=out_dtype)
for k in range(n)] for k in range(n)]
if unsorted_indices:
for idx in range(n):
d = data[idx]
d = d[range(d.shape[0])]
assert not d.has_sorted_indices
data[idx] = d
if explicit_zero:
for idx in range(n):
assert data[idx].nnz > 1, (
"can't make a sparse matrix with explicit 0")
d_idx = numpy.random.randint(data[idx].nnz)
data[idx].data[d_idx] = 0
#numpy 1.5.0 with scipy 0.9.0 have scipy.sparse.XXX_matrix return #numpy 1.5.0 with scipy 0.9.0 have scipy.sparse.XXX_matrix return
#typenum 10(ulonglong) instead of 8(uint64) event if they are the same! #typenum 10(ulonglong) instead of 8(uint64) event if they are the same!
#Theano don't like ulonglong type_num #Theano don't like ulonglong type_num
...@@ -1845,17 +1865,11 @@ class Remove0Tester(utt.InferShapeTester): ...@@ -1845,17 +1865,11 @@ class Remove0Tester(utt.InferShapeTester):
('csr', scipy.sparse.csr_matrix), ] ('csr', scipy.sparse.csr_matrix), ]
for format, matrix_class in configs: for format, matrix_class in configs:
# real (x,), (mat,) = sparse_random_inputs(format, (3, 4),
origin = (numpy.arange(9) + 1).reshape((3, 3)) out_dtype=config.floatX,
origin.astype(config.floatX) explicit_zero=True)
mat = matrix_class(origin).astype(theano.config.floatX) assert 0 in mat.data
mat[0, 1] = mat[1, 0] = mat[2, 2] = 0
assert mat.size == 9
# symbolic
x = theano.sparse.SparseType(format=format, dtype=config.floatX)()
# the In thingy has to be there because theano has as rule not # the In thingy has to be there because theano has as rule not
# to optimize inputs # to optimize inputs
f = theano.function([theano.In(x, borrow=True, mutable=True)], f = theano.function([theano.In(x, borrow=True, mutable=True)],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论