提交 38e3c3b2 authored 作者: Iban Harlouchet's avatar Iban Harlouchet

numpydoc for theano/sparse/opt.py

上级 48ef7fd4
...@@ -19,8 +19,11 @@ _is_dense = sparse._is_dense ...@@ -19,8 +19,11 @@ _is_dense = sparse._is_dense
@gof.local_optimizer([csm_properties]) @gof.local_optimizer([csm_properties])
def local_csm_properties_csm(node): def local_csm_properties_csm(node):
"""if we find csm_properties(CSM(*args)), then we can replace that with the """
*args directly""" If we find csm_properties(CSM(*args)), then we can replace that with the
*args directly.
"""
if node.op == csm_properties: if node.op == csm_properties:
csm, = node.inputs csm, = node.inputs
if csm.owner and (csm.owner.op == CSC or csm.owner.op == CSR): if csm.owner and (csm.owner.op == CSC or csm.owner.op == CSR):
...@@ -39,6 +42,7 @@ register_specialize(local_csm_properties_csm) ...@@ -39,6 +42,7 @@ register_specialize(local_csm_properties_csm)
def local_inplace_remove0(node): def local_inplace_remove0(node):
""" """
Optimization to insert inplace versions of Remove0. Optimization to insert inplace versions of Remove0.
""" """
# If inplace is not enabled, enable it and replace that op with a # If inplace is not enabled, enable it and replace that op with a
# new op which has inplace enabled # new op which has inplace enabled
...@@ -56,15 +60,27 @@ theano.compile.optdb.register( ...@@ -56,15 +60,27 @@ theano.compile.optdb.register(
class AddSD_ccode(gof.op.Op): class AddSD_ccode(gof.op.Op):
"""Add a sparse and a dense matrix. """
Add a sparse and a dense matrix.
Parameters
----------
x
A sparse matrix.
y
A dense matrix
:param x: A sparse matrix. Returns
:param y: A dense matrix -------
matrix
`x`+`y`
:return: `x`+`y` Notes
-----
The grad implemented is structured on `x`.
:note: The grad implemented is structured on `x`.
""" """
__props__ = ("format", "inplace") __props__ = ("format", "inplace")
def __init__(self, format, inplace=False, *args, **kwargs): def __init__(self, format, inplace=False, *args, **kwargs):
...@@ -161,6 +177,7 @@ class AddSD_ccode(gof.op.Op): ...@@ -161,6 +177,7 @@ class AddSD_ccode(gof.op.Op):
def local_inplace_addsd_ccode(node): def local_inplace_addsd_ccode(node):
""" """
Optimization to insert inplace versions of AddSD. Optimization to insert inplace versions of AddSD.
""" """
if isinstance(node.op, sparse.AddSD) and theano.config.cxx: if isinstance(node.op, sparse.AddSD) and theano.config.cxx:
out_dtype = scalar.upcast(*node.inputs) out_dtype = scalar.upcast(*node.inputs)
...@@ -191,6 +208,7 @@ def local_dense_from_sparse_sparse_from_dense(node): ...@@ -191,6 +208,7 @@ def local_dense_from_sparse_sparse_from_dense(node):
def local_addsd_ccode(node): def local_addsd_ccode(node):
""" """
Convert AddSD to faster AddSD_ccode. Convert AddSD to faster AddSD_ccode.
""" """
if isinstance(node.op, sparse.AddSD) and theano.config.cxx: if isinstance(node.op, sparse.AddSD) and theano.config.cxx:
new_node = AddSD_ccode(format=node.inputs[0].type.format)(*node.inputs) new_node = AddSD_ccode(format=node.inputs[0].type.format)(*node.inputs)
...@@ -203,21 +221,31 @@ theano.compile.optdb.register('local_addsd_ccode', ...@@ -203,21 +221,31 @@ theano.compile.optdb.register('local_addsd_ccode',
class StructuredDotCSC(gof.Op): class StructuredDotCSC(gof.Op):
"""Structured Dot CSC is like dot, except that only the """
gradient wrt non-zero elements of the sparse matrix Structured Dot CSC is like dot, except that only the gradient wrt non-zero
`a` are calculated and propagated. elements of the sparse matrix `a` are calculated and propagated.
The output is presumed to be a dense matrix, and is represented by a The output is presumed to be a dense matrix, and is represented by a
TensorType instance. TensorType instance.
:param a: A sparse matrix in csc format. Parameters
:param b: A sparse or dense matrix. ----------
a
A sparse matrix in csc format.
b
A sparse or dense matrix.
:return: The dot product of `a` and `b`. Returns
-------
The dot product of `a` and `b`.
Notes
-----
The grad implemented is structured.
This op is used as an optimization for StructuredDot.
:note: The grad implemented is structured.
:note: This op is used as an optimization for StructuredDot.
""" """
__props__ = () __props__ = ()
def make_node(self, a_val, a_ind, a_ptr, a_nrows, b): def make_node(self, a_val, a_ind, a_ptr, a_nrows, b):
...@@ -389,20 +417,31 @@ sd_csc = StructuredDotCSC() ...@@ -389,20 +417,31 @@ sd_csc = StructuredDotCSC()
class StructuredDotCSR(gof.Op): class StructuredDotCSR(gof.Op):
"""Structured Dot CSR is like dot, except that only the """
Structured Dot CSR is like dot, except that only the
gradient wrt non-zero elements of the sparse matrix gradient wrt non-zero elements of the sparse matrix
`a` are calculated and propagated. `a` are calculated and propagated.
The output is presumed to be a dense matrix, and is represented by a The output is presumed to be a dense matrix, and is represented by a
TensorType instance. TensorType instance.
:param a: A sparse matrix in csr format. Parameters
:param b: A sparse or dense matrix. ----------
a
A sparse matrix in csr format.
b
A sparse or dense matrix.
:return: The dot product of `a` and `b`. Returns
-------
matrix
The dot product of `a` and `b`.
Notes
-----
The grad implemented is structured.
This op is used as an optimization for StructuredDot.
:note: The grad implemented is structured.
:note: This op is used as an optimization for StructuredDot.
""" """
__props__ = () __props__ = ()
...@@ -427,17 +466,27 @@ class StructuredDotCSR(gof.Op): ...@@ -427,17 +466,27 @@ class StructuredDotCSR(gof.Op):
def c_code(self, node, name, inputs, outputs, sub): def c_code(self, node, name, inputs, outputs, sub):
""" """
C-implementation of the dot product of the sparse matrix A and matrix C-implementation of the dot product of the sparse matrix A and matrix B.
B.
@param a_val: non-zero values of the sparse matrix Parameters
@param a_ind: column indices of the non-null values (.indices of a ----------
scipy.csc_matrix) a_val
@param a_ptr: a_ptr indicates col indices for col. i are in the range Non-zero values of the sparse matrix.
a_ptr[i]:a_ptr[i+1] a_ind
@param n_cols: number of columns of sparse matrix Column indices of the non-null values (.indices of a
@param b: dense matrix to perform dot product with, as in dot(a, b) scipy.csc_matrix).
@param z: return value a_ptr
@param sub: TODO, not too sure, something to do with weave probably Indicates col indices for col. i are in the range
a_ptr[i]:a_ptr[i+1].
n_cols
Number of columns of sparse matrix.
b
Dense matrix to perform dot product with, as in dot(a, b).
z
Return value.
sub
TODO, not too sure, something to do with weave probably.
""" """
(a_val, a_ind, a_ptr, b) = inputs (a_val, a_ind, a_ptr, b) = inputs
(z,) = outputs (z,) = outputs
...@@ -569,19 +618,30 @@ def local_structured_dot(node): ...@@ -569,19 +618,30 @@ def local_structured_dot(node):
class UsmmCscDense(gof.Op): class UsmmCscDense(gof.Op):
"""Performs the expression is `alpha` * `x` `y` + `z`.
:param x: Matrix variable.
:param y: Matrix variable.
:param z: Dense matrix.
:param alpha: A tensor scalar.
:return: The dense matrix resulting from `alpha` * `x` `y` + `z`.
:note: The grad is not implemented for this op.
:note: Optimized version os Usmm when `x` is in csc format and
`y` is dense.
""" """
Performs the expression is `alpha` * `x` `y` + `z`.
Parameters
----------
x
Matrix variable.
y
Matrix variable.
z
Dense matrix.
alpha
A tensor scalar.
Returns
-------
The dense matrix resulting from `alpha` * `x` `y` + `z`.
Notes
-----
The grad is not implemented for this op.
Optimized version os Usmm when `x` is in csc format and `y` is dense.
"""
__props__ = ("inplace",) __props__ = ("inplace",)
def __init__(self, inplace): def __init__(self, inplace):
...@@ -837,7 +897,10 @@ register_specialize(local_usmm_csc_dense_inplace, 'cxx_only', 'inplace') ...@@ -837,7 +897,10 @@ register_specialize(local_usmm_csc_dense_inplace, 'cxx_only', 'inplace')
# This is tested in tests/test_basic.py:UsmmTests # This is tested in tests/test_basic.py:UsmmTests
@gof.local_optimizer([usmm]) @gof.local_optimizer([usmm])
def local_usmm_csx(node): def local_usmm_csx(node):
""" usmm -> usmm_csc_dense """ """
usmm -> usmm_csc_dense
"""
if node.op == usmm: if node.op == usmm:
alpha, x, y, z = node.inputs alpha, x, y, z = node.inputs
...@@ -987,7 +1050,10 @@ csm_grad_c = CSMGradC() ...@@ -987,7 +1050,10 @@ csm_grad_c = CSMGradC()
# This is tested in tests/test_opt.py:test_local_csm_grad_c # This is tested in tests/test_opt.py:test_local_csm_grad_c
@gof.local_optimizer([csm_grad(None)]) @gof.local_optimizer([csm_grad(None)])
def local_csm_grad_c(node): def local_csm_grad_c(node):
""" csm_grad(None) -> csm_grad_c """ """
csm_grad(None) -> csm_grad_c
"""
if node.op == csm_grad(None): if node.op == csm_grad(None):
return [csm_grad_c(*node.inputs)] return [csm_grad_c(*node.inputs)]
return False return False
...@@ -996,22 +1062,37 @@ def local_csm_grad_c(node): ...@@ -996,22 +1062,37 @@ def local_csm_grad_c(node):
class MulSDCSC(gof.Op): class MulSDCSC(gof.Op):
"""Multiplication of sparse matrix by a broadcasted dense vector """
Multiplication of sparse matrix by a broadcasted dense vector
element wise. element wise.
:param a_data: Sparse matrix data. Parameters
:param a_indices: Sparse matrix indices. ----------
:param a_indptr: Sparse matrix indptr. a_data
:param b: Tensor type matrix. Sparse matrix data.
a_indices
Sparse matrix indices.
a_indptr
Sparse matrix indptr.
b
Tensor type matrix.
:return: The multiplication of the two matrix element wise. Returns
-------
The multiplication of the two matrices element-wise.
Notes
-----
`a_data`, `a_indices` and `a_indptr` must be the properties of a sparse
matrix in csc format.
The dtype of `a_data`, i.e. the dtype of the sparse matrix, cannot be a
complex type.
This op is used as an optimization of mul_s_d.
:note: `a_data`, `a_indices` and `a_indptr` must be the properties
of a sparse matrix in csc format.
:note: The dtype of `a_data`, i.e. the dtype of the sparse matrix,
cannot be a complex type.
:note: This op is used as an optimization of mul_s_d.
""" """
__props__ = () __props__ = ()
def make_node(self, a_data, a_indices, a_indptr, b): def make_node(self, a_data, a_indices, a_indptr, b):
...@@ -1108,21 +1189,35 @@ mul_s_d_csc = MulSDCSC() ...@@ -1108,21 +1189,35 @@ mul_s_d_csc = MulSDCSC()
class MulSDCSR(gof.Op): class MulSDCSR(gof.Op):
"""Multiplication of sparse matrix by a broadcasted dense vector """
Multiplication of sparse matrix by a broadcasted dense vector
element wise. element wise.
:param a_data: Sparse matrix data. Parameters
:param a_indices: Sparse matrix indices. ----------
:param a_indptr: Sparse matrix indptr. a_data
:param b: Tensor type matrix. Sparse matrix data.
a_indices
:return: The multiplication of the two matrix element wise. Sparse matrix indices.
a_indptr
:note: `a_data`, `a_indices` and `a_indptr` must be the properties Sparse matrix indptr.
b
Tensor type matrix.
Returns
-------
The multiplication of the two matrix element wise.
Notes
-----
`a_data`, `a_indices` and `a_indptr` must be the properties
of a sparse matrix in csr format. of a sparse matrix in csr format.
:note: The dtype of `a_data`, i.e. the dtype of the sparse matrix,
The dtype of `a_data`, i.e. the dtype of the sparse matrix,
cannot be a complex type. cannot be a complex type.
:note: This op is used as an optimization of mul_s_d.
This op is used as an optimization of mul_s_d.
""" """
__props__ = () __props__ = ()
...@@ -1262,21 +1357,35 @@ register_specialize(local_mul_s_d, 'cxx_only') ...@@ -1262,21 +1357,35 @@ register_specialize(local_mul_s_d, 'cxx_only')
class MulSVCSR(gof.Op): class MulSVCSR(gof.Op):
"""Multiplication of sparse matrix by a broadcasted dense vector """
Multiplication of sparse matrix by a broadcasted dense vector
element wise. element wise.
:param a_data: Sparse matrix data. Parameters
:param a_indices: Sparse matrix indices. ----------
:param a_indptr: Sparse matrix indptr. a_data
:param b: Tensor type matrix. Sparse matrix data.
a_indices
:return: The multiplication of the two matrix element wise. Sparse matrix indices.
a_indptr
:note: `a_data`, `a_indices` and `a_indptr` must be the properties Sparse matrix indptr.
b
Tensor type matrix.
Returns
-------
The multiplication of the two matrix element wise.
Notes
-----
`a_data`, `a_indices` and `a_indptr` must be the properties
of a sparse matrix in csr format. of a sparse matrix in csr format.
:note: The dtype of `a_data`, i.e. the dtype of the sparse matrix,
The dtype of `a_data`, i.e. the dtype of the sparse matrix,
cannot be a complex type. cannot be a complex type.
:note: This op is used as an optimization of MulSV.
This op is used as an optimization of MulSV.
""" """
__props__ = () __props__ = ()
...@@ -1399,23 +1508,36 @@ register_specialize(local_mul_s_v, 'cxx_only') ...@@ -1399,23 +1508,36 @@ register_specialize(local_mul_s_v, 'cxx_only')
class StructuredAddSVCSR(gof.Op): class StructuredAddSVCSR(gof.Op):
"""Structured addition of a sparse matrix and a dense vector. """
Structured addition of a sparse matrix and a dense vector.
The elements of the vector are are only added to the corresponding The elements of the vector are are only added to the corresponding
non-zero elements. Therefore, this operation outputs another sparse non-zero elements. Therefore, this operation outputs another sparse
matrix. matrix.
:param a_data: Sparse matrix data. Parameters
:param a_indices: Sparse matrix indices. ----------
:param a_indptr: Sparse matrix indptr. a_data
:param b: Tensor type vector. Sparse matrix data.
a_indices
Sparse matrix indices.
a_indptr
Sparse matrix indptr.
b
Tensor type vector.
Returns
-------
A sparse matrix containing the addition of the vector to the data of the
sparse matrix.
:return: A sparse matrix containing the addition of the vector to Notes
the data of the sparse matrix. -----
The a_* are the properties of a sparse matrix in csr format.
This op is used as an optimization for StructuredAddSV.
:note: The a_* are the properties of a sparse matrix in csr
format.
:note: This op is used as an optimization for StructuredAddSV.
""" """
__props__ = () __props__ = ()
def make_node(self, a_data, a_indices, a_indptr, b): def make_node(self, a_data, a_indices, a_indptr, b):
...@@ -1553,7 +1675,8 @@ register_specialize(local_structured_add_s_v, 'cxx_only') ...@@ -1553,7 +1675,8 @@ register_specialize(local_structured_add_s_v, 'cxx_only')
class SamplingDotCSR(gof.Op): class SamplingDotCSR(gof.Op):
"""Operand optimized for calculating the dot product dot(`x`, `y`.T) = `z` """
Operand optimized for calculating the dot product dot(`x`, `y`.T) = `z`
when you only want to calculate a subset of `z`. when you only want to calculate a subset of `z`.
It is equivalent to `p` o (`x` . `y`.T) where o is the element-wise It is equivalent to `p` o (`x` . `y`.T) where o is the element-wise
...@@ -1563,28 +1686,41 @@ class SamplingDotCSR(gof.Op): ...@@ -1563,28 +1686,41 @@ class SamplingDotCSR(gof.Op):
interface than `dot` because SamplingDot requires `x` to be a `m`x`k` interface than `dot` because SamplingDot requires `x` to be a `m`x`k`
matrix while `y` is a `n`x`k` matrix instead of the usual `k`x`n` matrix. matrix while `y` is a `n`x`k` matrix instead of the usual `k`x`n` matrix.
.. note:: Parameters
----------
x
Tensor matrix.
y
Tensor matrix.
p_data
Sparse matrix data.
p_ind
Sparse matrix indices.
p_ptr
Sparse matric indptr.
p_ncols
Sparse matrix number of columns.
Returns
-------
A dense matrix containing the dot product of `x` by `y`.T only
where `p` is 1.
Notes
-----
It will work if the pattern is not binary value, but if the It will work if the pattern is not binary value, but if the
pattern doesn't have a high sparsity proportion it will be slower pattern doesn't have a high sparsity proportion it will be slower
then a more optimized dot followed by a normal elemwise then a more optimized dot followed by a normal elemwise
multiplication. multiplication.
:param x: Tensor matrix. If we have the input of mixed dtype, we insert cast elemwise
:param y: Tensor matrix.
:param p_data: Sparse matrix data.
:param p_ind: Sparse matrix indices.
:param p_ptr: Sparse matric indptr.
:param p_ncols: Sparse matrix number of columns.
:return: A dense matrix containing the dot product of `x` by `y`.T only
where `p` is 1.
:note: If we have the input of mixed dtype, we insert cast elemwise
in the graph to be able to call blas function as they don't in the graph to be able to call blas function as they don't
allow mixed dtype. allow mixed dtype.
:note: This op is used as an optimization for SamplingDot.
This op is used as an optimization for SamplingDot.
""" """
__props__ = () __props__ = ()
def make_node(self, x, y, p_data, p_ind, p_ptr, p_ncols): def make_node(self, x, y, p_data, p_ind, p_ptr, p_ncols):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论