提交 e836df32 authored 作者: Li Yao's avatar Li Yao

ensure sorted indices tested

上级 9109e410
...@@ -273,7 +273,9 @@ class EnsureSortedIndices(Op): ...@@ -273,7 +273,9 @@ class EnsureSortedIndices(Op):
def grad(self, (x,), (gz,)): def grad(self, (x,), (gz,)):
return [gz] return [gz]
def infer_shape(self, node, i0_shapes):
return i0_shapes
def __str__(self): def __str__(self):
if self.inplace: if self.inplace:
return self.__class__.__name__ + "{inplace}" return self.__class__.__name__ + "{inplace}"
......
...@@ -16,7 +16,7 @@ from theano import function, tensor ...@@ -16,7 +16,7 @@ from theano import function, tensor
import theano import theano
from theano.sparse.sandbox import sp from theano.sparse.sandbox import sp
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
from theano.sparse.tests.test_basic import random_lil
class TestSP(unittest.TestCase): class TestSP(unittest.TestCase):
def test_convolution(self): def test_convolution(self):
...@@ -414,32 +414,25 @@ def test_diagonal(): ...@@ -414,32 +414,25 @@ def test_diagonal():
assert numpy.all(n == f(range(K)).toarray()) assert numpy.all(n == f(range(K)).toarray())
def test_EnsureSortedIndices(): def test_EnsureSortedIndices():
x = 2000
input_tensor = theano.sparse.csc_dmatrix() y = 2000
sort_op = sp.ensure_sorted_indices(input_tensor) sparsity = 1000
f = theano.function([input_tensor], sort_op) for i in range(2):
# testing both csc and csr
# test_sample = scipy.sparse.rand(1000, 1000, density=0.01) if i is 0:
# the function above is not working since scipy has lower version # csc
# specify the number of rows and columns in the test case input_tensor = theano.sparse.csc_dmatrix()
r = 2000 sample = scipy.sparse.csc_matrix(random_lil((x,y),'float64',sparsity))
c = 2000 else:
# construct a test sample # csr
test_sample = numpy.random.rand(r,c) input_tensor = theano.sparse.csr_dmatrix()
indices_zero_x = numpy.random.permutation(range(r)) sample = scipy.sparse.csr_matrix(random_lil((x,y),'float64',sparsity))
indices_zero_y = numpy.random.permutation(range(c))
# take 70% of the test_sample and make them 0 sort_op = sp.ensure_sorted_indices(input_tensor)
sparse_ratio_x = int(r*0.9) f = theano.function([input_tensor], sort_op)
sparse_ratio_y = int(c*0.9) sorted_scipy = sample.sorted_indices()
for i in indices_zero_x[:sparse_ratio_x]: sorted_theano = f(sample)
for j in indices_zero_y[:sparse_ratio_y]: assert numpy.all(sorted_theano.todense() == sorted_scipy.todense())
test_sample[i,j] = 0
# sort test sample by scipy
test_sample = scipy.sparse.csc_matrix(test_sample)
sorted_for_real = test_sample.sorted_indices()
sorted_theano = f(test_sample)
assert numpy.all(sorted_theano.todense() == sorted_for_real.todense())
def test_diagonal_grad(): def test_diagonal_grad():
def d(x): def d(x):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论