未验证 提交 64237c3f 编写于 作者: Z zhangkaihuo 提交者: GitHub

Supplementary documents (#41700)

上级 cbe7466f
...@@ -878,6 +878,28 @@ def monkey_patch_varbase(): ...@@ -878,6 +878,28 @@ def monkey_patch_varbase():
@framework.dygraph_only @framework.dygraph_only
def values(self): def values(self):
"""
**Notes**:
**This API is ONLY available in Dygraph mode**
Get the values of current SparseTensor(COO or CSR).
Returns:
Tensor: A DenseTensor
Examples:
.. code-block:: python
import paddle
from paddle.fluid.framework import _test_eager_guard
with _test_eager_guard():
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
values = [1, 2, 3, 4, 5]
dense_shape = [3, 4]
sparse_x = paddle.sparse.sparse_coo_tensor(paddle.to_tensor(indices, dtype='int32'), paddle.to_tensor(values, dtype='float32'), shape=dense_shape)
print(sparse_x.values())
#[1, 2, 3, 4, 5]
"""
if self.is_sparse_coo(): if self.is_sparse_coo():
return _C_ops.final_state_sparse_coo_values(self) return _C_ops.final_state_sparse_coo_values(self)
elif self.is_sparse_csr(): elif self.is_sparse_csr():
...@@ -888,6 +910,30 @@ def monkey_patch_varbase(): ...@@ -888,6 +910,30 @@ def monkey_patch_varbase():
@framework.dygraph_only @framework.dygraph_only
def to_dense(self): def to_dense(self):
"""
**Notes**:
**This API is ONLY available in Dygraph mode**
Convert the current SparseTensor(COO or CSR) to DenseTensor.
Returns:
Tensor: A DenseTensor
Examples:
.. code-block:: python
import paddle
from paddle.fluid.framework import _test_eager_guard
with _test_eager_guard():
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
values = [1, 2, 3, 4, 5]
dense_shape = [3, 4]
sparse_x = paddle.sparse.sparse_coo_tensor(paddle.to_tensor(indices, dtype='int64'), paddle.to_tensor(values, dtype='float32'), shape=dense_shape)
dense_x = sparse_x.to_dense()
#[[0., 1., 0., 2.],
# [0., 0., 3., 0.],
# [4., 5., 0., 0.]]
"""
if self.is_sparse_coo(): if self.is_sparse_coo():
return _C_ops.final_state_sparse_coo_to_dense(self) return _C_ops.final_state_sparse_coo_to_dense(self)
elif self.is_sparse_csr(): elif self.is_sparse_csr():
...@@ -897,6 +943,28 @@ def monkey_patch_varbase(): ...@@ -897,6 +943,28 @@ def monkey_patch_varbase():
@framework.dygraph_only @framework.dygraph_only
def to_sparse_coo(self, sparse_dim): def to_sparse_coo(self, sparse_dim):
"""
**Notes**:
**This API is ONLY available in Dygraph mode**
Convert the current DenseTensor to SparseTensor in COO format.
Returns:
Tensor: A SparseCooTensor
Examples:
.. code-block:: python
import paddle
from paddle.fluid.framework import _test_eager_guard
with _test_eager_guard():
dense_x = [[0, 1, 0, 2], [0, 0, 3, 4]]
dense_x = paddle.to_tensor(dense_x, dtype='float32')
sparse_x = dense_x.to_sparse_coo(sparse_dim=2)
#indices=[[0, 0, 1, 1],
# [1, 3, 2, 3]],
#values=[1., 2., 3., 4.]
"""
if self.is_sparse_csr(): if self.is_sparse_csr():
return _C_ops.final_state_sparse_to_sparse_coo(self, sparse_dim) return _C_ops.final_state_sparse_to_sparse_coo(self, sparse_dim)
elif self.is_sparse_coo(): elif self.is_sparse_coo():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册