提交 f79c3b87 authored 作者: Frederic's avatar Frederic

pep8 fix.

上级 300db3d0
...@@ -7,7 +7,7 @@ try: ...@@ -7,7 +7,7 @@ try:
import scipy.sparse as sp import scipy.sparse as sp
import scipy.sparse import scipy.sparse
except ImportError: except ImportError:
pass#the variable enable_sparse will be used to disable the test file. pass # The variable enable_sparse will be used to disable the test file.
import theano import theano
from theano import compile, config from theano import compile, config
...@@ -17,9 +17,10 @@ from theano.gof.python25 import all, product ...@@ -17,9 +17,10 @@ from theano.gof.python25 import all, product
if enable_sparse == False: if enable_sparse == False:
raise SkipTest('Optional package sparse disabled') raise SkipTest('Optional package sparse disabled')
from theano.sparse.basic import _is_dense, _is_sparse, _is_dense_variable, _is_sparse_variable from theano.sparse.basic import _is_dense, _is_sparse, _mtypes
from theano.sparse.basic import _mtypes from theano.sparse.basic import _is_dense_variable, _is_sparse_variable
from theano.sparse import as_sparse_variable, CSC, CSR, CSM, CSMProperties, SparseType, StructuredDotCSC from theano.sparse import as_sparse_variable, CSC, CSR, CSM, CSMProperties
from theano.sparse import SparseType, StructuredDotCSC
from theano.sparse import add, mul, structured_dot, transpose from theano.sparse import add, mul, structured_dot, transpose
from theano.sparse import csc_from_dense, csr_from_dense, dense_from_sparse from theano.sparse import csc_from_dense, csr_from_dense, dense_from_sparse
from theano.sparse import Dot, Usmm, UsmmCscDense from theano.sparse import Dot, Usmm, UsmmCscDense
...@@ -41,30 +42,32 @@ def as_sparse_format(data, format): ...@@ -41,30 +42,32 @@ def as_sparse_format(data, format):
def eval_outputs(outputs): def eval_outputs(outputs):
return compile.function([], outputs)()[0] return compile.function([], outputs)()[0]
def random_lil(shape, dtype, nnz): def random_lil(shape, dtype, nnz):
rval = sp.lil_matrix(shape, dtype=dtype) rval = sp.lil_matrix(shape, dtype=dtype)
huge = 2**30 huge = 2 ** 30
for k in range(nnz): for k in range(nnz):
# set non-zeros in random locations (row x, col y) # set non-zeros in random locations (row x, col y)
idx = numpy.random.random_integers(huge,size=len(shape)) % shape idx = numpy.random.random_integers(huge, size=len(shape)) % shape
value = numpy.random.rand() value = numpy.random.rand()
#if dtype *int*, value will always be zeros! #if dtype *int*, value will always be zeros!
if "int" in dtype: if "int" in dtype:
value = int(value*100) value = int(value * 100)
rval.__setitem__( rval.__setitem__(
idx, idx,
value) value)
return rval return rval
class T_transpose(unittest.TestCase): class T_transpose(unittest.TestCase):
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
def test_transpose_csc(self): def test_transpose_csc(self):
sp = scipy.sparse.csc_matrix(scipy.sparse.eye(5,3)) sp = scipy.sparse.csc_matrix(scipy.sparse.eye(5, 3))
a = as_sparse_variable(sp) a = as_sparse_variable(sp)
self.assertFalse(a.data is sp) self.assertFalse(a.data is sp)
self.assertTrue(a.data.shape == (5,3)) self.assertTrue(a.data.shape == (5, 3))
self.assertTrue(a.type.dtype == 'float64', a.type.dtype) self.assertTrue(a.type.dtype == 'float64', a.type.dtype)
self.assertTrue(a.type.format == 'csc', a.type.format) self.assertTrue(a.type.format == 'csc', a.type.format)
ta = transpose(a) ta = transpose(a)
...@@ -72,10 +75,11 @@ class T_transpose(unittest.TestCase): ...@@ -72,10 +75,11 @@ class T_transpose(unittest.TestCase):
self.assertTrue(ta.type.format == 'csr', ta.type.format) self.assertTrue(ta.type.format == 'csr', ta.type.format)
vta = eval_outputs([ta]) vta = eval_outputs([ta])
self.assertTrue(vta.shape == (3,5)) self.assertTrue(vta.shape == (3, 5))
def test_transpose_csr(self): def test_transpose_csr(self):
a = as_sparse_variable(scipy.sparse.csr_matrix(scipy.sparse.eye(5,3))) a = as_sparse_variable(scipy.sparse.csr_matrix(scipy.sparse.eye(5, 3)))
self.assertTrue(a.data.shape == (5,3)) self.assertTrue(a.data.shape == (5, 3))
self.assertTrue(a.type.dtype == 'float64') self.assertTrue(a.type.dtype == 'float64')
self.assertTrue(a.type.format == 'csr') self.assertTrue(a.type.format == 'csr')
ta = transpose(a) ta = transpose(a)
...@@ -83,13 +87,16 @@ class T_transpose(unittest.TestCase): ...@@ -83,13 +87,16 @@ class T_transpose(unittest.TestCase):
self.assertTrue(ta.type.format == 'csc', ta.type.format) self.assertTrue(ta.type.format == 'csc', ta.type.format)
vta = eval_outputs([ta]) vta = eval_outputs([ta])
self.assertTrue(vta.shape == (3,5)) self.assertTrue(vta.shape == (3, 5))
class T_AddMul(unittest.TestCase): class T_AddMul(unittest.TestCase):
def testAddSS(self): def testAddSS(self):
self._testSS(add) self._testSS(add)
def testAddSD(self): def testAddSD(self):
self._testSD(add) self._testSD(add)
def testAddDS(self): def testAddDS(self):
self._testDS(add) self._testDS(add)
...@@ -97,17 +104,19 @@ class T_AddMul(unittest.TestCase): ...@@ -97,17 +104,19 @@ class T_AddMul(unittest.TestCase):
self._testSS(mul, self._testSS(mul,
numpy.array([[1., 0], [3, 0], [0, 6]]), numpy.array([[1., 0], [3, 0], [0, 6]]),
numpy.array([[1., 0], [3, 0], [0, 6]])) numpy.array([[1., 0], [3, 0], [0, 6]]))
def testMulSD(self): def testMulSD(self):
self._testSD(mul, self._testSD(mul,
numpy.array([[1., 0], [3, 0], [0, 6]]), numpy.array([[1., 0], [3, 0], [0, 6]]),
numpy.array([[1., 0], [3, 0], [0, 6]])) numpy.array([[1., 0], [3, 0], [0, 6]]))
def testMulDS(self): def testMulDS(self):
self._testDS(mul, self._testDS(mul,
numpy.array([[1., 0], [3, 0], [0, 6]]), numpy.array([[1., 0], [3, 0], [0, 6]]),
numpy.array([[1., 0], [3, 0], [0, 6]])) numpy.array([[1., 0], [3, 0], [0, 6]]))
def _testSS(self, op, array1 = numpy.array([[1., 0], [3, 0], [0, 6]]), def _testSS(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]),
array2 = numpy.asarray([[0, 2.], [0, 4], [5, 0]])): array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])):
for mtype in _mtypes: for mtype in _mtypes:
a = mtype(array1) a = mtype(array1)
aR = as_sparse_variable(a) aR = as_sparse_variable(a)
...@@ -130,26 +139,29 @@ class T_AddMul(unittest.TestCase): ...@@ -130,26 +139,29 @@ class T_AddMul(unittest.TestCase):
self.assertTrue(apb.type.format == bR.type.format, apb.type.format) self.assertTrue(apb.type.format == bR.type.format, apb.type.format)
val = eval_outputs([apb]) val = eval_outputs([apb])
self.assertTrue(val.shape == (3,2)) self.assertTrue(val.shape == (3, 2))
if op is add: if op is add:
self.assertTrue(numpy.all(val.todense() == (a + b).todense())) self.assertTrue(numpy.all(val.todense() == (a + b).todense()))
self.assertTrue(numpy.all(val.todense() == numpy.array([[1., 2], [3, 4], [5, 6]]))) ans = numpy.array([[1., 2], [3, 4], [5, 6]])
self.assertTrue(numpy.all(val.todense() == ans))
elif op is mul: elif op is mul:
self.assertTrue(numpy.all(val.todense() == (a.multiply(b)).todense())) self.assertTrue(numpy.all(val.todense()
self.assertTrue(numpy.all(val.todense() == numpy.array([[1, 0], [9, 0], [0, 36]]))) == (a.multiply(b)).todense()))
ans = numpy.array([[1, 0], [9, 0], [0, 36]])
self.assertTrue(numpy.all(val.todense() == ans))
def _testSD(self, op, array1 = numpy.array([[1., 0], [3, 0], [0, 6]]), def _testSD(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]),
array2 = numpy.asarray([[0, 2.], [0, 4], [5, 0]])): array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])):
for mtype in _mtypes: for mtype in _mtypes:
a = numpy.array(array1) a = numpy.array(array1)
aR = tensor.as_tensor_variable(a) aR = tensor.as_tensor_variable(a)
self.assertFalse(aR.data is a) #constants are copied self.assertFalse(aR.data is a) # constants are copied
self.assertTrue(_is_dense(a)) self.assertTrue(_is_dense(a))
self.assertTrue(_is_dense_variable(aR)) self.assertTrue(_is_dense_variable(aR))
b = mtype(array2) b = mtype(array2)
bR = as_sparse_variable(b) bR = as_sparse_variable(b)
self.assertFalse(bR.data is b) #constants are copied self.assertFalse(bR.data is b) # constants are copied
self.assertTrue(_is_sparse(b)) self.assertTrue(_is_sparse(b))
self.assertTrue(_is_sparse_variable(bR)) self.assertTrue(_is_sparse_variable(bR))
...@@ -163,15 +175,16 @@ class T_AddMul(unittest.TestCase): ...@@ -163,15 +175,16 @@ class T_AddMul(unittest.TestCase):
if op is add: if op is add:
self.assertTrue(_is_dense_variable(apb)) self.assertTrue(_is_dense_variable(apb))
self.assertTrue(numpy.all(val == (a + b))) self.assertTrue(numpy.all(val == (a + b)))
self.assertTrue(numpy.all(val == numpy.array([[1., 2], [3, 4], [5, 6]]))) ans = numpy.array([[1., 2], [3, 4], [5, 6]])
self.assertTrue(numpy.all(val == ans))
elif op is mul: elif op is mul:
self.assertTrue(_is_sparse_variable(apb)) self.assertTrue(_is_sparse_variable(apb))
self.assertTrue(numpy.all(val.todense() == (b.multiply(a)))) self.assertTrue(numpy.all(val.todense() == (b.multiply(a))))
self.assertTrue(numpy.all(val.todense() == numpy.array([[1, 0], self.assertTrue(numpy.all(val.todense() == numpy.array([[1, 0],
[9, 0], [0, 36]]))) [9, 0], [0, 36]])))
def _testDS(self, op, array1 = numpy.array([[1., 0], [3, 0], [0, 6]]), def _testDS(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]),
array2 = numpy.asarray([[0, 2.], [0, 4], [5, 0]])): array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])):
for mtype in _mtypes: for mtype in _mtypes:
a = mtype(array1) a = mtype(array1)
aR = as_sparse_variable(a) aR = as_sparse_variable(a)
...@@ -195,12 +208,13 @@ class T_AddMul(unittest.TestCase): ...@@ -195,12 +208,13 @@ class T_AddMul(unittest.TestCase):
if op is add: if op is add:
self.assertTrue(_is_dense_variable(apb)) self.assertTrue(_is_dense_variable(apb))
self.assertTrue(numpy.all(val == (a + b))) self.assertTrue(numpy.all(val == (a + b)))
self.assertTrue(numpy.all(val == numpy.array([[1., 2], [3, 4], [5, 6]]))) ans = numpy.array([[1., 2], [3, 4], [5, 6]])
self.assertTrue(numpy.all(val == ans))
elif op is mul: elif op is mul:
self.assertTrue(_is_sparse_variable(apb)) self.assertTrue(_is_sparse_variable(apb))
ans = numpy.array([[1, 0], [9, 0], [0, 36]])
self.assertTrue(numpy.all(val.todense() == (a.multiply(b)))) self.assertTrue(numpy.all(val.todense() == (a.multiply(b))))
self.assertTrue(numpy.all(val.todense() == numpy.array([[1, 0], self.assertTrue(numpy.all(val.todense() == ans))
[9, 0], [0, 36]])))
def test_upcast(self): def test_upcast(self):
array1 = numpy.array([[1, 0], [3, 0], [0, 6]], dtype='float32') array1 = numpy.array([[1, 0], [3, 0], [0, 6]], dtype='float32')
...@@ -279,7 +293,7 @@ class T_conversion(unittest.TestCase): ...@@ -279,7 +293,7 @@ class T_conversion(unittest.TestCase):
a = tensor.as_tensor_variable(numpy.random.rand(5)) a = tensor.as_tensor_variable(numpy.random.rand(5))
s = csc_from_dense(a) s = csc_from_dense(a)
val = eval_outputs([s]) val = eval_outputs([s])
self.assertTrue(str(val.dtype)=='float64') self.assertTrue(str(val.dtype) == 'float64')
self.assertTrue(val.format == 'csc') self.assertTrue(val.format == 'csc')
if 0: if 0:
...@@ -287,7 +301,7 @@ class T_conversion(unittest.TestCase): ...@@ -287,7 +301,7 @@ class T_conversion(unittest.TestCase):
a = tensor.as_tensor_variable(numpy.random.rand(5)) a = tensor.as_tensor_variable(numpy.random.rand(5))
s = csr_from_dense(a) s = csr_from_dense(a)
val = eval_outputs([s]) val = eval_outputs([s])
self.assertTrue(str(val.dtype)=='float64') self.assertTrue(str(val.dtype) == 'float64')
self.assertTrue(val.format == 'csr') self.assertTrue(val.format == 'csr')
if 1: if 1:
...@@ -297,25 +311,27 @@ class T_conversion(unittest.TestCase): ...@@ -297,25 +311,27 @@ class T_conversion(unittest.TestCase):
s = t(scipy.sparse.identity(5)) s = t(scipy.sparse.identity(5))
d = dense_from_sparse(s) d = dense_from_sparse(s)
# s should be copied into the graph as a constant # s should be copied into the graph as a constant
s[0,0] = 3.0 # changes s, but not the copy s[0, 0] = 3.0 # changes s, but not the copy
val = eval_outputs([d]) val = eval_outputs([d])
return return
self.assertTrue(str(val.dtype)==s.dtype) self.assertTrue(str(val.dtype) == s.dtype)
self.assertTrue(numpy.all(val[0] == [1,0,0,0,0])) self.assertTrue(numpy.all(val[0] == [1, 0, 0, 0, 0]))
class test_structureddot(unittest.TestCase): class test_structureddot(unittest.TestCase):
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
def test_structureddot_csc_grad(self): def test_structureddot_csc_grad(self):
#shortcut: testing csc in float32, testing csr in float64 #shortcut: testing csc in float32, testing csr in float64
# allocate a random sparse matrix # allocate a random sparse matrix
spmat = sp.csc_matrix(random_lil((4,3), 'float32', 3)) spmat = sp.csc_matrix(random_lil((4, 3), 'float32', 3))
mat = numpy.asarray(numpy.random.randn(3,2), 'float32') mat = numpy.asarray(numpy.random.randn(3, 2), 'float32')
def buildgraphCSC(spdata,sym_mat): def buildgraphCSC(spdata, sym_mat):
csc = CSC(spdata, spmat.indices[:spmat.size], csc = CSC(spdata, spmat.indices[:spmat.size],
spmat.indptr, spmat.shape) spmat.indptr, spmat.shape)
assert csc.type.dtype == 'float32' assert csc.type.dtype == 'float32'
...@@ -331,11 +347,11 @@ class test_structureddot(unittest.TestCase): ...@@ -331,11 +347,11 @@ class test_structureddot(unittest.TestCase):
#shortcut: testing csc in float32, testing csr in float64 #shortcut: testing csc in float32, testing csr in float64
# allocate a random sparse matrix # allocate a random sparse matrix
spmat = sp.csr_matrix(random_lil((4,3), 'float64', 3)) spmat = sp.csr_matrix(random_lil((4, 3), 'float64', 3))
mat = numpy.asarray(numpy.random.randn(3,2), 'float64') mat = numpy.asarray(numpy.random.randn(3, 2), 'float64')
def buildgraph(spdata,sym_mat): def buildgraph(spdata, sym_mat):
csr = CSR(spdata, spmat.indices[:spmat.size], csr = CSR(spdata, spmat.indices[:spmat.size],
spmat.indptr, spmat.shape) spmat.indptr, spmat.shape)
assert csr.type.dtype == 'float64' assert csr.type.dtype == 'float64'
...@@ -348,28 +364,30 @@ class test_structureddot(unittest.TestCase): ...@@ -348,28 +364,30 @@ class test_structureddot(unittest.TestCase):
def test_upcast(self): def test_upcast(self):
typenames = 'float32', 'int64', 'int8', 'int32', 'int16', 'float64', 'complex64', 'complex128' typenames = ('float32', 'int64', 'int8', 'int32',
'int16', 'float64', 'complex64', 'complex128')
for dense_dtype in typenames: for dense_dtype in typenames:
for sparse_dtype in typenames: for sparse_dtype in typenames:
correct_dtype = theano.scalar.upcast(sparse_dtype, dense_dtype) correct_dtype = theano.scalar.upcast(sparse_dtype, dense_dtype)
a = SparseType('csc', dtype=sparse_dtype)() a = SparseType('csc', dtype=sparse_dtype)()
b = tensor.matrix(dtype=dense_dtype) b = tensor.matrix(dtype=dense_dtype)
d = structured_dot(a,b) d = structured_dot(a, b)
assert d.type.dtype == correct_dtype assert d.type.dtype == correct_dtype
# compile and run a function # compile and run a function
f = theano.function([a,b],d) f = theano.function([a, b], d)
M,N,K,nnz = (4,3,5,3) M, N, K, nnz = (4, 3, 5, 3)
spmat = sp.csc_matrix(random_lil((M,N), sparse_dtype, nnz)) spmat = sp.csc_matrix(random_lil((M, N), sparse_dtype, nnz))
# the following madness is necessary to workaround # the following madness is necessary to workaround
# an intc vs. int32 bug. # an intc vs. int32 bug.
# The lil makes an intc on my computer when sparse_dtype # The lil makes an intc on my computer when sparse_dtype
# is int32. # is int32.
spmat.dtype = numpy.dtype(sparse_dtype) spmat.dtype = numpy.dtype(sparse_dtype)
mat = numpy.asarray(numpy.random.randn(N,K)*9, dtype=dense_dtype) mat = numpy.asarray(numpy.random.randn(N, K) * 9,
print 'DTYPES', sparse_dtype,dense_dtype dtype=dense_dtype)
print 'DTYPES', sparse_dtype, dense_dtype
print 'sym types', a.type, b.type print 'sym types', a.type, b.type
print 'dtype strings', spmat.dtype, mat.dtype print 'dtype strings', spmat.dtype, mat.dtype
print 'numpy dtype num', mat.dtype.num print 'numpy dtype num', mat.dtype.num
...@@ -380,29 +398,32 @@ class test_structureddot(unittest.TestCase): ...@@ -380,29 +398,32 @@ class test_structureddot(unittest.TestCase):
assert theano_result.dtype == scipy_result.dtype assert theano_result.dtype == scipy_result.dtype
assert _allclose(theano_result, scipy_result) assert _allclose(theano_result, scipy_result)
def test_opt_unpack(self): def test_opt_unpack(self):
# #
# Test that a graph involving structured_dot(assembled_csc_matrix) is optimized to be # Test that a graph involving
# just a structured_dot_csc Op and no assembly of a csc_matrix. # structured_dot(assembled_csc_matrix) is optimized to be just
# a structured_dot_csc Op and no assembly of a csc_matrix.
# #
# The optimization from structured_dot -> structured_dot_csc is currently disabled, # The optimization from structured_dot -> structured_dot_csc
# So this test is not expected to pass # is currently disabled, So this test is not expected to pass
return return
# #
kerns = tensor.Tensor(dtype='int64', broadcastable=[False])('kerns') kerns = tensor.Tensor(dtype='int64', broadcastable=[False])('kerns')
spmat = sp.lil_matrix((4,6), dtype='int64') spmat = sp.lil_matrix((4, 6), dtype='int64')
for i in range(5): for i in range(5):
# set non-zeros in random locations (row x, col y) # set non-zeros in random locations (row x, col y)
x = numpy.floor(numpy.random.rand()*spmat.shape[0]) x = numpy.floor(numpy.random.rand() * spmat.shape[0])
y = numpy.floor(numpy.random.rand()*spmat.shape[1]) y = numpy.floor(numpy.random.rand() * spmat.shape[1])
spmat[x,y] = numpy.random.rand()*10 spmat[x, y] = numpy.random.rand() * 10
spmat = sp.csc_matrix(spmat) spmat = sp.csc_matrix(spmat)
images = tensor.Tensor(dtype='float32', broadcastable=[False, False])('images') images = tensor.Tensor(dtype='float32',
broadcastable=[False, False])(
'images')
cscmat = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape) cscmat = CSC(kerns, spmat.indices[:spmat.size],
spmat.indptr, spmat.shape)
f = theano.function([kerns, images], structured_dot(cscmat, images.T)) f = theano.function([kerns, images], structured_dot(cscmat, images.T))
sdcscpresent = False sdcscpresent = False
...@@ -415,34 +436,37 @@ class test_structureddot(unittest.TestCase): ...@@ -415,34 +436,37 @@ class test_structureddot(unittest.TestCase):
assert sdcscpresent assert sdcscpresent
kernvals = numpy.array(spmat.data[:spmat.size]) kernvals = numpy.array(spmat.data[:spmat.size])
#print 'kdtype', kernvals.dtype, kernvals.shape, kernvals.ndim, kernvals.dtype.num #print 'kdtype', kernvals.dtype, kernvals.shape,
#print kernvals.ndim, kernvals.dtype.num
#print 'type of kernvals = ', kernvals.dtype #print 'type of kernvals = ', kernvals.dtype
bsize = 3 bsize = 3
imvals = 1.0 * numpy.array(numpy.arange(bsize*spmat.shape[1]).\ imvals = 1.0 * numpy.array(numpy.arange(bsize * spmat.shape[1]).\
reshape(bsize,spmat.shape[1]), dtype='float32') reshape(bsize, spmat.shape[1]), dtype='float32')
outvals = f(kernvals,imvals) outvals = f(kernvals, imvals)
print outvals print outvals
def test_dot_sparse_sparse(self): def test_dot_sparse_sparse(self):
#test dot for 2 input sparse matrix #test dot for 2 input sparse matrix
sparse_dtype = 'float64' sparse_dtype = 'float64'
sp_mat = {'csc':sp.csc_matrix, sp_mat = {'csc': sp.csc_matrix,
'csr':sp.csr_matrix} 'csr': sp.csr_matrix}
for sparse_format_a in ['csc','csr']: for sparse_format_a in ['csc', 'csr']:
for sparse_format_b in ['csc', 'csr']: for sparse_format_b in ['csc', 'csr']:
a = SparseType(sparse_format_a, dtype=sparse_dtype)() a = SparseType(sparse_format_a, dtype=sparse_dtype)()
b = SparseType(sparse_format_b, dtype=sparse_dtype)() b = SparseType(sparse_format_b, dtype=sparse_dtype)()
d = theano.dot(a,b) d = theano.dot(a, b)
f = theano.function([a,b], theano.Out(d, borrow=True)) f = theano.function([a, b], theano.Out(d, borrow=True))
topo = f.maker.env.toposort() topo = f.maker.env.toposort()
for M,N,K,nnz in [(4,3,2,3), for M, N, K, nnz in [(4, 3, 2, 3),
(40,30,20,3), (40, 30, 20, 3),
(40,30,20,30), (40, 30, 20, 30),
(400,3000,200,6000), (400, 3000, 200, 6000),
]: ]:
a_val = sp_mat[sparse_format_a](random_lil((M,N), sparse_dtype, nnz)) a_val = sp_mat[sparse_format_a](
b_val = sp_mat[sparse_format_b](random_lil((N,K), sparse_dtype, nnz)) random_lil((M, N), sparse_dtype, nnz))
b_val = sp_mat[sparse_format_b](
random_lil((N, K), sparse_dtype, nnz))
f(a_val, b_val) f(a_val, b_val)
def test_csc_correct_output_faster_than_scipy(self): def test_csc_correct_output_faster_than_scipy(self):
...@@ -451,16 +475,16 @@ class test_structureddot(unittest.TestCase): ...@@ -451,16 +475,16 @@ class test_structureddot(unittest.TestCase):
a = SparseType('csc', dtype=sparse_dtype)() a = SparseType('csc', dtype=sparse_dtype)()
b = tensor.matrix(dtype=dense_dtype) b = tensor.matrix(dtype=dense_dtype)
d = theano.dot(a,b) d = theano.dot(a, b)
f = theano.function([a,b], theano.Out(d, borrow=True)) f = theano.function([a, b], theano.Out(d, borrow=True))
for M,N,K,nnz in [(4,3,2,3), for M, N, K, nnz in [(4, 3, 2, 3),
(40,30,20,3), (40, 30, 20, 3),
(40,30,20,30), (40, 30, 20, 30),
(400,3000,200,6000), (400, 3000, 200, 6000),
]: ]:
spmat = sp.csc_matrix(random_lil((M,N), sparse_dtype, nnz)) spmat = sp.csc_matrix(random_lil((M, N), sparse_dtype, nnz))
mat = numpy.asarray(numpy.random.randn(N,K), dense_dtype) mat = numpy.asarray(numpy.random.randn(N, K), dense_dtype)
theano_times = [] theano_times = []
scipy_times = [] scipy_times = []
for i in xrange(5): for i in xrange(5):
...@@ -470,8 +494,8 @@ class test_structureddot(unittest.TestCase): ...@@ -470,8 +494,8 @@ class test_structureddot(unittest.TestCase):
scipy_result = spmat * mat scipy_result = spmat * mat
t2 = time.time() t2 = time.time()
theano_times.append(t1-t0) theano_times.append(t1 - t0)
scipy_times.append(t2-t1) scipy_times.append(t2 - t1)
theano_time = numpy.min(theano_times) theano_time = numpy.min(theano_times)
scipy_time = numpy.min(scipy_times) scipy_time = numpy.min(scipy_times)
...@@ -479,14 +503,16 @@ class test_structureddot(unittest.TestCase): ...@@ -479,14 +503,16 @@ class test_structureddot(unittest.TestCase):
speedup = scipy_time / theano_time speedup = scipy_time / theano_time
print scipy_times print scipy_times
print theano_times print theano_times
print 'M=%(M)s N=%(N)s K=%(K)s nnz=%(nnz)s theano_time=%(theano_time)s speedup=%(speedup)s' % locals() print ('M=%(M)s N=%(N)s K=%(K)s nnz=%(nnz)s theano_time'
'=%(theano_time)s speedup=%(speedup)s') % locals()
# fail if Theano is slower than scipy by more than a certain amount # fail if Theano is slower than scipy by more than a certain amount
overhead_tol = 0.003 # seconds overall overhead_tol = 0.003 # seconds overall
overhead_rtol = 1.2 # times as long overhead_rtol = 1.2 # times as long
self.assertTrue(numpy.allclose(theano_result, scipy_result)) self.assertTrue(numpy.allclose(theano_result, scipy_result))
if not theano.config.mode in ["DebugMode", "DEBUG_MODE"]: if not theano.config.mode in ["DebugMode", "DEBUG_MODE"]:
self.assertFalse(theano_time > overhead_rtol*scipy_time + overhead_tol) self.assertFalse(theano_time > overhead_rtol * scipy_time +
overhead_tol)
def test_csr_correct_output_faster_than_scipy(self): def test_csr_correct_output_faster_than_scipy(self):
...@@ -497,33 +523,34 @@ class test_structureddot(unittest.TestCase): ...@@ -497,33 +523,34 @@ class test_structureddot(unittest.TestCase):
a = SparseType('csr', dtype=sparse_dtype)() a = SparseType('csr', dtype=sparse_dtype)()
b = tensor.matrix(dtype=dense_dtype) b = tensor.matrix(dtype=dense_dtype)
d = theano.dot(a,b) d = theano.dot(a, b)
f = theano.function([a,b], d) f = theano.function([a, b], d)
for M,N,K,nnz in [(4,3,2,3), for M, N, K, nnz in [(4, 3, 2, 3),
(40,30,20,3), (40, 30, 20, 3),
(40,30,20,30), (40, 30, 20, 30),
(400,3000,200,6000), (400, 3000, 200, 6000),
]: ]:
spmat = sp.csr_matrix(random_lil((M,N), sparse_dtype, nnz)) spmat = sp.csr_matrix(random_lil((M, N), sparse_dtype, nnz))
mat = numpy.asarray(numpy.random.randn(N,K), dense_dtype) mat = numpy.asarray(numpy.random.randn(N, K), dense_dtype)
t0 = time.time() t0 = time.time()
theano_result = f(spmat, mat) theano_result = f(spmat, mat)
t1 = time.time() t1 = time.time()
scipy_result = spmat * mat scipy_result = spmat * mat
t2 = time.time() t2 = time.time()
theano_time = t1-t0 theano_time = t1 - t0
scipy_time = t2-t1 scipy_time = t2 - t1
#print theano_result #print theano_result
#print scipy_result #print scipy_result
print 'theano took', theano_time, print 'theano took', theano_time,
print 'scipy took', scipy_time print 'scipy took', scipy_time
overhead_tol = 0.002 # seconds overhead_tol = 0.002 # seconds
overhead_rtol = 1.1 # times as long overhead_rtol = 1.1 # times as long
self.assertTrue(numpy.allclose(theano_result, scipy_result)) self.assertTrue(numpy.allclose(theano_result, scipy_result))
if not theano.config.mode in ["DebugMode", "DEBUG_MODE"]: if not theano.config.mode in ["DebugMode", "DEBUG_MODE"]:
self.assertFalse(theano_time > overhead_rtol*scipy_time + overhead_tol) self.assertFalse(theano_time > overhead_rtol * scipy_time +
overhead_tol)
class DotTests(unittest.TestCase): class DotTests(unittest.TestCase):
...@@ -531,11 +558,16 @@ class DotTests(unittest.TestCase): ...@@ -531,11 +558,16 @@ class DotTests(unittest.TestCase):
x_size = (10, 1000) x_size = (10, 1000)
y_size = (1000, 10000) y_size = (1000, 10000)
self.x_csr = scipy.sparse.csr_matrix(numpy.random.binomial(1, 0.5, x_size), dtype=theano.config.floatX) self.x_csr = scipy.sparse.csr_matrix(
self.x_csc = scipy.sparse.csc_matrix(numpy.random.binomial(1, 0.5, x_size), dtype=theano.config.floatX) numpy.random.binomial(1, 0.5, x_size), dtype=theano.config.floatX)
self.y = numpy.asarray(numpy.random.uniform(-1, 1, y_size), dtype=theano.config.floatX) self.x_csc = scipy.sparse.csc_matrix(
self.y_csr = scipy.sparse.csr_matrix(numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX) numpy.random.binomial(1, 0.5, x_size), dtype=theano.config.floatX)
self.y_csc = scipy.sparse.csc_matrix(numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX) self.y = numpy.asarray(numpy.random.uniform(-1, 1, y_size),
dtype=theano.config.floatX)
self.y_csr = scipy.sparse.csr_matrix(
numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX)
self.y_csc = scipy.sparse.csc_matrix(
numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX)
def test_csr_dense(self): def test_csr_dense(self):
x = theano.sparse.csr_matrix('x') x = theano.sparse.csr_matrix('x')
...@@ -551,11 +583,12 @@ class DotTests(unittest.TestCase): ...@@ -551,11 +583,12 @@ class DotTests(unittest.TestCase):
f_b = lambda x, y: (x * y).shape f_b = lambda x, y: (x * y).shape
assert numpy.all(f_a(self.x_csr, self.y) == f_b(self.x_csr, self.y)) assert numpy.all(f_a(self.x_csr, self.y) == f_b(self.x_csr, self.y))
topo = f_a.maker.env.toposort() topo = f_a.maker.env.toposort()
if theano.config.mode!='FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
nb = 0 nb = 0
else: else:
nb = 1 nb = 1
assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense)) for node in topo]) == nb assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense))
for node in topo]) == nb
def test_csc_dense(self): def test_csc_dense(self):
x = theano.sparse.csc_matrix('x') x = theano.sparse.csc_matrix('x')
...@@ -571,11 +604,12 @@ class DotTests(unittest.TestCase): ...@@ -571,11 +604,12 @@ class DotTests(unittest.TestCase):
f_b = lambda x, y: (x * y).shape f_b = lambda x, y: (x * y).shape
assert numpy.all(f_a(self.x_csc, self.y) == f_b(self.x_csc, self.y)) assert numpy.all(f_a(self.x_csc, self.y) == f_b(self.x_csc, self.y))
topo = f_a.maker.env.toposort() topo = f_a.maker.env.toposort()
if theano.config.mode!='FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
nb = 0 nb = 0
else: else:
nb = 1 nb = 1
assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense)) for node in topo]) == nb assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense))
for node in topo]) == nb
def test_sparse_sparse(self): def test_sparse_sparse(self):
for d1, d2 in [('float32', 'float32'), for d1, d2 in [('float32', 'float32'),
...@@ -585,10 +619,10 @@ class DotTests(unittest.TestCase): ...@@ -585,10 +619,10 @@ class DotTests(unittest.TestCase):
('float32', 'int16'), ('float32', 'int16'),
('float32', 'complex64'), ('float32', 'complex64'),
]: ]:
for x_f, y_f in [('csc','csc'), for x_f, y_f in [('csc', 'csc'),
('csc','csr'), ('csc', 'csr'),
('csr','csc'), ('csr', 'csc'),
('csr','csr'), ('csr', 'csr'),
]: ]:
x = theano.sparse.SparseType(format=x_f, dtype=d1)('x') x = theano.sparse.SparseType(format=x_f, dtype=d1)('x')
y = theano.sparse.SparseType(format=x_f, dtype=d2)('x') y = theano.sparse.SparseType(format=x_f, dtype=d2)('x')
...@@ -596,8 +630,8 @@ class DotTests(unittest.TestCase): ...@@ -596,8 +630,8 @@ class DotTests(unittest.TestCase):
f_a = theano.function([x, y], theano.sparse.dot(x, y)) f_a = theano.function([x, y], theano.sparse.dot(x, y))
f_b = lambda x, y: x * y f_b = lambda x, y: x * y
vx = getattr(self,'x_'+x_f).astype(d1) vx = getattr(self, 'x_' + x_f).astype(d1)
vy = getattr(self,'y_'+y_f).astype(d2) vy = getattr(self, 'y_' + y_f).astype(d2)
assert _allclose(f_a(vx, vy), f_b(vx, vy).toarray()) assert _allclose(f_a(vx, vy), f_b(vx, vy).toarray())
# Test infer_shape # Test infer_shape
...@@ -605,11 +639,12 @@ class DotTests(unittest.TestCase): ...@@ -605,11 +639,12 @@ class DotTests(unittest.TestCase):
f_b = lambda x, y: (x * y).shape f_b = lambda x, y: (x * y).shape
assert numpy.all(f_a(vx, vy) == f_b(vx, vy)) assert numpy.all(f_a(vx, vy) == f_b(vx, vy))
topo = f_a.maker.env.toposort() topo = f_a.maker.env.toposort()
if theano.config.mode!='FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
nb = 0 nb = 0
else: else:
nb = 1 nb = 1
assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense)) for node in topo]) == nb assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense))
for node in topo]) == nb
class UsmmTests(unittest.TestCase): class UsmmTests(unittest.TestCase):
...@@ -619,9 +654,14 @@ class UsmmTests(unittest.TestCase): ...@@ -619,9 +654,14 @@ class UsmmTests(unittest.TestCase):
y_size = (100, 200) y_size = (100, 200)
z_size = (x_size[0], y_size[1]) z_size = (x_size[0], y_size[1])
self.x = numpy.asarray(numpy.random.binomial(1, 0.5, x_size), dtype=theano.config.floatX) self.x = numpy.asarray(numpy.random.binomial(1, 0.5, x_size),
self.y = numpy.asarray(numpy.random.uniform(-1, 1, y_size), dtype=theano.config.floatX) dtype=theano.config.floatX)
self.z = numpy.asarray(numpy.random.uniform(-1, 1, z_size), dtype=theano.config.floatX) self.y = numpy.asarray(numpy.random.uniform(-1, 1, y_size),
dtype=theano.config.floatX)
self.z = numpy.asarray(numpy.random.uniform(-1, 1, z_size),
dtype=theano.config.floatX)
utt.seed_rng()
self.rng = numpy.random.RandomState(seed=utt.fetch_seed())
def test(self): def test(self):
def mat(format, name, dtype): def mat(format, name, dtype):
...@@ -633,6 +673,11 @@ class UsmmTests(unittest.TestCase): ...@@ -633,6 +673,11 @@ class UsmmTests(unittest.TestCase):
params = product(*([['float32', 'float64', 'int16', 'complex64']] * 4 + params = product(*([['float32', 'float64', 'int16', 'complex64']] * 4 +
[['dense', 'csc', 'csr']] * 2)) [['dense', 'csc', 'csr']] * 2))
# All test are too slow, so we randomly take 100 of them.
# The buildbot change the seed, so we will finish by running them all.
# As of this writing they where all passing.
#params = self.rng.permutation(list(params))[:500]
for dtype1, dtype2, dtype3, dtype4, format1, format2 in params: for dtype1, dtype2, dtype3, dtype4, format1, format2 in params:
if format1 == 'dense' and format2 == 'dense': if format1 == 'dense' and format2 == 'dense':
# Usmm won't be used! # Usmm won't be used!
...@@ -696,7 +741,8 @@ class UsmmTests(unittest.TestCase): ...@@ -696,7 +741,8 @@ class UsmmTests(unittest.TestCase):
new_topo = [] new_topo = []
for node in topo: for node in topo:
if not (isinstance(node.op, tensor.Elemwise) and \ if not (isinstance(node.op, tensor.Elemwise) and \
isinstance(node.op.scalar_op, theano.scalar.basic.Cast)): isinstance(node.op.scalar_op,
theano.scalar.basic.Cast)):
new_topo.append(node) new_topo.append(node)
topo = new_topo topo = new_topo
assert len(topo) == 5, topo assert len(topo) == 5, topo
...@@ -713,12 +759,11 @@ class UsmmTests(unittest.TestCase): ...@@ -713,12 +759,11 @@ class UsmmTests(unittest.TestCase):
assert topo[4].op.inplace assert topo[4].op.inplace
elif not fast_compile: elif not fast_compile:
# The op Usmm should be inserted # The op Usmm should be inserted
assert len(topo)==3, topo assert len(topo) == 3, topo
assert isinstance(topo[0].op, theano.tensor.DimShuffle) assert isinstance(topo[0].op, theano.tensor.DimShuffle)
assert topo[1].op == theano.tensor.neg assert topo[1].op == theano.tensor.neg
assert isinstance(topo[2].op, theano.sparse.Usmm) assert isinstance(topo[2].op, theano.sparse.Usmm)
def test_infer_shape(self): def test_infer_shape(self):
def mat(format, name, dtype): def mat(format, name, dtype):
if format == 'dense': if format == 'dense':
...@@ -765,8 +810,8 @@ class UsmmTests(unittest.TestCase): ...@@ -765,8 +810,8 @@ class UsmmTests(unittest.TestCase):
nb = 0 nb = 0
else: else:
nb = 1 nb = 1
assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense)) for node in topo]) == nb assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense))
for node in topo]) == nb
def test_shape_i(): def test_shape_i():
...@@ -774,7 +819,8 @@ def test_shape_i(): ...@@ -774,7 +819,8 @@ def test_shape_i():
a = SparseType('csr', dtype=sparse_dtype)() a = SparseType('csr', dtype=sparse_dtype)()
f = theano.function([a], a.shape[1]) f = theano.function([a], a.shape[1])
assert f(sp.csr_matrix(random_lil((100,10), sparse_dtype, 3))) == 10 assert f(sp.csr_matrix(random_lil((100, 10), sparse_dtype, 3))) == 10
def test_shape(): def test_shape():
# Test that getting the shape of a sparse variable # Test that getting the shape of a sparse variable
...@@ -783,47 +829,69 @@ def test_shape(): ...@@ -783,47 +829,69 @@ def test_shape():
a = SparseType('csr', dtype=sparse_dtype)() a = SparseType('csr', dtype=sparse_dtype)()
f = theano.function([a], a.shape) f = theano.function([a], a.shape)
assert numpy.all(f(sp.csr_matrix(random_lil((100,10), sparse_dtype, 3)))==(100,10)) assert numpy.all(f(sp.csr_matrix(random_lil((100, 10), sparse_dtype, 3)))
if theano.config.mode!='FAST_COMPILE': == (100, 10))
if theano.config.mode != 'FAST_COMPILE':
topo = f.maker.env.toposort() topo = f.maker.env.toposort()
assert len(topo)==3 assert len(topo) == 3
assert isinstance(topo[0].op,tensor.opt.Shape_i) assert isinstance(topo[0].op, tensor.opt.Shape_i)
assert isinstance(topo[1].op,tensor.opt.Shape_i) assert isinstance(topo[1].op, tensor.opt.Shape_i)
assert isinstance(topo[2].op,tensor.opt.MakeVector) assert isinstance(topo[2].op, tensor.opt.MakeVector)
def test_may_share_memory(): def test_may_share_memory():
a=scipy.sparse.csc_matrix(scipy.sparse.eye(5,3)) a = scipy.sparse.csc_matrix(scipy.sparse.eye(5, 3))
b=scipy.sparse.csc_matrix(scipy.sparse.eye(4,3)) b = scipy.sparse.csc_matrix(scipy.sparse.eye(4, 3))
as_ar = lambda a: theano._asarray(a, dtype='int32') as_ar = lambda a: theano._asarray(a, dtype='int32')
for a_,b_,rep in [(a,a,True),(b,b,True),(a,b,False), for a_, b_, rep in [(a, a, True),
(a,a.data,True),(a,a.indptr,True),(a,a.indices,True),(a,as_ar(a.shape),False), (b, b, True),
(a.data,a,True),(a.indptr,a,True),(a.indices,a,True),(as_ar(a.shape),a,False), (a, b, False),
(b,b.data,True),(b,b.indptr,True),(b,b.indices,True),(b,as_ar(b.shape),False), (a, a.data, True),
(b.data,b,True),(b.indptr,b,True),(b.indices,b,True),(as_ar(b.shape),b,False), (a, a.indptr, True),
(b.data,a,False),(b.indptr,a,False),(b.indices,a,False),(as_ar(b.shape),a,False), (a, a.indices, True),
]: (a, as_ar(a.shape), False),
(a.data, a, True),
(a.indptr, a, True),
(a.indices, a, True),
(as_ar(a.shape), a, False),
(b, b.data, True),
(b, b.indptr, True),
(b, b.indices, True),
(b, as_ar(b.shape), False),
(b.data, b, True),
(b.indptr, b, True),
(b.indices, b, True),
(as_ar(b.shape), b, False),
(b.data, a, False),
(b.indptr, a, False),
(b.indices, a, False),
(as_ar(b.shape), a, False),
]:
assert SparseType.may_share_memory(a_, b_) == rep
assert SparseType.may_share_memory(a_,b_)==rep
def test_sparse_shared_memory(): def test_sparse_shared_memory():
# Note : There are no inplace ops on sparse matrix yet. If one is someday implemented, we could test it here. # Note : There are no inplace ops on sparse matrix yet. If one is
a = random_lil((3,4), 'float32', 3).tocsr() # someday implemented, we could test it here.
m1 = random_lil((4,4), 'float32', 3).tocsr() a = random_lil((3, 4), 'float32', 3).tocsr()
m2 = random_lil((4,4), 'float32', 3).tocsr() m1 = random_lil((4, 4), 'float32', 3).tocsr()
m2 = random_lil((4, 4), 'float32', 3).tocsr()
x = SparseType('csr', dtype='float32')() x = SparseType('csr', dtype='float32')()
y = SparseType('csr', dtype='float32')() y = SparseType('csr', dtype='float32')()
sdot = theano.sparse.structured_dot sdot = theano.sparse.structured_dot
z = sdot(x*3,m1) + sdot(y*2, m2) z = sdot(x * 3, m1) + sdot(y * 2, m2)
f = theano.function([theano.In(x,mutable=True),theano.In(y,mutable = True)],z, mode='FAST_RUN') f = theano.function([theano.In(x, mutable=True),
theano.In(y, mutable=True)], z, mode='FAST_RUN')
def f_(x,y,m1=m1,m2=m2): def f_(x, y, m1=m1, m2=m2):
return numpy.dot(x*3,m1) + numpy.dot(y*2,m2) return numpy.dot(x * 3, m1) + numpy.dot(y * 2, m2)
assert SparseType.may_share_memory(a,a) #This is trivial assert SparseType.may_share_memory(a, a) # This is trivial
result = f(a,a) result = f(a, a)
result_ = f_(a,a) result_ = f_(a, a)
assert (result_.todense() == result.todense()).all() assert (result_.todense() == result.todense()).all()
...@@ -835,6 +903,7 @@ def test_size(): ...@@ -835,6 +903,7 @@ def test_size():
x = getattr(theano.sparse, sparse_type)() x = getattr(theano.sparse, sparse_type)()
y = getattr(scipy.sparse, sparse_type)((5, 7)).astype(config.floatX) y = getattr(scipy.sparse, sparse_type)((5, 7)).astype(config.floatX)
get_size = theano.function([x], x.size) get_size = theano.function([x], x.size)
def check(): def check():
assert y.size == get_size(y) assert y.size == get_size(y)
# We verify that the size is correctly updated as we store more data # We verify that the size is correctly updated as we store more data
...@@ -847,20 +916,20 @@ def test_size(): ...@@ -847,20 +916,20 @@ def test_size():
import theano.tensor.tests.test_sharedvar import theano.tensor.tests.test_sharedvar
test_shared_options=theano.tensor.tests.test_sharedvar.makeSharedTester( test_shared_options = theano.tensor.tests.test_sharedvar.makeSharedTester(
shared_constructor_ = theano.sparse.shared, shared_constructor_=theano.sparse.shared,
dtype_ = 'float64', dtype_='float64',
get_value_borrow_true_alias_ = True, get_value_borrow_true_alias_=True,
shared_borrow_true_alias_ = True, shared_borrow_true_alias_=True,
set_value_borrow_true_alias_ = True, set_value_borrow_true_alias_=True,
set_value_inplace_ = False, set_value_inplace_=False,
set_casted_value_inplace_ = False, set_casted_value_inplace_=False,
shared_constructor_accept_ndarray_ = False, shared_constructor_accept_ndarray_=False,
internal_type_ = scipy.sparse.csc_matrix, internal_type_=scipy.sparse.csc_matrix,
test_internal_type_ = scipy.sparse.issparse, test_internal_type_=scipy.sparse.issparse,
theano_fct_ = lambda a: dense_from_sparse(a*2.), theano_fct_=lambda a: dense_from_sparse(a * 2.),
ref_fct_ = lambda a: numpy.asarray((a*2).todense()), ref_fct_=lambda a: numpy.asarray((a * 2).todense()),
cast_value_ = scipy.sparse.csr_matrix, cast_value_=scipy.sparse.csr_matrix,
name='test_shared_options', name='test_shared_options',
) )
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论