1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
.. _libdoc_sparse:
=========================================
:mod:`sparse` -- Symbolic Sparse Matrices
=========================================
In the tutorial section, you can find a :ref:`sparse tutorial
<tutsparse>`.
The sparse submodule is not loaded when we import Theano. You must
import ``theano.sparse`` to enable it.
The sparse module provides the same functionality as the tensor
module. The difference lies under the covers because sparse matrices
do not store data in a contiguous array. Note that there are no GPU
implementations for sparse matrices in Theano. The sparse module has
been used in:
- NLP: Dense linear transformations of sparse vectors.
- Audio: Filterbank in the Fourier domain.
Compressed Sparse Format
========================
This section tries to explain how information is stored for the two
sparse formats of SciPy supported by Theano. There are more formats
that can be used with SciPy and some documentation about them may be
found `here
<../../sandbox/sparse.html>`_.
.. Changes to this section should also result in changes to tutorial/sparse.txt.
Theano supports two *compressed sparse formats*: ``csc`` and ``csr``,
respectively based on columns and rows. They have both the same
attributes: ``data``, ``indices``, ``indptr`` and ``shape``.
* The ``data`` attribute is a one-dimensional ``ndarray`` which
contains all the non-zero elements of the sparse matrix.
* The ``indices`` and ``indptr`` attributes are used to store the
position of the data in the sparse matrix.
* The ``shape`` attribute is exactly the same as the ``shape``
attribute of a dense (i.e. generic) matrix. It can be explicitly
specified at the creation of a sparse matrix if it cannot be
inferred from the first three attributes.
CSC Matrix
----------
In the *Compressed Sparse Column* format, ``indices`` stands for
indexes inside the column vectors of the matrix and ``indptr`` tells
where the column starts in the ``data`` and in the ``indices``
attributes. ``indptr`` can be thought of as giving the slice which
must be applied to the other attribute in order to get each column of
the matrix. In other words, ``slice(indptr[i], indptr[i+1])``
corresponds to the slice needed to find the i-th column of the matrix
in the ``data`` and ``indices`` fields.
The following example builds a matrix and returns its columns. It
prints the i-th column, i.e. a list of indices in the column and their
corresponding value in the second list.
>>> import numpy as np
>>> import scipy.sparse as sp
>>> data = np.asarray([7, 8, 9])
>>> indices = np.asarray([0, 1, 2])
>>> indptr = np.asarray([0, 2, 3, 3])
>>> m = sp.csc_matrix((data, indices, indptr), shape=(3, 3))
>>> m.toarray()
array([[7, 0, 0],
[8, 0, 0],
[0, 9, 0]])
>>> i = 0
>>> m.indices[m.indptr[i]:m.indptr[i+1]], m.data[m.indptr[i]:m.indptr[i+1]]
(array([0, 1], dtype=int32), array([7, 8]))
>>> i = 1
>>> m.indices[m.indptr[i]:m.indptr[i+1]], m.data[m.indptr[i]:m.indptr[i+1]]
(array([2], dtype=int32), array([9]))
>>> i = 2
>>> m.indices[m.indptr[i]:m.indptr[i+1]], m.data[m.indptr[i]:m.indptr[i+1]]
(array([], dtype=int32), array([], dtype=int64))
CSR Matrix
----------
In the *Compressed Sparse Row* format, ``indices`` stands for indexes
inside the row vectors of the matrix and ``indptr`` tells where the
row starts in the ``data`` and in the ``indices``
attributes. ``indptr`` can be thought of as giving the slice which
must be applied to the other attribute in order to get each row of the
matrix. In other words, ``slice(indptr[i], indptr[i+1])`` corresponds
to the slice needed to find the i-th row of the matrix in the ``data``
and ``indices`` fields.
The following example builds a matrix and returns its rows. It prints
the i-th row, i.e. a list of indices in the row and their
corresponding value in the second list.
>>> import numpy as np
>>> import scipy.sparse as sp
>>> data = np.asarray([7, 8, 9])
>>> indices = np.asarray([0, 1, 2])
>>> indptr = np.asarray([0, 2, 3, 3])
>>> m = sp.csr_matrix((data, indices, indptr), shape=(3, 3))
>>> m.toarray()
array([[7, 8, 0],
[0, 0, 9],
[0, 0, 0]])
>>> i = 0
>>> m.indices[m.indptr[i]:m.indptr[i+1]], m.data[m.indptr[i]:m.indptr[i+1]]
(array([0, 1], dtype=int32), array([7, 8]))
>>> i = 1
>>> m.indices[m.indptr[i]:m.indptr[i+1]], m.data[m.indptr[i]:m.indptr[i+1]]
(array([2], dtype=int32), array([9]))
>>> i = 2
>>> m.indices[m.indptr[i]:m.indptr[i+1]], m.data[m.indptr[i]:m.indptr[i+1]]
(array([], dtype=int32), array([], dtype=int64))
List of Implemented Operations
==============================
- Moving from and to sparse
- :func:`dense_from_sparse <theano.sparse.basic.dense_from_sparse>`.
Both grads are implemented. Structured by default.
- :func:`csr_from_dense <theano.sparse.basic.csr_from_dense>`,
:func:`csc_from_dense <theano.sparse.basic.csc_from_dense>`.
The grad implemented is structured.
- Theano SparseVariable objects have a method ``toarray()`` that is the same as
:func:`dense_from_sparse <theano.sparse.basic.dense_from_sparse>`.
- Construction of Sparses and their Properties
- :class:`CSM <theano.sparse.basic.CSM>` and ``CSC``, ``CSR`` to construct a matrix.
The grad implemented is regular.
- :func:`csm_properties <theano.sparse.basic.csm_properties>`.
to get the properties of a sparse matrix.
The grad implemented is regular.
- csm_indices(x), csm_indptr(x), csm_data(x) and csm_shape(x) or x.shape.
- :func:`sp_ones_like <theano.sparse.basic.sp_ones_like>`.
The grad implemented is regular.
- :func:`sp_zeros_like <theano.sparse.basic.sp_zeros_like>`.
The grad implemented is regular.
- :func:`square_diagonal <theano.sparse.basic.square_diagonal>`.
The grad implemented is regular.
- :func:`construct_sparse_from_list <theano.sparse.basic.construct_sparse_from_list>`.
The grad implemented is regular.
- Cast
- :func:`cast <theano.sparse.basic.cast>` with ``bcast``, ``wcast``, ``icast``, ``lcast``,
``fcast``, ``dcast``, ``ccast``, and ``zcast``.
The grad implemented is regular.
- Transpose
- :func:`transpose <theano.sparse.basic.transpose>`.
The grad implemented is regular.
- Basic Arithmetic
- :func:`neg <theano.sparse.basic.neg>`.
The grad implemented is regular.
- :func:`eq <theano.sparse.basic.eq>`.
- :func:`neq <theano.sparse.basic.neq>`.
- :func:`gt <theano.sparse.basic.gt>`.
- :func:`ge <theano.sparse.basic.ge>`.
- :func:`lt <theano.sparse.basic.lt>`.
- :func:`le <theano.sparse.basic.le>`.
- :func:`add <theano.sparse.basic.add>`.
The grad implemented is regular.
- :func:`sub <theano.sparse.basic.sub>`.
The grad implemented is regular.
- :func:`mul <theano.sparse.basic.mul>`.
The grad implemented is regular.
- :func:`col_scale <theano.sparse.basic.col_scale>` to multiply by a vector along the columns.
The grad implemented is structured.
- :func:`row_scale <theano.sparse.basic.row_scale>` to multiply by a vector along the rows.
The grad implemented is structured.
- Monoid (Element-wise operation with only one sparse input).
`They all have a structured grad.`
- ``structured_sigmoid``
- ``structured_exp``
- ``structured_log``
- ``structured_pow``
- ``structured_minimum``
- ``structured_maximum``
- ``structured_add``
- ``sin``
- ``arcsin``
- ``tan``
- ``arctan``
- ``sinh``
- ``arcsinh``
- ``tanh``
- ``arctanh``
- ``rad2deg``
- ``deg2rad``
- ``rint``
- ``ceil``
- ``floor``
- ``trunc``
- ``sgn``
- ``log1p``
- ``expm1``
- ``sqr``
- ``sqrt``
- Dot Product
- :func:`dot <theano.sparse.basic.dot>`.
- One of the inputs must be sparse, the other sparse or dense.
- The grad implemented is regular.
- No C code for perform and no C code for grad.
- Returns a dense for perform and a dense for grad.
- :func:`structured_dot <theano.sparse.basic.structured_dot>`.
- The first input is sparse, the second can be sparse or dense.
- The grad implemented is structured.
- C code for perform and grad.
- It returns a sparse output if both inputs are sparse and
dense one if one of the inputs is dense.
- Returns a sparse grad for sparse inputs and dense grad for
dense inputs.
- :func:`true_dot <theano.sparse.basic.true_dot>`.
- The first input is sparse, the second can be sparse or dense.
- The grad implemented is regular.
- No C code for perform and no C code for grad.
- Returns a Sparse.
- The gradient returns a Sparse for sparse inputs and by
default a dense for dense inputs. The parameter
``grad_preserves_dense`` can be set to False to return a
sparse grad for dense inputs.
- :func:`sampling_dot <theano.sparse.basic.sampling_dot>`.
- Both inputs must be dense.
- The grad implemented is structured for `p`.
- Sample of the dot and sample of the gradient.
- C code for perform but not for grad.
- Returns sparse for perform and grad.
- :func:`usmm <theano.sparse.basic.usmm>`.
- You *shouldn't* insert this op yourself!
- There is an optimization that transform a
:func:`dot <theano.sparse.basic.dot>` to ``Usmm`` when possible.
- This op is the equivalent of gemm for sparse dot.
- There is no grad implemented for this op.
- One of the inputs must be sparse, the other sparse or dense.
- Returns a dense from perform.
- Slice Operations
- sparse_variable[N, N], returns a tensor scalar.
There is no grad implemented for this operation.
- sparse_variable[M:N, O:P], returns a sparse matrix
There is no grad implemented for this operation.
- Sparse variables don't support [M, N:O] and [M:N, O] as we don't
support sparse vectors and returning a sparse matrix would break
the numpy interface. Use [M:M+1, N:O] and [M:N, O:O+1] instead.
- :func:`diag <theano.sparse.basic.diag>`.
The grad implemented is regular.
- Concatenation
- :func:`hstack <theano.sparse.basic.hstack>`.
The grad implemented is regular.
- :func:`vstack <theano.sparse.basic.vstack>`.
The grad implemented is regular.
- Probability
`There is no grad implemented for these operations.`
- :class:`Poisson <theano.sparse.basic.Poisson>` and ``poisson``
- :class:`Binomial <theano.sparse.basic.Binomial>` and ``csc_fbinomial``, ``csc_dbinomial``
``csr_fbinomial``, ``csr_dbinomial``
- :class:`Multinomial <theano.sparse.basic.Multinomial>` and ``multinomial``
- Internal Representation
`They all have a regular grad implemented.`
- :func:`ensure_sorted_indices <theano.sparse.basic.ensure_sorted_indices>`.
- :func:`remove0 <theano.sparse.basic.remove0>`.
- :func:`clean <theano.sparse.basic.clean>` to resort indices and remove zeros
- To help testing
- :func:`theano.sparse.tests.test_basic.sparse_random_inputs`
===================================================================
:mod:`sparse` -- Sparse Op
===================================================================
.. module:: sparse
:platform: Unix, Windows
:synopsis: Sparse Op
.. moduleauthor:: LISA
.. automodule:: theano.sparse.basic
:members:
.. autofunction:: theano.sparse.tests.test_basic.sparse_random_inputs
|