Esempio n. 1
0
    def test_sparse_input_aliasing_affecting_inplace_operations(self):
        ##
        # Note this test will never fail because I am not aware of any
        # inplace op on sparse variables
        try:
            import scipy.sparse as sp
        except ImportError:
            # The variable enable_sparse will be used to disable the test file.
            pass

        from theano.sparse import enable_sparse

        if not enable_sparse:
            pytest.skip("Optional package sparse disabled")

        from theano import sparse

        # Note: to trigger this bug with theano rev 4586:2bc6fc7f218b,
        #        you need to make in inputs mutable (so that inplace
        #        operations are used) and to break the elemwise composition
        #        with some non-elemwise op (here dot)

        x = sparse.SparseType("csc", dtype="float64")()
        y = sparse.SparseType("csc", dtype="float64")()
        f = theano.function(
            [theano.In(x, mutable=True),
             theano.In(y, mutable=True)], (x + y) + (x + y))
        # Test 1. If the same variable is given twice

        # Compute bogus values
        m = sp.csc_matrix(
            np.asarray(
                [
                    [1, 0, 0, 0, 0],
                    [0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 1],
                ],
                dtype="float64",
            ))
        bogus_vals = f(m, m)
        # Since we used inplace operation v and m may be corrupted
        # so we need to recreate them

        m = sp.csc_matrix(
            np.asarray(
                [
                    [1, 0, 0, 0, 0],
                    [0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 1],
                ],
                dtype="float64",
            ))
        m_copy = m.copy()
        vals = f(m, m_copy)

        assert np.allclose(vals.todense(), bogus_vals.todense())
Esempio n. 2
0
    def test_sparse_input_aliasing_affecting_inplace_operations(self):
        sp = pytest.importorskip("scipy", minversion="0.7.0")

        from theano import sparse

        # Note: to trigger this bug with theano rev 4586:2bc6fc7f218b,
        #        you need to make in inputs mutable (so that inplace
        #        operations are used) and to break the elemwise composition
        #        with some non-elemwise op (here dot)

        x = sparse.SparseType("csc", dtype="float64")()
        y = sparse.SparseType("csc", dtype="float64")()
        f = theano.function(
            [theano.In(x, mutable=True), theano.In(y, mutable=True)], (x + y) + (x + y)
        )
        # Test 1. If the same variable is given twice

        # Compute bogus values
        m = sp.sparse.csc_matrix(
            np.asarray(
                [
                    [1, 0, 0, 0, 0],
                    [0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 1],
                ],
                dtype="float64",
            )
        )
        bogus_vals = f(m, m)
        # Since we used inplace operation v and m may be corrupted
        # so we need to recreate them

        m = sp.sparse.csc_matrix(
            np.asarray(
                [
                    [1, 0, 0, 0, 0],
                    [0, 1, 0, 0, 0],
                    [0, 0, 1, 0, 0],
                    [0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 1],
                ],
                dtype="float64",
            )
        )
        m_copy = m.copy()
        vals = f(m, m_copy)

        assert np.allclose(vals.todense(), bogus_vals.todense())
Esempio n. 3
0
    def make_node(self, diag):
        diag = tensor.as_tensor_variable(diag)
        if diag.type.ndim != 1:
            raise TypeError('data argument must be a vector', diag.type)

        return gof.Apply(self, [diag],
                [sparse.SparseType(dtype=diag.dtype, format='csc')()])
Esempio n. 4
0
    def test_sparse(self):
        mySymbolicSparseList = TypedListType(
            sparse.SparseType('csr', theano.config.floatX))()
        mySymbolicSparse = sparse.csr_matrix()

        z = Count()(mySymbolicSparseList, mySymbolicSparse)

        f = theano.function([mySymbolicSparseList, mySymbolicSparse], z)

        x = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))
        y = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))

        self.assertTrue(f([x, y, y], y) == 2)
Esempio n. 5
0
    def test_sparse(self):
        sp = pytest.importorskip("scipy")
        mySymbolicSparseList = TypedListType(
            sparse.SparseType("csr", theano.config.floatX))()
        mySymbolicSparse = sparse.csr_matrix()

        z = Count()(mySymbolicSparseList, mySymbolicSparse)

        f = theano.function([mySymbolicSparseList, mySymbolicSparse], z)

        x = sp.sparse.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))
        y = sp.sparse.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))

        assert f([x, y, y], y) == 2
Esempio n. 6
0
    def test_sparse(self):
        if not scipy_imported:
            raise SkipTest('Optional package SciPy not installed')
        mySymbolicSparseList = TypedListType(
            sparse.SparseType('csr', theano.config.floatX))()
        mySymbolicSparse = sparse.csr_matrix()

        z = Count()(mySymbolicSparseList, mySymbolicSparse)

        f = theano.function([mySymbolicSparseList, mySymbolicSparse], z)

        x = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))
        y = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))

        self.assertTrue(f([x, y, y], y) == 2)
Esempio n. 7
0
    def test_sparse(self):
        if not scipy_imported:
            pytest.skip("Optional package SciPy not installed")
        mySymbolicSparseList = TypedListType(
            sparse.SparseType("csr", theano.config.floatX)
        )()
        mySymbolicSparse = sparse.csr_matrix()

        z = Count()(mySymbolicSparseList, mySymbolicSparse)

        f = theano.function([mySymbolicSparseList, mySymbolicSparse], z)

        x = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))
        y = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3))

        assert f([x, y, y], y) == 2
Esempio n. 8
0
 def test_mul_s_v(self):
     sp_types = {'csc': sp.csc_matrix,
         'csr': sp.csr_matrix}
     
     for format in ['csr', 'csc']:
         for dtype in ['float32', 'float64']:
             x = S.SparseType(format, dtype=dtype)()
             y = T.vector(dtype=dtype)
             f = theano.function([x, y], S2.mul_s_v(x, y))
             
             spmat = sp_types[format](random_lil((4, 3), dtype, 3))
             mat = numpy.asarray(numpy.random.rand(3), dtype=dtype)
             
             out = f(spmat, mat)
             
             assert numpy.allclose(out.toarray(), spmat.toarray() * mat)
Esempio n. 9
0
 def test_structured_add_s_v(self):
     sp_types = {'csc': sp.csc_matrix,
         'csr': sp.csr_matrix}
     
     for format in ['csr', 'csc']:
         for dtype in ['float32', 'float64']:
             x = S.SparseType(format, dtype=dtype)()
             y = T.vector(dtype=dtype)
             f = theano.function([x, y], S2.structured_add_s_v(x, y))
             
             spmat = sp_types[format](random_lil((4, 3), dtype, 3))
             spones = spmat.copy()
             spones.data = numpy.ones_like(spones.data)
             mat = numpy.asarray(numpy.random.rand(3), dtype=dtype)
             
             out = f(spmat, mat)
             
             assert numpy.allclose(out.toarray(), spones.multiply(spmat + mat))
Esempio n. 10
0
 def make_node(self, diag):
     return gof.Apply(self, [diag],
                      [sparse.SparseType(dtype=diag.dtype, format='csc')()])