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

ensure sorted indices tested

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