math.py 9.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import paddle.utils.deprecated as deprecated
16
from paddle import _C_ops, _legacy_C_ops
17
from paddle.fluid.data_feeder import check_variable_and_dtype
姜永久 已提交
18
from paddle.fluid.framework import in_dygraph_mode
19
from paddle.fluid.layer_helper import LayerHelper, _non_static_mode
20

Z
Zhong Hui 已提交
21 22
__all__ = []

23

24 25 26 27 28 29
@deprecated(
    since="2.4.0",
    update_to="paddle.geometric.segment_sum",
    level=1,
    reason="paddle.incubate.segment_sum will be removed in future",
)
30
def segment_sum(data, segment_ids, name=None):
Z
Zhong Hui 已提交
31
    r"""
32 33
    Segment Sum Operator.

Z
Zman 已提交
34
    Sum the elements of input `data` which with
35
    the same index in `segment_ids`.
Z
Zman 已提交
36 37 38 39 40 41
    It computes a tensor such that

    .. math::

        out_i = \sum_{j \in \{segment\_ids_j == i \} } data_{j}

42 43 44
    where sum is over j such that `segment_ids[j] == i`.

    Args:
45
        data (Tensor): A tensor, available data type float32, float64, int32, int64.
46
        segment_ids (Tensor): A 1-D tensor, which have the same size
47
                            with the first dimension of input data.
48
                            Available data type is int32, int64.
49
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
50 51
                            For more information, please refer to :ref:`api_guide_Name`.

52
    Returns:
Z
Zman 已提交
53
       Tensor, the Segment Sum result.
54 55 56 57 58 59 60 61 62 63 64 65

    Examples:

        .. code-block:: python

            import paddle
            data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
            segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
            out = paddle.incubate.segment_sum(data, segment_ids)
            #Outputs: [[4., 4., 4.], [4., 5., 6.]]

    """
H
hong 已提交
66
    if in_dygraph_mode():
67
        return _C_ops.segment_pool(data, segment_ids, "SUM")[0]
姜永久 已提交
68 69 70 71 72 73
    else:
        check_variable_and_dtype(
            data, "X", ("float32", "float64", "int32", "int64"), "segment_pool"
        )
        check_variable_and_dtype(
            segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
74
        )
75

姜永久 已提交
76 77 78 79 80 81 82 83 84 85
        helper = LayerHelper("segment_sum", **locals())
        out = helper.create_variable_for_type_inference(dtype=data.dtype)
        summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
        helper.append_op(
            type="segment_pool",
            inputs={"X": data, "SegmentIds": segment_ids},
            outputs={"Out": out, "SummedIds": summed_ids},
            attrs={"pooltype": "SUM"},
        )
        return out
86 87


88 89 90 91 92 93
@deprecated(
    since="2.4.0",
    update_to="paddle.geometric.segment_mean",
    level=1,
    reason="paddle.incubate.segment_mean will be removed in future",
)
94
def segment_mean(data, segment_ids, name=None):
Z
Zhong Hui 已提交
95
    r"""
Z
Zman 已提交
96
    Segment Mean Operator.
97 98 99

    Ihis operator calculate the mean value of input `data` which
    with the same index in `segment_ids`.
Z
Zman 已提交
100 101 102 103 104 105
    It computes a tensor such that

    .. math::

        out_i = \mathop{mean}_{j \in \{segment\_ids_j == i \} } data_{j}

106 107 108 109
    where sum is over j such that 'segment_ids[j] == i' and $n_i$ is the number
    of all index 'segment_ids[j] == i'.

    Args:
110
        data (tensor): a tensor, available data type float32, float64, int32, int64.
111 112
        segment_ids (tensor): a 1-d tensor, which have the same size
                            with the first dimension of input data.
113
                            available data type is int32, int64.
114
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
115
                            For more information, please refer to :ref:`api_guide_Name`.
116 117

    Returns:
Z
Zman 已提交
118
       Tensor, the Segment Mean result.
119 120 121 122 123 124 125 126 127 128 129 130

    Examples:

        .. code-block:: python

            import paddle
            data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
            segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
            out = paddle.incubate.segment_mean(data, segment_ids)
            #Outputs: [[2., 2., 2.], [4., 5., 6.]]

    """
H
hong 已提交
131 132

    if in_dygraph_mode():
133
        return _C_ops.segment_pool(data, segment_ids, "MEAN")[0]
J
Jiabin Yang 已提交
134
    if _non_static_mode():
135 136 137
        out, tmp = _legacy_C_ops.segment_pool(
            data, segment_ids, 'pooltype', "MEAN"
        )
138 139
        return out

140 141 142 143 144 145
    check_variable_and_dtype(
        data, "X", ("float32", "float64", "int32", "int64"), "segment_pool"
    )
    check_variable_and_dtype(
        segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
    )
146 147 148 149

    helper = LayerHelper("segment_mean", **locals())
    out = helper.create_variable_for_type_inference(dtype=data.dtype)
    summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
150 151 152 153 154 155
    helper.append_op(
        type="segment_pool",
        inputs={"X": data, "SegmentIds": segment_ids},
        outputs={"Out": out, "SummedIds": summed_ids},
        attrs={"pooltype": "MEAN"},
    )
156 157 158
    return out


159 160 161 162 163 164
@deprecated(
    since="2.4.0",
    update_to="paddle.geometric.segment_min",
    level=1,
    reason="paddle.incubate.segment_min will be removed in future",
)
165
def segment_min(data, segment_ids, name=None):
Z
Zhong Hui 已提交
166
    r"""
167 168
    Segment min operator.

Z
Zman 已提交
169
    Calculate the minimum elements of input `data` which with
170
    the same index in `segment_ids`.
Z
Zman 已提交
171 172 173 174 175 176
    It computes a tensor such that

    .. math::

        out_i = \min_{j \in \{segment\_ids_j == i \} } data_{j}

177 178 179
    where min is over j such that `segment_ids[j] == i`.

    Args:
180
        data (tensor): a tensor, available data type float32, float64, int32, int64.
181
        segment_ids (tensor): a 1-d tensor, which have the same size
182
                            with the first dimension of input data.
183
                            available data type is int32, int64.
184
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
185 186
                            For more information, please refer to :ref:`api_guide_Name`.

187
    Returns:
Z
Zman 已提交
188
       Tensor, the minimum result.
189 190 191 192 193 194 195 196 197 198 199 200

    Examples:

        .. code-block:: python

            import paddle
            data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
            segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
            out = paddle.incubate.segment_min(data, segment_ids)
            #Outputs:  [[1., 2., 1.], [4., 5., 6.]]

    """
H
hong 已提交
201 202

    if in_dygraph_mode():
203
        return _C_ops.segment_pool(data, segment_ids, "MIN")[0]
H
hong 已提交
204

J
Jiabin Yang 已提交
205
    if _non_static_mode():
206 207 208
        out, tmp = _legacy_C_ops.segment_pool(
            data, segment_ids, 'pooltype', "MIN"
        )
209 210
        return out

211 212 213 214 215 216
    check_variable_and_dtype(
        data, "X", ("float32", "float64", "int32", "int64"), "segment_pool"
    )
    check_variable_and_dtype(
        segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
    )
217 218 219 220

    helper = LayerHelper("segment_min", **locals())
    out = helper.create_variable_for_type_inference(dtype=data.dtype)
    summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
221 222 223 224 225 226
    helper.append_op(
        type="segment_pool",
        inputs={"X": data, "SegmentIds": segment_ids},
        outputs={"Out": out, "SummedIds": summed_ids},
        attrs={"pooltype": "MIN"},
    )
227 228 229
    return out


230 231 232 233 234 235
@deprecated(
    since="2.4.0",
    update_to="paddle.geometric.segment_max",
    level=1,
    reason="paddle.incubate.segment_max will be removed in future",
)
236
def segment_max(data, segment_ids, name=None):
Z
Zhong Hui 已提交
237
    r"""
238 239
    Segment max operator.

Z
Zman 已提交
240
    Calculate the maximum elements of input `data` which with
241
    the same index in `segment_ids`.
Z
Zman 已提交
242 243 244 245 246 247
    It computes a tensor such that

    .. math::

        out_i = \max_{j \in \{segment\_ids_j == i \} } data_{j}

248 249 250
    where max is over j such that `segment_ids[j] == i`.

    Args:
251
        data (tensor): a tensor, available data type float32, float64, int32, int64.
252
        segment_ids (tensor): a 1-d tensor, which have the same size
253
                            with the first dimension of input data.
254
                            available data type is int32, int64.
255
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
256
                            For more information, please refer to :ref:`api_guide_Name`.
257 258

    Returns:
Z
Zman 已提交
259
       Tensor, the maximum result.
260 261 262 263 264 265 266 267 268 269 270 271

    Examples:

        .. code-block:: python

            import paddle
            data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
            segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
            out = paddle.incubate.segment_max(data, segment_ids)
            #Outputs: [[3., 2., 3.], [4., 5., 6.]]

    """
H
hong 已提交
272 273

    if in_dygraph_mode():
274
        out, tmp = _C_ops.segment_pool(data, segment_ids, "MAX")
H
hong 已提交
275 276
        return out

J
Jiabin Yang 已提交
277
    if _non_static_mode():
278 279 280
        out, tmp = _legacy_C_ops.segment_pool(
            data, segment_ids, 'pooltype', "MAX"
        )
281 282
        return out

283 284 285 286 287 288
    check_variable_and_dtype(
        data, "X", ("float32", "float64", "int32", "int64"), "segment_pool"
    )
    check_variable_and_dtype(
        segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
    )
289 290 291 292

    helper = LayerHelper("segment_max", **locals())
    out = helper.create_variable_for_type_inference(dtype=data.dtype)
    summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
293 294 295 296 297 298
    helper.append_op(
        type="segment_pool",
        inputs={"X": data, "SegmentIds": segment_ids},
        outputs={"Out": out, "SummedIds": summed_ids},
        attrs={"pooltype": "MAX"},
    )
299
    return out