提交 bb5a2ee7 authored 作者: Nicolas Bouchard's avatar Nicolas Bouchard

Tutorial correction.

上级 245adb5e
......@@ -25,7 +25,8 @@ since not only the actual data is stored, but also the position of nearly every
element of the matrix. This would also require more computation
time whereas a dense matrix representation along with regular optimized algorithms might do a
better job. Other examples may be found at the nexus of the specific purpose and structure
of the matrices.
of the matrices. More documentation may be found in the
`SciPy Sparse Reference <http://docs.scipy.org/doc/scipy/reference/sparse.html>`_.
Since sparse matrices are not stored in contiguous arrays, there are several
ways to represent them in memory. This is usually designated by the so-called ``format``
......@@ -37,10 +38,11 @@ arrays with a number of dimensions different from two.
So far, Theano implements two ``formats`` of sparse matrix: ``csc`` and ``csr``.
Those are almost identical except that ``csc`` is based on the *columns* of the
matrix and ``csr`` is based on its *rows*. They both have the same purpose:
to provide for the use of efficient algorithms performing linear algebra operations. A disadvantage is that
they fail to ensure easy access to the elements of the underlying matrix. This means that if you are planning
to access elements of a sparse matrix a lot in your computational graph, perhaps
a tensor variable could be a better choice.
to provide for the use of efficient algorithms performing linear algebra operations.
A disadvantage is that they fail to give an efficient way to modify the sparsity structure
of the underlying matrix, i.e. adding new elements. This means that if you are
planning to add new elements in a sparse matrix very often in your computational graph,
perhaps a tensor variable could be a better choice.
More documentation may be found in the :ref:`Sparse Library Reference <libdoc_sparse>`.
......@@ -66,8 +68,7 @@ tutorial:
>>> import theano
>>> import numpy as np
>>> import scipy.sparse as sp
>>> from theano import tensor as T
>>> from theano import sparse as S
>>> from theano import sparse
CSC Matrix
----------
......@@ -131,7 +132,7 @@ since the sparse package does not provide any way to handle a number of
dimensions different from two. The set of all accepted ``dtype`` for the sparse
matrices can be found in ``sparse.all_dtypes``.
>>> S.all_dtypes
>>> sparse.all_dtypes
set(['int32', 'int16', 'float64', 'complex128', 'complex64', 'int64', 'int8', 'float32'])
Properties and Construction
......@@ -147,9 +148,9 @@ and ``CSR`` can be used. This will create the sparse matrix in the desired
format. As an example, the following code reconstructs a ``csc`` matrix into
a ``csr`` one.
>>> x = S.csc_matrix(name='x', dtype='int64')
>>> data, indices, indptr, shape = S.csm_properties(x)
>>> y = S.CSR(data, indices, indptr, shape)
>>> x = sparse.csc_matrix(name='x', dtype='int64')
>>> data, indices, indptr, shape = sparse.csm_properties(x)
>>> y = sparse.CSR(data, indices, indptr, shape)
>>> f = theano.function([x], y)
>>> a = sp.csc_matrix(np.asarray([[0, 1, 1], [0, 0, 0], [1, 0, 0]]))
>>> print a.toarray()
......@@ -174,9 +175,9 @@ provides the ``dense_from_sparse``, ``csr_from_dense`` and
``csc_from_dense`` functions. No additional detail must be provided. Here is
an example that performs a full cycle from sparse to sparse:
>>> x = S.csc_matrix(name='x', dtype='float32')
>>> y = S.dense_from_sparse(x)
>>> z = S.csc_from_dense(y)
>>> x = sparse.csc_matrix(name='x', dtype='float32')
>>> y = sparse.dense_from_sparse(x)
>>> z = sparse.csc_from_dense(y)
Structured Operation
--------------------
......@@ -186,8 +187,8 @@ matrices. These ops are said to be *structured* and simply do not perform any
computations on the zero elements of the sparse matrix. They can be thought as being
applied only to the data attribute of the latter.
>>> x = S.csc_matrix(name='x', dtype='float32')
>>> y = S.structured_add(x, 2)
>>> x = sparse.csc_matrix(name='x', dtype='float32')
>>> y = sparse.structured_add(x, 2)
>>> f = theano.function([x], y)
>>> a = sp.csc_matrix(np.asarray([[0, 0, -1], [0, -2, 1], [3, 0, 0]], dtype='float32'))
>>> print a.toarray()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论