paddle.sparse. cast ( x, index_dtype=None, value_dtype=None, name=None ) [source]

cast non-zero-index of SparseTensor to index_dtype, non-zero-element of SparseTensor to value_dtype , requiring x to be a SparseCooTensor or SparseCsrTensor.

  • x (Tensor) – The input Sparse Tensor with data type float32, float64.

  • index_dtype (np.dtype|str, optional) – Data type of the index of SparseCooTensor, or crows/cols of SparseCsrTensor. Can be uint8, int8, int16, int32, int64.

  • value_dtype (np.dtype|str, optional) – Data type of the value of SparseCooTensor, SparseCsrTensor. Can be bool, float16, float32, float64, int8, int32, int64, uint8.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.


A Sparse Tensor with the same data type and shape as x .


>>> import paddle

>>> dense_x = paddle.to_tensor([-2, 0, 1])
>>> sparse_x = dense_x.to_sparse_coo(1)
>>> out = paddle.sparse.cast(sparse_x, 'int32', 'float64')
>>> out
Tensor(shape=[3], dtype=paddle.float64, place=Place(cpu), stop_gradient=True,
    indices=[[0, 2]],
    values=[-2.,  1.])