Class LinalgSparseOps
-
Method Summary
Modifier and TypeMethodDescription<T extends TType>
CSRSparseMatrixComponents<T> cSRSparseMatrixComponents(Operand<? extends TType> csrSparseMatrix, Operand<TInt32> index, Class<T> type) Reads out the CSR components at batchindex.<T extends TType>
CSRSparseMatrixToDense<T> cSRSparseMatrixToDense(Operand<? extends TType> sparseInput, Class<T> type) Convert a (possibly batched) CSRSparseMatrix to dense.<T extends TType>
CSRSparseMatrixToSparseTensor<T> cSRSparseMatrixToSparseTensor(Operand<? extends TType> sparseMatrix, Class<T> type) Converts a (possibly batched) CSRSparesMatrix to a SparseTensor.denseToCSRSparseMatrix(Operand<? extends TType> denseInput, Operand<TInt64> indices) Converts a dense tensor to a (possibly batched) CSRSparseMatrix.final Opsops()Get the parentOpsobject.<T extends TType>
SparseMatrixAddsparseMatrixAdd(Operand<? extends TType> a, Operand<? extends TType> b, Operand<T> alpha, Operand<T> beta) Sparse addition of two CSR matrices, C = alpha * A + beta * B.<T extends TType>
SparseMatrixMatMul<T> sparseMatrixMatMul(Operand<? extends TType> a, Operand<T> b, SparseMatrixMatMul.Options... options) Matrix-multiplies a sparse matrix with a dense matrix.sparseMatrixMul(Operand<? extends TType> a, Operand<? extends TType> b) Element-wise multiplication of a sparse matrix with a dense tensor.sparseMatrixNNZ(Operand<? extends TType> sparseMatrix) Returns the number of nonzeroes ofsparse_matrix.sparseMatrixOrderingAMD(Operand<? extends TType> input) Computes the Approximate Minimum Degree (AMD) ordering ofinput.<T extends TNumber>
SparseMatrixSoftmaxsparseMatrixSoftmax(Operand<? extends TType> logits, Class<T> type) Calculates the softmax of a CSRSparseMatrix.<T extends TNumber>
SparseMatrixSoftmaxGradsparseMatrixSoftmaxGrad(Operand<? extends TType> softmax, Operand<? extends TType> gradSoftmax, Class<T> type) Calculates the gradient of the SparseMatrixSoftmax op.<T extends TType>
SparseMatrixSparseCholeskysparseMatrixSparseCholesky(Operand<? extends TType> input, Operand<TInt32> permutation, Class<T> type) Computes the sparse Cholesky decomposition ofinput.<T extends TType>
SparseMatrixSparseMatMulsparseMatrixSparseMatMul(Operand<? extends TType> a, Operand<? extends TType> b, Class<T> type, SparseMatrixSparseMatMul.Options... options) Sparse-matrix-multiplies two CSR matricesaandb.<T extends TType>
SparseMatrixTransposesparseMatrixTranspose(Operand<? extends TType> input, Class<T> type, SparseMatrixTranspose.Options... options) Transposes the inner (matrix) dimensions of a CSRSparseMatrix.<T extends TType>
SparseMatrixZerossparseMatrixZeros(Operand<TInt64> denseShape, Class<T> type) Creates an all-zeros CSRSparseMatrix with shapedense_shape.sparseTensorToCSRSparseMatrix(Operand<TInt64> indices, Operand<? extends TType> values, Operand<TInt64> denseShape) Converts a SparseTensor to a (possibly batched) CSRSparseMatrix.
-
Method Details
-
cSRSparseMatrixComponents
public <T extends TType> CSRSparseMatrixComponents<T> cSRSparseMatrixComponents(Operand<? extends TType> csrSparseMatrix, Operand<TInt32> index, Class<T> type) Reads out the CSR components at batchindex. This op is meant only for debugging / testing, and its interface is not expected to be stable.- Type Parameters:
T- data type forCSRSparseMatrixComponentsoutput and operands- Parameters:
csrSparseMatrix- A batched CSRSparseMatrix.index- The index incsr_sparse_matrix's batch.type- The value of the type attribute- Returns:
- a new instance of CSRSparseMatrixComponents
-
cSRSparseMatrixToDense
public <T extends TType> CSRSparseMatrixToDense<T> cSRSparseMatrixToDense(Operand<? extends TType> sparseInput, Class<T> type) Convert a (possibly batched) CSRSparseMatrix to dense.- Type Parameters:
T- data type forCSRSparseMatrixToDenseoutput and operands- Parameters:
sparseInput- A batched CSRSparseMatrix.type- The value of the type attribute- Returns:
- a new instance of CSRSparseMatrixToDense
-
cSRSparseMatrixToSparseTensor
public <T extends TType> CSRSparseMatrixToSparseTensor<T> cSRSparseMatrixToSparseTensor(Operand<? extends TType> sparseMatrix, Class<T> type) Converts a (possibly batched) CSRSparesMatrix to a SparseTensor.- Type Parameters:
T- data type forCSRSparseMatrixToSparseTensoroutput and operands- Parameters:
sparseMatrix- A (possibly batched) CSRSparseMatrix.type- The value of the type attribute- Returns:
- a new instance of CSRSparseMatrixToSparseTensor
-
denseToCSRSparseMatrix
public DenseToCSRSparseMatrix denseToCSRSparseMatrix(Operand<? extends TType> denseInput, Operand<TInt64> indices) Converts a dense tensor to a (possibly batched) CSRSparseMatrix.- Parameters:
denseInput- A Dense tensor.indices- Indices of nonzero elements.- Returns:
- a new instance of DenseToCSRSparseMatrix
-
sparseMatrixAdd
public <T extends TType> SparseMatrixAdd sparseMatrixAdd(Operand<? extends TType> a, Operand<? extends TType> b, Operand<T> alpha, Operand<T> beta) Sparse addition of two CSR matrices, C = alpha * A + beta * B. The gradients of SparseMatrixAdd outputs with respect to alpha and beta are not currently defined (TensorFlow will return zeros for these entries).- Type Parameters:
T- data type forSparseMatrixAddoutput and operands- Parameters:
a- A CSRSparseMatrix.b- A CSRSparseMatrix.alpha- A constant scalar.beta- A constant scalar.- Returns:
- a new instance of SparseMatrixAdd
-
sparseMatrixMatMul
public <T extends TType> SparseMatrixMatMul<T> sparseMatrixMatMul(Operand<? extends TType> a, Operand<T> b, SparseMatrixMatMul.Options... options) Matrix-multiplies a sparse matrix with a dense matrix. Returns a dense matrix. For inputs A and B, where A is CSR and B is dense; this op returns a dense C;If transpose_output is false, returns:
C = A . B
If transpose_output is
true, returns:C = transpose(A . B) = transpose(B) . transpose(A)
where the transposition is performed along the two innermost (matrix) dimensions.
If conjugate_output is
true, returns:C = conjugate(A . B) = conjugate(A) . conjugate(B)
If both conjugate_output and transpose_output are
true, returns:C = conjugate(transpose(A . B)) = conjugate(transpose(B)) . conjugate(transpose(A))- Type Parameters:
T- data type forSparseMatrixMatMuloutput and operands- Parameters:
a- A CSRSparseMatrix.b- A dense tensor.options- carries optional attribute values- Returns:
- a new instance of SparseMatrixMatMul
-
sparseMatrixMul
Element-wise multiplication of a sparse matrix with a dense tensor. Returns a sparse matrix.The dense tensor
bmay be either a scalar; otherwiseamust be a rank-3SparseMatrix; in this casebmust be shaped[batch_size, 1, 1]and the multiply operation broadcasts.NOTE even if
bis zero, the sparsity structure of the output does not change.- Parameters:
a- A CSRSparseMatrix.b- A dense tensor.- Returns:
- a new instance of SparseMatrixMul
-
sparseMatrixNNZ
Returns the number of nonzeroes ofsparse_matrix.- Parameters:
sparseMatrix- A CSRSparseMatrix.- Returns:
- a new instance of SparseMatrixNNZ
-
sparseMatrixOrderingAMD
Computes the Approximate Minimum Degree (AMD) ordering ofinput. Computes the Approximate Minimum Degree (AMD) ordering for a sparse matrix.The returned permutation may be used to permute the rows and columns of the given sparse matrix. This typically results in permuted sparse matrix's sparse Cholesky (or other decompositions) in having fewer zero fill-in compared to decomposition of the original matrix.
The input sparse matrix may have rank 2 or rank 3. The output Tensor, representing would then have rank 1 or 2 respectively, with the same batch shape as the input.
Each component of the input sparse matrix must represent a square symmetric matrix; only the lower triangular part of the matrix is read. The values of the sparse matrix does not affect the returned permutation, only the sparsity pattern of the sparse matrix is used. Hence, a single AMD ordering may be reused for the Cholesky decompositions of sparse matrices with the same sparsity pattern but with possibly different values.
Each batch component of the output permutation represents a permutation of
Nelements, where the input sparse matrix components each haveNrows. That is, the component contains each of the integers{0, .. N-1}exactly once. Theith element represents the row index that theith row maps to.Usage example:
from tensorflow.python.ops.linalg.sparse import sparse_csr_matrix_ops a_indices = np.array([[0, 0], [1, 1], [2, 1], [2, 2], [3, 3]]) a_values = np.array([1.0, 2.0, 1.0, 3.0, 4.0], np.float32) a_dense_shape = [4, 4] with tf.Session() as sess: # Define (COO format) SparseTensor over Numpy array. a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape) # Convert SparseTensors to CSR SparseMatrix. a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( a_st.indices, a_st.values, a_st.dense_shape) # Obtain the AMD Ordering for the CSR SparseMatrix. ordering_amd = sparse_csr_matrix_ops.sparse_matrix_ordering_amd(sparse_matrix) ordering_amd_value = sess.run(ordering_amd)ordering_amd_valuestores the AMD ordering:[1 2 3 0].input: A
CSRSparseMatrix.- Parameters:
input- ACSRSparseMatrix.- Returns:
- a new instance of SparseMatrixOrderingAMD
-
sparseMatrixSoftmax
public <T extends TNumber> SparseMatrixSoftmax sparseMatrixSoftmax(Operand<? extends TType> logits, Class<T> type) Calculates the softmax of a CSRSparseMatrix. Calculate the softmax of the innermost dimensions of a SparseMatrix.Missing values are treated as
-inf(i.e., logits of zero probability); and the output has the same sparsity structure as the input (though missing values in the output may now be treated as having probability zero).- Type Parameters:
T- data type forSparseMatrixSoftmaxoutput and operands- Parameters:
logits- A CSRSparseMatrix.type- The value of the type attribute- Returns:
- a new instance of SparseMatrixSoftmax
-
sparseMatrixSoftmaxGrad
public <T extends TNumber> SparseMatrixSoftmaxGrad sparseMatrixSoftmaxGrad(Operand<? extends TType> softmax, Operand<? extends TType> gradSoftmax, Class<T> type) Calculates the gradient of the SparseMatrixSoftmax op.- Type Parameters:
T- data type forSparseMatrixSoftmaxGradoutput and operands- Parameters:
softmax- A CSRSparseMatrix.gradSoftmax- The gradient ofsoftmax.type- The value of the type attribute- Returns:
- a new instance of SparseMatrixSoftmaxGrad
-
sparseMatrixSparseCholesky
public <T extends TType> SparseMatrixSparseCholesky sparseMatrixSparseCholesky(Operand<? extends TType> input, Operand<TInt32> permutation, Class<T> type) Computes the sparse Cholesky decomposition ofinput. Computes the Sparse Cholesky decomposition of a sparse matrix, with the given fill-in reducing permutation.The input sparse matrix and the fill-in reducing permutation
permutationmust have compatible shapes. If the sparse matrix has rank 3; with the batch dimensionB, then thepermutationmust be of rank 2; with the same batch dimensionB. There is no support for broadcasting.Furthermore, each component vector of
permutationmust be of lengthN, containing each of the integers {0, 1, ..., N - 1} exactly once, whereNis the number of rows of each component of the sparse matrix.Each component of the input sparse matrix must represent a symmetric positive definite (SPD) matrix; although only the lower triangular part of the matrix is read. If any individual component is not SPD, then an InvalidArgument error is thrown.
The returned sparse matrix has the same dense shape as the input sparse matrix. For each component
Aof the input sparse matrix, the corresponding output sparse matrix representsL, the lower triangular Cholesky factor satisfying the following identity:A = L * Lt
where Lt denotes the transpose of L (or its conjugate transpose, if
typeiscomplex64orcomplex128).The
typeparameter denotes the type of the matrix elements. The supported types are:float32,float64,complex64andcomplex128.Usage example:
from tensorflow.python.ops.linalg.sparse import sparse_csr_matrix_ops a_indices = np.array([[0, 0], [1, 1], [2, 1], [2, 2], [3, 3]]) a_values = np.array([1.0, 2.0, 1.0, 3.0, 4.0], np.float32) a_dense_shape = [4, 4] with tf.Session() as sess: # Define (COO format) SparseTensor over Numpy array. a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape) # Convert SparseTensors to CSR SparseMatrix. a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( a_st.indices, a_st.values, a_st.dense_shape) # Obtain the Sparse Cholesky factor using AMD Ordering for reducing zero # fill-in (number of structural non-zeros in the sparse Cholesky factor). ordering_amd = sparse_csr_matrix_ops.sparse_matrix_ordering_amd(sparse_matrix) cholesky_sparse_matrices = ( sparse_csr_matrix_ops.sparse_matrix_sparse_cholesky( sparse_matrix, ordering_amd, type=tf.float32)) # Convert the CSRSparseMatrix Cholesky factor to a dense Tensor dense_cholesky = sparse_csr_matrix_ops.csr_sparse_matrix_to_dense( cholesky_sparse_matrices, tf.float32) # Evaluate the dense Tensor value. dense_cholesky_value = sess.run(dense_cholesky)dense_cholesky_valuestores the dense Cholesky factor:[[ 1. 0. 0. 0.] [ 0. 1.41 0. 0.] [ 0. 0.70 1.58 0.] [ 0. 0. 0. 2.]]input: A
CSRSparseMatrix. permutation: ATensor. type: The type ofinput.- Type Parameters:
T- data type forSparseMatrixSparseCholeskyoutput and operands- Parameters:
input- ACSRSparseMatrix.permutation- A fill-in reducing permutation matrix.type- The value of the type attribute- Returns:
- a new instance of SparseMatrixSparseCholesky
-
sparseMatrixSparseMatMul
public <T extends TType> SparseMatrixSparseMatMul sparseMatrixSparseMatMul(Operand<? extends TType> a, Operand<? extends TType> b, Class<T> type, SparseMatrixSparseMatMul.Options... options) Sparse-matrix-multiplies two CSR matricesaandb. Performs a matrix multiplication of a sparse matrixawith a sparse matrixb; returns a sparse matrixa * b, unless eitheraorbis transposed or adjointed.Each matrix may be transposed or adjointed (conjugated and transposed) according to the Boolean parameters
transpose_a,adjoint_a,transpose_bandadjoint_b. At most one oftranspose_aoradjoint_amay be True. Similarly, at most one oftranspose_boradjoint_bmay be True.The inputs must have compatible shapes. That is, the inner dimension of
amust be equal to the outer dimension ofb. This requirement is adjusted according to whether eitheraorbis transposed or adjointed.The
typeparameter denotes the type of the matrix elements. Bothaandbmust have the same type. The supported types are:float32,float64,complex64andcomplex128.Both
aandbmust have the same rank. Broadcasting is not supported. If they have rank 3, each batch of 2D CSRSparseMatrices withinaandbmust have the same dense shape.The sparse matrix product may have numeric (non-structural) zeros. TODO(anudhyan): Consider adding a boolean attribute to control whether to prune zeros.
Usage example:
from tensorflow.python.ops.linalg.sparse import sparse_csr_matrix_ops a_indices = np.array([[0, 0], [2, 3], [2, 4], [3, 0]]) a_values = np.array([1.0, 5.0, -1.0, -2.0], np.float32) a_dense_shape = [4, 5] b_indices = np.array([[0, 0], [3, 0], [3, 1]]) b_values = np.array([2.0, 7.0, 8.0], np.float32) b_dense_shape = [5, 3] with tf.Session() as sess: # Define (COO format) Sparse Tensors over Numpy arrays a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape) b_st = tf.sparse.SparseTensor(b_indices, b_values, b_dense_shape) # Convert SparseTensors to CSR SparseMatrix a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( a_st.indices, a_st.values, a_st.dense_shape) b_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( b_st.indices, b_st.values, b_st.dense_shape) # Compute the CSR SparseMatrix matrix multiplication c_sm = sparse_csr_matrix_ops.sparse_matrix_sparse_mat_mul( a=a_sm, b=b_sm, type=tf.float32) # Convert the CSR SparseMatrix product to a dense Tensor c_sm_dense = sparse_csr_matrix_ops.csr_sparse_matrix_to_dense( c_sm, tf.float32) # Evaluate the dense Tensor value c_sm_dense_value = sess.run(c_sm_dense)c_sm_dense_valuestores the dense matrix product:[[ 2. 0. 0.] [ 0. 0. 0.] [ 35. 40. 0.] [ -4. 0. 0.]]a: A
CSRSparseMatrix. b: ACSRSparseMatrixwith the same type and rank asa. type: The type of bothaandb. transpose_a: If True,atransposed before multiplication. transpose_b: If True,btransposed before multiplication. adjoint_a: If True,aadjointed before multiplication. adjoint_b: If True,badjointed before multiplication.- Type Parameters:
T- data type forSparseMatrixSparseMatMuloutput and operands- Parameters:
a- A CSRSparseMatrix.b- A CSRSparseMatrix.type- The value of the type attributeoptions- carries optional attribute values- Returns:
- a new instance of SparseMatrixSparseMatMul
-
sparseMatrixTranspose
public <T extends TType> SparseMatrixTranspose sparseMatrixTranspose(Operand<? extends TType> input, Class<T> type, SparseMatrixTranspose.Options... options) Transposes the inner (matrix) dimensions of a CSRSparseMatrix. Transposes the inner (matrix) dimensions of a SparseMatrix and optionally conjugates its values.- Type Parameters:
T- data type forSparseMatrixTransposeoutput and operands- Parameters:
input- A CSRSparseMatrix.type- The value of the type attributeoptions- carries optional attribute values- Returns:
- a new instance of SparseMatrixTranspose
-
sparseMatrixZeros
public <T extends TType> SparseMatrixZeros sparseMatrixZeros(Operand<TInt64> denseShape, Class<T> type) Creates an all-zeros CSRSparseMatrix with shapedense_shape.- Type Parameters:
T- data type forSparseMatrixZerosoutput and operands- Parameters:
denseShape- The desired matrix shape.type- The value of the type attribute- Returns:
- a new instance of SparseMatrixZeros
-
sparseTensorToCSRSparseMatrix
public SparseTensorToCSRSparseMatrix sparseTensorToCSRSparseMatrix(Operand<TInt64> indices, Operand<? extends TType> values, Operand<TInt64> denseShape) Converts a SparseTensor to a (possibly batched) CSRSparseMatrix.- Parameters:
indices- SparseTensor indices.values- SparseTensor values.denseShape- SparseTensor dense shape.- Returns:
- a new instance of SparseTensorToCSRSparseMatrix
-
ops
-