def test_ensure_sorted_indices(): 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_ensure_sorted_indices(): 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_local_csm_properties_csm(): data = tensor.vector() indices, indptr, shape = (tensor.ivector(), tensor.ivector(), tensor.ivector()) mode = theano.compile.mode.get_default_mode() mode = mode.including("specialize", "local_csm_properties_csm") for CS, cast in [(sparse.CSC, sp.csc_matrix), (sparse.CSR, sp.csr_matrix)]: f = theano.function([data, indices, indptr, shape], sparse.csm_properties( CS(data, indices, indptr, shape)), mode=mode) assert not any( isinstance(node.op, (sparse.CSM, sparse.CSMProperties)) for node in f.maker.fgraph.toposort()) v = cast(random_lil((10, 40), config.floatX, 3)) f(v.data, v.indices, v.indptr, v.shape)
def test_local_csm_properties_csm(): data = tensor.vector() indices, indptr, shape = (tensor.ivector(), tensor.ivector(), tensor.ivector()) mode = theano.compile.mode.get_default_mode() mode = mode.including("specialize", "local_csm_properties_csm") for CS, cast in [(CSC, sp.csc_matrix), (CSR, sp.csr_matrix)]: f = theano.function([data, indices, indptr, shape], csm_properties(CS(data, indices, indptr, shape)), mode=mode) #theano.printing.debugprint(f) assert not any(isinstance(node.op, (CSM, CSMProperties)) for node in f.maker.env.toposort()) v = cast(random_lil((10, 40), config.floatX, 3)) f(v.data, v.indices, v.indptr, v.shape)
def test_local_csm_grad_c(): data = tensor.vector() indices, indptr, shape = (tensor.ivector(), tensor.ivector(), tensor.ivector()) mode = theano.compile.mode.get_default_mode() if theano.config.mode == 'FAST_COMPILE': mode = theano.compile.Mode(linker='c|py', optimizer='fast_compile') mode = mode.including("specialize", "local_csm_grad_c") for CS, cast in [(sparse.CSC, sp.csc_matrix), (sparse.CSR, sp.csr_matrix)]: cost = tensor.sum(sparse.DenseFromSparse()(CS(data, indices, indptr, shape))) f = theano.function( [data, indices, indptr, shape], tensor.grad(cost, data), mode=mode) assert not any(isinstance(node.op, sparse.CSMGrad) for node in f.maker.fgraph.toposort()) v = cast(random_lil((10, 40), config.floatX, 3)) f(v.data, v.indices, v.indptr, v.shape)
def test_local_csm_grad_c(): if not theano.config.cxx: raise SkipTest("G++ not available, so we need to skip this test.") data = tensor.vector() indices, indptr, shape = (tensor.ivector(), tensor.ivector(), tensor.ivector()) mode = theano.compile.mode.get_default_mode() if theano.config.mode == 'FAST_COMPILE': mode = theano.compile.Mode(linker='c|py', optimizer='fast_compile') mode = mode.including("specialize", "local_csm_grad_c") for CS, cast in [(sparse.CSC, sp.csc_matrix), (sparse.CSR, sp.csr_matrix)]: cost = tensor.sum(sparse.DenseFromSparse()(CS(data, indices, indptr, shape))) f = theano.function( [data, indices, indptr, shape], tensor.grad(cost, data), mode=mode) assert not any(isinstance(node.op, sparse.CSMGrad) for node in f.maker.fgraph.toposort()) v = cast(random_lil((10, 40), config.floatX, 3)) f(v.data, v.indices, v.indptr, v.shape)