Class LinalgSparseOps

java.lang.Object
org.tensorflow.op.LinalgSparseOps

public final class LinalgSparseOps extends Object
An API for building linalg.sparse operations as Ops
See Also:
  • 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 batch index. This op is meant only for debugging / testing, and its interface is not expected to be stable.
      Type Parameters:
      T - data type for CSRSparseMatrixComponents output and operands
      Parameters:
      csrSparseMatrix - A batched CSRSparseMatrix.
      index - The index in csr_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 for CSRSparseMatrixToDense output 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 for CSRSparseMatrixToSparseTensor output 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 for SparseMatrixAdd output 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 for SparseMatrixMatMul output and operands
      Parameters:
      a - A CSRSparseMatrix.
      b - A dense tensor.
      options - carries optional attribute values
      Returns:
      a new instance of SparseMatrixMatMul
    • sparseMatrixMul

      public SparseMatrixMul sparseMatrixMul(Operand<? extends TType> a, Operand<? extends TType> b)
      Element-wise multiplication of a sparse matrix with a dense tensor. Returns a sparse matrix.

      The dense tensor b may be either a scalar; otherwise a must be a rank-3 SparseMatrix; in this case b must be shaped [batch_size, 1, 1] and the multiply operation broadcasts.

      NOTE even if b is 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

      public SparseMatrixNNZ sparseMatrixNNZ(Operand<? extends TType> sparseMatrix)
      Returns the number of nonzeroes of sparse_matrix.
      Parameters:
      sparseMatrix - A CSRSparseMatrix.
      Returns:
      a new instance of SparseMatrixNNZ
    • sparseMatrixOrderingAMD

      public SparseMatrixOrderingAMD sparseMatrixOrderingAMD(Operand<? extends TType> input)
      Computes the Approximate Minimum Degree (AMD) ordering of input. 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 N elements, where the input sparse matrix components each have N rows. That is, the component contains each of the integers {0, .. N-1} exactly once. The ith element represents the row index that the ith 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_value stores the AMD ordering: [1 2 3 0].

      input: A CSRSparseMatrix.

      Parameters:
      input - A CSRSparseMatrix.
      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 for SparseMatrixSoftmax output 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 for SparseMatrixSoftmaxGrad output and operands
      Parameters:
      softmax - A CSRSparseMatrix.
      gradSoftmax - The gradient of softmax.
      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 of input. 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 permutation must have compatible shapes. If the sparse matrix has rank 3; with the batch dimension B, then the permutation must be of rank 2; with the same batch dimension B. There is no support for broadcasting.

      Furthermore, each component vector of permutation must be of length N, containing each of the integers {0, 1, ..., N - 1} exactly once, where N is 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 A of the input sparse matrix, the corresponding output sparse matrix represents L, the lower triangular Cholesky factor satisfying the following identity:

         A = L * Lt
       

      where Lt denotes the transpose of L (or its conjugate transpose, if type is complex64 or complex128).

      The type parameter denotes the type of the matrix elements. The supported types are: float32, float64, complex64 and complex128.

      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_value stores 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: A Tensor. type: The type of input.

      Type Parameters:
      T - data type for SparseMatrixSparseCholesky output and operands
      Parameters:
      input - A CSRSparseMatrix.
      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 matrices a and b. Performs a matrix multiplication of a sparse matrix a with a sparse matrix b; returns a sparse matrix a * b, unless either a or b is transposed or adjointed.

      Each matrix may be transposed or adjointed (conjugated and transposed) according to the Boolean parameters transpose_a, adjoint_a, transpose_b and adjoint_b. At most one of transpose_a or adjoint_a may be True. Similarly, at most one of transpose_b or adjoint_b may be True.

      The inputs must have compatible shapes. That is, the inner dimension of a must be equal to the outer dimension of b. This requirement is adjusted according to whether either a or b is transposed or adjointed.

      The type parameter denotes the type of the matrix elements. Both a and b must have the same type. The supported types are: float32, float64, complex64 and complex128.

      Both a and b must have the same rank. Broadcasting is not supported. If they have rank 3, each batch of 2D CSRSparseMatrices within a and b must 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_value stores the dense matrix product:

           [[  2.   0.   0.]
            [  0.   0.   0.]
            [ 35.  40.   0.]
            [ -4.   0.   0.]]
       

      a: A CSRSparseMatrix. b: A CSRSparseMatrix with the same type and rank as a. type: The type of both a and b. transpose_a: If True, a transposed before multiplication. transpose_b: If True, b transposed before multiplication. adjoint_a: If True, a adjointed before multiplication. adjoint_b: If True, b adjointed before multiplication.

      Type Parameters:
      T - data type for SparseMatrixSparseMatMul output and operands
      Parameters:
      a - A CSRSparseMatrix.
      b - A CSRSparseMatrix.
      type - The value of the type attribute
      options - 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 for SparseMatrixTranspose output and operands
      Parameters:
      input - A CSRSparseMatrix.
      type - The value of the type attribute
      options - 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 shape dense_shape.
      Type Parameters:
      T - data type for SparseMatrixZeros output 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

      public final Ops ops()
      Get the parent Ops object.