math.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from paddle import _C_ops
  15. from paddle.base.data_feeder import check_variable_and_dtype
  16. from paddle.base.layer_helper import LayerHelper
  17. from paddle.framework import in_dynamic_or_pir_mode
  18. __all__ = []
  19. def segment_sum(data, segment_ids, name=None):
  20. r"""
  21. Segment Sum Operator.
  22. This operator sums the elements of input `data` which with
  23. the same index in `segment_ids`.
  24. It computes a tensor such that $out_i = \\sum_{j} data_{j}$
  25. where sum is over j such that `segment_ids[j] == i`.
  26. Args:
  27. data (Tensor): A tensor, available data type float32, float64, int32, int64, float16.
  28. segment_ids (Tensor): A 1-D tensor, which have the same size
  29. with the first dimension of input data.
  30. Available data type is int32, int64.
  31. name (str, optional): Name for the operation (optional, default is None).
  32. For more information, please refer to :ref:`api_guide_Name`.
  33. Returns:
  34. - output (Tensor), the reduced result.
  35. Examples:
  36. .. code-block:: python
  37. >>> import paddle
  38. >>> data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
  39. >>> segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
  40. >>> out = paddle.geometric.segment_sum(data, segment_ids)
  41. >>> print(out.numpy())
  42. [[4. 4. 4.]
  43. [4. 5. 6.]]
  44. """
  45. if in_dynamic_or_pir_mode():
  46. return _C_ops.segment_pool(data, segment_ids, "SUM")
  47. else:
  48. check_variable_and_dtype(
  49. data,
  50. "X",
  51. ("float32", "float64", "int32", "int64", "float16", "uint16"),
  52. "segment_pool",
  53. )
  54. check_variable_and_dtype(
  55. segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
  56. )
  57. helper = LayerHelper("segment_sum", **locals())
  58. out = helper.create_variable_for_type_inference(dtype=data.dtype)
  59. summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
  60. helper.append_op(
  61. type="segment_pool",
  62. inputs={"X": data, "SegmentIds": segment_ids},
  63. outputs={"Out": out, "SummedIds": summed_ids},
  64. attrs={"pooltype": "SUM"},
  65. )
  66. return out
  67. def segment_mean(data, segment_ids, name=None):
  68. r"""
  69. Segment mean Operator.
  70. This operator calculate the mean value of input `data` which
  71. with the same index in `segment_ids`.
  72. It computes a tensor such that $out_i = \\frac{1}{n_i} \\sum_{j} data[j]$
  73. where sum is over j such that 'segment_ids[j] == i' and $n_i$ is the number
  74. of all index 'segment_ids[j] == i'.
  75. Args:
  76. data (tensor): a tensor, available data type float32, float64, int32, int64, float16.
  77. segment_ids (tensor): a 1-d tensor, which have the same size
  78. with the first dimension of input data.
  79. available data type is int32, int64.
  80. name (str, optional): Name for the operation (optional, default is None).
  81. For more information, please refer to :ref:`api_guide_Name`.
  82. Returns:
  83. - output (Tensor), the reduced result.
  84. Examples:
  85. .. code-block:: python
  86. >>> import paddle
  87. >>> data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
  88. >>> segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
  89. >>> out = paddle.geometric.segment_mean(data, segment_ids)
  90. >>> print(out.numpy())
  91. [[2. 2. 2.]
  92. [4. 5. 6.]]
  93. """
  94. if in_dynamic_or_pir_mode():
  95. return _C_ops.segment_pool(data, segment_ids, "MEAN")
  96. else:
  97. check_variable_and_dtype(
  98. data,
  99. "X",
  100. ("float32", "float64", "int32", "int64", "float16", "uint16"),
  101. "segment_pool",
  102. )
  103. check_variable_and_dtype(
  104. segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
  105. )
  106. helper = LayerHelper("segment_mean", **locals())
  107. out = helper.create_variable_for_type_inference(dtype=data.dtype)
  108. summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
  109. helper.append_op(
  110. type="segment_pool",
  111. inputs={"X": data, "SegmentIds": segment_ids},
  112. outputs={"Out": out, "SummedIds": summed_ids},
  113. attrs={"pooltype": "MEAN"},
  114. )
  115. return out
  116. def segment_min(data, segment_ids, name=None):
  117. r"""
  118. Segment min operator.
  119. This operator calculate the minimum elements of input `data` which with
  120. the same index in `segment_ids`.
  121. It computes a tensor such that $out_i = \\min_{j} data_{j}$
  122. where min is over j such that `segment_ids[j] == i`.
  123. Args:
  124. data (tensor): a tensor, available data type float32, float64, int32, int64, float16.
  125. segment_ids (tensor): a 1-d tensor, which have the same size
  126. with the first dimension of input data.
  127. available data type is int32, int64.
  128. name (str, optional): Name for the operation (optional, default is None).
  129. For more information, please refer to :ref:`api_guide_Name`.
  130. Returns:
  131. - output (Tensor), the reduced result.
  132. Examples:
  133. .. code-block:: python
  134. >>> import paddle
  135. >>> data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
  136. >>> segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
  137. >>> out = paddle.geometric.segment_min(data, segment_ids)
  138. >>> print(out.numpy())
  139. [[1. 2. 1.]
  140. [4. 5. 6.]]
  141. """
  142. if in_dynamic_or_pir_mode():
  143. return _C_ops.segment_pool(data, segment_ids, "MIN")
  144. else:
  145. check_variable_and_dtype(
  146. data,
  147. "X",
  148. ("float32", "float64", "int32", "int64", "float16", "uint16"),
  149. "segment_pool",
  150. )
  151. check_variable_and_dtype(
  152. segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
  153. )
  154. helper = LayerHelper("segment_min", **locals())
  155. out = helper.create_variable_for_type_inference(dtype=data.dtype)
  156. summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
  157. helper.append_op(
  158. type="segment_pool",
  159. inputs={"X": data, "SegmentIds": segment_ids},
  160. outputs={"Out": out, "SummedIds": summed_ids},
  161. attrs={"pooltype": "MIN"},
  162. )
  163. return out
  164. def segment_max(data, segment_ids, name=None):
  165. r"""
  166. Segment max operator.
  167. This operator calculate the maximum elements of input `data` which with
  168. the same index in `segment_ids`.
  169. It computes a tensor such that $out_i = \\max_{j} data_{j}$
  170. where max is over j such that `segment_ids[j] == i`.
  171. Args:
  172. data (tensor): a tensor, available data type float32, float64, int32, int64, float16.
  173. segment_ids (tensor): a 1-d tensor, which have the same size
  174. with the first dimension of input data.
  175. available data type is int32, int64.
  176. name (str, optional): Name for the operation (optional, default is None).
  177. For more information, please refer to :ref:`api_guide_Name`.
  178. Returns:
  179. - output (Tensor), the reduced result.
  180. Examples:
  181. .. code-block:: python
  182. >>> import paddle
  183. >>> data = paddle.to_tensor([[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32')
  184. >>> segment_ids = paddle.to_tensor([0, 0, 1], dtype='int32')
  185. >>> out = paddle.geometric.segment_max(data, segment_ids)
  186. >>> print(out.numpy())
  187. [[3. 2. 3.]
  188. [4. 5. 6.]]
  189. """
  190. if in_dynamic_or_pir_mode():
  191. return _C_ops.segment_pool(data, segment_ids, "MAX")
  192. else:
  193. check_variable_and_dtype(
  194. data,
  195. "X",
  196. ("float32", "float64", "int32", "int64", "float16", "uint16"),
  197. "segment_pool",
  198. )
  199. check_variable_and_dtype(
  200. segment_ids, "SegmentIds", ("int32", "int64"), "segment_pool"
  201. )
  202. helper = LayerHelper("segment_max", **locals())
  203. out = helper.create_variable_for_type_inference(dtype=data.dtype)
  204. summed_ids = helper.create_variable_for_type_inference(dtype=data.dtype)
  205. helper.append_op(
  206. type="segment_pool",
  207. inputs={"X": data, "SegmentIds": segment_ids},
  208. outputs={"Out": out, "SummedIds": summed_ids},
  209. attrs={"pooltype": "MAX"},
  210. )
  211. return out