From 64237c3f89086fa7ce8163c2f2283163365d76db Mon Sep 17 00:00:00 2001 From: zhangkaihuo Date: Thu, 14 Apr 2022 19:32:38 +0800 Subject: [PATCH] Supplementary documents (#41700) --- .../fluid/dygraph/varbase_patch_methods.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/python/paddle/fluid/dygraph/varbase_patch_methods.py b/python/paddle/fluid/dygraph/varbase_patch_methods.py index b2441e90fc..03045579e7 100644 --- a/python/paddle/fluid/dygraph/varbase_patch_methods.py +++ b/python/paddle/fluid/dygraph/varbase_patch_methods.py @@ -878,6 +878,28 @@ def monkey_patch_varbase(): @framework.dygraph_only 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(): return _C_ops.final_state_sparse_coo_values(self) elif self.is_sparse_csr(): @@ -888,6 +910,30 @@ def monkey_patch_varbase(): @framework.dygraph_only 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(): return _C_ops.final_state_sparse_coo_to_dense(self) elif self.is_sparse_csr(): @@ -897,6 +943,28 @@ def monkey_patch_varbase(): @framework.dygraph_only 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(): return _C_ops.final_state_sparse_to_sparse_coo(self, sparse_dim) elif self.is_sparse_coo(): -- GitLab