cast

paddle.sparse. cast ( x, index_dtype=None, value_dtype=None, name=None ) [源代码]

输入 xSparseCooTensorSparseCsrTensor 。将稀疏 Tensor 的 index 转换为 index_dtype 类型 ( SparseCsrTensor 的 index 指: crowscol ),value 转换为 value_dtype 类型,

参数

  • x (SparseTensor) - 输入的稀疏 Tensor,可以为 Coo 或 Csr 格式,数据类型为 float32、float64。

  • index_dtype (np.dtype|str,可选) - SparseCooTensor 的 index 类型,SparseCsrTensor 的 crows/cols 类型。可以是 uint8,int8,int16,int32,int64。

  • value_dtype (np.dtype|str,可选) - SparseCooTensor 或 SparseCsrTensor 的 value 类型。可以是 uint8,int8,int16,int32,int64。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

多维稀疏 Tensor,稀疏格式与 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.])