math.py 10.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.

J
Jiabin Yang 已提交
15
from paddle.fluid.layer_helper import LayerHelper, _non_static_mode
16
from paddle.fluid.data_feeder import check_variable_and_dtype
17
from paddle import _C_ops, _legacy_C_ops
H
hong 已提交
18
from paddle.fluid.framework import _in_legacy_dygraph, in_dygraph_mode
19
import paddle.utils.deprecated as deprecated
20

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

23

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

    This operator sums the elements of input `data` which with
    the same index in `segment_ids`.
    It computes a tensor such that $out_i = \\sum_{j} data_{j}$
    where sum is over j such that `segment_ids[j] == i`.

    Args:
38
        data (Tensor): A tensor, available data type float32, float64, int32, int64.
39
        segment_ids (Tensor): A 1-D tensor, which have the same size
40
                            with the first dimension of input data.
41
                            Available data type is int32, int64.
42
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
43 44
                            For more information, please refer to :ref:`api_guide_Name`.

45 46 47 48 49 50 51 52 53 54 55 56 57 58
    Returns:
       output (Tensor): the reduced result.

    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 已提交
59
    if in_dygraph_mode():
60
        return _C_ops.segment_pool(data, segment_ids, "SUM")[0]
H
hong 已提交
61
    if _in_legacy_dygraph():
62 63
        out, tmp = _legacy_C_ops.segment_pool(data, segment_ids, 'pooltype',
                                              "SUM")
64 65
        return out

66 67 68
    check_variable_and_dtype(data, "X",
                             ("float32", "float64", "int32", "int64"),
                             "segment_pool")
69 70 71 72 73 74
    check_variable_and_dtype(segment_ids, "SegmentIds", ("int32", "int64"),
                             "segment_pool")

    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)
75 76 77 78 79 80 81 82 83 84
    helper.append_op(type="segment_pool",
                     inputs={
                         "X": data,
                         "SegmentIds": segment_ids
                     },
                     outputs={
                         "Out": out,
                         "SummedIds": summed_ids
                     },
                     attrs={"pooltype": "SUM"})
85 86 87
    return out


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

    Ihis operator calculate the mean value of input `data` which
    with the same index in `segment_ids`.
    It computes a tensor such that $out_i = \\frac{1}{n_i}  \\sum_{j} data[j]$
    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:
103
        data (tensor): a tensor, available data type float32, float64, int32, int64.
104 105
        segment_ids (tensor): a 1-d tensor, which have the same size
                            with the first dimension of input data.
106
                            available data type is int32, int64.
107
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
108
                            For more information, please refer to :ref:`api_guide_Name`.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    Returns:
       output (Tensor): the reduced result.

    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 已提交
124 125

    if in_dygraph_mode():
126
        return _C_ops.segment_pool(data, segment_ids, "MEAN")[0]
J
Jiabin Yang 已提交
127
    if _non_static_mode():
128 129
        out, tmp = _legacy_C_ops.segment_pool(data, segment_ids, 'pooltype',
                                              "MEAN")
130 131
        return out

132 133 134
    check_variable_and_dtype(data, "X",
                             ("float32", "float64", "int32", "int64"),
                             "segment_pool")
135 136 137 138 139 140
    check_variable_and_dtype(segment_ids, "SegmentIds", ("int32", "int64"),
                             "segment_pool")

    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)
141 142 143 144 145 146 147 148 149 150
    helper.append_op(type="segment_pool",
                     inputs={
                         "X": data,
                         "SegmentIds": segment_ids
                     },
                     outputs={
                         "Out": out,
                         "SummedIds": summed_ids
                     },
                     attrs={"pooltype": "MEAN"})
151 152 153
    return out


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

    This operator calculate the minimum elements of input `data` which with
    the same index in `segment_ids`.
    It computes a tensor such that $out_i = \\min_{j} data_{j}$
    where min is over j such that `segment_ids[j] == i`.

    Args:
168
        data (tensor): a tensor, available data type float32, float64, int32, int64.
169
        segment_ids (tensor): a 1-d tensor, which have the same size
170
                            with the first dimension of input data.
171
                            available data type is int32, int64.
172
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
173 174
                            For more information, please refer to :ref:`api_guide_Name`.

175 176 177 178 179 180 181 182 183 184 185 186 187 188
    Returns:
       output (Tensor): the reduced result.

    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 已提交
189 190

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

J
Jiabin Yang 已提交
193
    if _non_static_mode():
194 195
        out, tmp = _legacy_C_ops.segment_pool(data, segment_ids, 'pooltype',
                                              "MIN")
196 197
        return out

198 199 200
    check_variable_and_dtype(data, "X",
                             ("float32", "float64", "int32", "int64"),
                             "segment_pool")
201 202 203 204 205 206
    check_variable_and_dtype(segment_ids, "SegmentIds", ("int32", "int64"),
                             "segment_pool")

    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)
207 208 209 210 211 212 213 214 215 216
    helper.append_op(type="segment_pool",
                     inputs={
                         "X": data,
                         "SegmentIds": segment_ids
                     },
                     outputs={
                         "Out": out,
                         "SummedIds": summed_ids
                     },
                     attrs={"pooltype": "MIN"})
217 218 219
    return out


220 221 222 223
@deprecated(since="2.4.0",
            update_to="paddle.geometric.segment_max",
            level=1,
            reason="paddle.incubate.segment_max will be removed in future")
224
def segment_max(data, segment_ids, name=None):
Z
Zhong Hui 已提交
225
    r"""
226 227 228 229
    Segment max operator.

    This operator calculate the maximum elements of input `data` which with
    the same index in `segment_ids`.
Z
Zhong Hui 已提交
230
    It computes a tensor such that $out_i = \\max_{j} data_{j}$
231 232 233
    where max is over j such that `segment_ids[j] == i`.

    Args:
234
        data (tensor): a tensor, available data type float32, float64, int32, int64.
235
        segment_ids (tensor): a 1-d tensor, which have the same size
236
                            with the first dimension of input data.
237
                            available data type is int32, int64.
238
        name (str, optional): Name for the operation (optional, default is None).
Z
Zhong Hui 已提交
239
                            For more information, please refer to :ref:`api_guide_Name`.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

    Returns:
       output (Tensor): the reduced result.

    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 已提交
255 256

    if in_dygraph_mode():
257
        out, tmp = _C_ops.segment_pool(data, segment_ids, "MAX")
H
hong 已提交
258 259
        return out

J
Jiabin Yang 已提交
260
    if _non_static_mode():
261 262
        out, tmp = _legacy_C_ops.segment_pool(data, segment_ids, 'pooltype',
                                              "MAX")
263 264
        return out

265 266 267
    check_variable_and_dtype(data, "X",
                             ("float32", "float64", "int32", "int64"),
                             "segment_pool")
268 269 270 271 272 273
    check_variable_and_dtype(segment_ids, "SegmentIds", ("int32", "int64"),
                             "segment_pool")

    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)
274 275 276 277 278 279 280 281 282 283
    helper.append_op(type="segment_pool",
                     inputs={
                         "X": data,
                         "SegmentIds": segment_ids
                     },
                     outputs={
                         "Out": out,
                         "SummedIds": summed_ids
                     },
                     attrs={"pooltype": "MAX"})
284
    return out