Exemplo n.º 1
0
    def test_sparse_input_aliasing_affecting_inplace_operations(self):
        sp = pytest.importorskip("scipy", minversion="0.7.0")

        from aesara import sparse

        # Note: to trigger this bug with aesara 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 = function([In(x, mutable=True), 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())
Exemplo n.º 2
0
    def test_sparse(self):
        sp = pytest.importorskip("scipy")
        mySymbolicSparseList = TypedListType(
            sparse.SparseType("csr", aesara.config.floatX))()
        mySymbolicSparse = sparse.csr_matrix()

        z = Count()(mySymbolicSparseList, mySymbolicSparse)

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

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

        assert f([x, y, y], y) == 2