diff --git a/docs/api/paddle/scatter_nd_add_cn.rst b/docs/api/paddle/scatter_nd_add_cn.rst index e99e4159a30..ed14829f9a5 100644 --- a/docs/api/paddle/scatter_nd_add_cn.rst +++ b/docs/api/paddle/scatter_nd_add_cn.rst @@ -57,4 +57,13 @@ Tensor,数据类型和形状都与 :code:`x` 相同。 代码示例 :::::::::::: -COPY-FROM: paddle.scatter_nd_add + + output = [0, 22, 12, 14, 4, 5] + +**示例一图解说明**: + + 在这个示例中,通过 Paddle 的 scatter_nd_add 函数对张量 x 进行稀疏加法操作。初始张量 x 为 [0, 1, 2, 3, 4, 5],通过 index 指定需要更新的索引位置,并使用 updates 中的值进行累加。scatter_nd_add 函数会根据 index 的位置逐步累加 updates 中的对应值,而不是替换原有值,最终得到输出张量为 [0, 22, 12, 14, 4, 5],实现了对张量部分元素的累加更新而保持其他元素不变。 + .. figure:: ../../images/api_legend/scatter_nd_add.png + :width: 700 + :alt: 示例一图示 + :align: center diff --git a/docs/api/paddle/scatter_nd_add_en.rst b/docs/api/paddle/scatter_nd_add_en.rst new file mode 100644 index 00000000000..716e9755b32 --- /dev/null +++ b/docs/api/paddle/scatter_nd_add_en.rst @@ -0,0 +1,69 @@ +.. _en_api_paddle_scatter_nd_add: + +scatter_nd_add +------------------------------- + +.. py:function:: paddle.scatter_nd_add(x, index, updates, name=None) + + + + +Performs sparse addition on individual values or slices in a Tensor, resulting in an output Tensor. + +:code:`x` is a Tensor with a dimension of :code:`R`. :code:`index` is a Tensor with a dimension of :code:`K`, which means that the shape of :code:`index` is :math:`[i_0, i_1, ..., i_{K-2}, Q]`, where :math:`Q \leq R`. :code:`updates` is a Tensor with a dimension of :math:`K - 1 + R - Q` and a shape of :math:`index.shape[:-1] + x.shape[index.shape[-1]:]`. + +According to the :math:`[i_0, i_1, ..., i_{K-2}]` of :code:`index`, it selects the corresponding :code:`updates` slice and adds it to the :code:`x` slice obtained by the last dimension of :code:`index`, resulting in the final output Tensor. + +Examples: + +:: + + - Example 1: + x = [0, 1, 2, 3, 4, 5] + index = [[1], [2], [3], [1]] + updates = [9, 10, 11, 12] + + Result: + + output = [0, 22, 12, 14, 4, 5] + + - Example 2: + x = [[65, 17], [-14, -25]] + index = [[], []] + updates = [[[-1, -2], [1, 2]], + [[3, 4], [-3, -4]]] + x.shape = (2, 2) + index.shape = (2, 0) + updates.shape = (2, 2, 2) + + Result: + + output = [[67, 19], [-16, -27]] + + +Parameters +:::::::::::: + + - **x** (Tensor) - The input tensor, data type can be int32, int64, float32, float64. + - **index** (Tensor) - The index tensor, data type must be non-negative int32 or non-negative int64. Its dimension :code:`index.ndim` must be greater than 1, and :code:`index.shape[-1] <= x.ndim` + - **updates** (Tensor) - The update tensor, which must have the same data type as :code:`x`. Its shape must be :code:`index.shape[:-1] + x.shape[index.shape[-1]:]`. + - **name** (str, optional) - For more details, refer to :ref:`api_guide_Name`. Generally, this does not need to be set. Default is None. + +Returns +:::::::::::: + +A tensor with the same data type and shape as :code:`x`. + +Example Code +:::::::::::: + + + output = [0, 22, 12, 14, 4, 5] + +**Explanation of Example 1**: + +In this example, the scatter_nd_add function of Paddle performs sparse addition on the tensor `x`. The initial tensor `x` is `[0, 1, 2, 3, 4, 5]`. The `index` specifies the positions to be updated, and the values in `updates` are used to accumulate them. The scatter_nd_add function will add the corresponding values in `updates` to the specified positions in `x`, rather than replacing the original values. Finally, the output tensor is `[0, 22, 12, 14, 4, 5]`, achieving the cumulative update of specific elements in the tensor while keeping other elements unchanged. + .. figure:: ../../images/api_legend/scatter_nd_add.png + :width: 700 + :alt: Example 1 Illustration + :align: center diff --git a/docs/images/api_legend/scatter_nd_ad.png b/docs/images/api_legend/scatter_nd_ad.png new file mode 100644 index 00000000000..54d30b6422d Binary files /dev/null and b/docs/images/api_legend/scatter_nd_ad.png differ