def _testSS( self, op, array1=numpy.array([[1.0, 0], [3, 0], [0, 6]]), array2=numpy.asarray([[0, 2.0], [0, 4], [5, 0]]) ): for mtype in _mtypes: a = mtype(array1) aR = as_sparse_variable(a) self.assertFalse(aR.data is a) self.assertTrue(_is_sparse(a)) self.assertTrue(_is_sparse_variable(aR)) b = mtype(array2) bR = as_sparse_variable(b) self.assertFalse(bR.data is b) self.assertTrue(_is_sparse(b)) self.assertTrue(_is_sparse_variable(bR)) apb = op(aR, bR) self.assertTrue(_is_sparse_variable(apb)) self.assertTrue(apb.type.dtype == aR.type.dtype, apb.type.dtype) self.assertTrue(apb.type.dtype == bR.type.dtype, apb.type.dtype) self.assertTrue(apb.type.format == aR.type.format, apb.type.format) self.assertTrue(apb.type.format == bR.type.format, apb.type.format) val = eval_outputs([apb]) self.assertTrue(val.shape == (3, 2)) if op is add: self.assertTrue(numpy.all(val.todense() == (a + b).todense())) ans = numpy.array([[1.0, 2], [3, 4], [5, 6]]) self.assertTrue(numpy.all(val.todense() == ans)) verify_grad_sparse(op, [a, b], structured=False) elif op is mul: self.assertTrue(numpy.all(val.todense() == (a.multiply(b)).todense())) ans = numpy.array([[1, 0], [9, 0], [0, 36]]) self.assertTrue(numpy.all(val.todense() == ans))
def test_structureddot_csr_grad(self): # shortcut: testing csc in float32, testing csr in float64 # allocate a random sparse matrix spmat = sp.csr_matrix(random_lil((4, 3), "float64", 3)) mat = numpy.asarray(numpy.random.randn(3, 2), "float64") verify_grad_sparse(structured_dot, [spmat, mat], structured=True) def buildgraph_T(spmat, mat): return structured_dot(mat.T, spmat.T) verify_grad_sparse(buildgraph_T, [spmat, mat], structured=True)