未验证 提交 a0bf44f5 编写于 作者: L Li-fAngyU 提交者: GitHub

【PaddlePaddle Hackathon 3 No.5】为 Paddle 新增 bucketize (#44195)

* add paddle.bucketize api

add paddle.bucketize api

* updata paddle.bucketize code style.

* upgrade unittests/test_bucketize_api.py.

* update tests/unittests/test_bucketize_api.py file.

* 修改bucketize API 的英文参数描述
上级 02c59eda
...@@ -292,6 +292,7 @@ from .tensor.search import argmax # noqa: F401 ...@@ -292,6 +292,7 @@ from .tensor.search import argmax # noqa: F401
from .tensor.search import argmin # noqa: F401 from .tensor.search import argmin # noqa: F401
from .tensor.search import argsort # noqa: F401 from .tensor.search import argsort # noqa: F401
from .tensor.search import searchsorted # noqa: F401 from .tensor.search import searchsorted # noqa: F401
from .tensor.search import bucketize # noqa: F401
from .tensor.search import masked_select # noqa: F401 from .tensor.search import masked_select # noqa: F401
from .tensor.search import topk # noqa: F401 from .tensor.search import topk # noqa: F401
from .tensor.search import where # noqa: F401 from .tensor.search import where # noqa: F401
...@@ -443,6 +444,7 @@ __all__ = [ # noqa ...@@ -443,6 +444,7 @@ __all__ = [ # noqa
'flops', 'flops',
'sort', 'sort',
'searchsorted', 'searchsorted',
'bucketize',
'split', 'split',
'logical_and', 'logical_and',
'full_like', 'full_like',
......
# Copyright (c) 2022 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.
from __future__ import print_function
from re import X
import unittest
import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid import Program, program_guard
np.random.seed(10)
class TestBucketizeAPI(unittest.TestCase):
# test paddle.tensor.math.nanmean
def setUp(self):
self.sorted_sequence = np.array([2, 4, 8, 16]).astype("float64")
self.x = np.array([[0, 8, 4, 16], [-1, 2, 8, 4]]).astype("float64")
self.place = [paddle.CPUPlace()]
if core.is_compiled_with_cuda():
self.place.append(paddle.CUDAPlace(0))
def test_api_static(self):
paddle.enable_static()
def run(place):
with paddle.static.program_guard(paddle.static.Program()):
sorted_sequence = paddle.static.data(
'SortedSequence',
shape=self.sorted_sequence.shape,
dtype="float64")
x = paddle.static.data('x', shape=self.x.shape, dtype="float64")
out1 = paddle.bucketize(x, sorted_sequence)
out2 = paddle.bucketize(x, sorted_sequence, right=True)
exe = paddle.static.Executor(place)
res = exe.run(feed={
'SortedSequence': self.sorted_sequence,
'x': self.x
},
fetch_list=[out1, out2])
out_ref = np.searchsorted(self.sorted_sequence, self.x)
out_ref1 = np.searchsorted(self.sorted_sequence,
self.x,
side='right')
self.assertTrue(np.allclose(out_ref, res[0]))
self.assertTrue(np.allclose(out_ref1, res[1]))
for place in self.place:
run(place)
def test_api_dygraph(self):
def run(place):
paddle.disable_static(place)
sorted_sequence = paddle.to_tensor(self.sorted_sequence)
x = paddle.to_tensor(self.x)
out1 = paddle.bucketize(x, sorted_sequence)
out2 = paddle.bucketize(x, sorted_sequence, right=True)
out_ref1 = np.searchsorted(self.sorted_sequence, self.x)
out_ref2 = np.searchsorted(self.sorted_sequence,
self.x,
side='right')
self.assertEqual(np.allclose(out_ref1, out1.numpy()), True)
self.assertEqual(np.allclose(out_ref2, out2.numpy()), True)
paddle.enable_static()
for place in self.place:
run(place)
def test_out_int32(self):
paddle.disable_static()
sorted_sequence = paddle.to_tensor(self.sorted_sequence)
x = paddle.to_tensor(self.x)
out = paddle.bucketize(x, sorted_sequence, out_int32=True)
self.assertTrue(out.type, 'int32')
def test_bucketize_dims_error(self):
with paddle.static.program_guard(paddle.static.Program()):
sorted_sequence = paddle.static.data('SortedSequence',
shape=[2, 2],
dtype="float64")
x = paddle.static.data('x', shape=[2, 5], dtype="float64")
self.assertRaises(ValueError, paddle.bucketize, x, sorted_sequence)
def test_input_error(self):
for place in self.place:
paddle.disable_static(place)
sorted_sequence = paddle.to_tensor(self.sorted_sequence)
self.assertRaises(ValueError, paddle.bucketize, self.x,
sorted_sequence)
def test_empty_input_error(self):
for place in self.place:
paddle.disable_static(place)
sorted_sequence = paddle.to_tensor(self.sorted_sequence)
x = paddle.to_tensor(self.x)
self.assertRaises(ValueError, paddle.bucketize, None,
sorted_sequence)
self.assertRaises(AttributeError, paddle.bucketize, x, None)
if __name__ == "__main__":
unittest.main()
...@@ -250,6 +250,7 @@ from .search import argmax # noqa: F401 ...@@ -250,6 +250,7 @@ from .search import argmax # noqa: F401
from .search import argmin # noqa: F401 from .search import argmin # noqa: F401
from .search import argsort # noqa: F401 from .search import argsort # noqa: F401
from .search import searchsorted # noqa: F401 from .search import searchsorted # noqa: F401
from .search import bucketize # noqa: F401
from .search import topk # noqa: F401 from .search import topk # noqa: F401
from .search import where # noqa: F401 from .search import where # noqa: F401
from .search import index_select # noqa: F401 from .search import index_select # noqa: F401
...@@ -503,6 +504,7 @@ tensor_method_func = [ #noqa ...@@ -503,6 +504,7 @@ tensor_method_func = [ #noqa
'put_along_axis_', 'put_along_axis_',
'exponential_', 'exponential_',
'heaviside', 'heaviside',
'bucketize',
] ]
#this list used in math_op_patch.py for magic_method bind #this list used in math_op_patch.py for magic_method bind
......
...@@ -914,6 +914,61 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): ...@@ -914,6 +914,61 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None):
return values, indices return values, indices
def bucketize(x, sorted_sequence, out_int32=False, right=False, name=None):
"""
This API is used to find the index of the corresponding 1D tensor `sorted_sequence` in the innermost dimension based on the given `x`.
Args:
x(Tensor): An input N-D tensor value with type int32, int64, float32, float64.
sorted_sequence(Tensor): An input 1-D tensor with type int32, int64, float32, float64. The value of the tensor monotonically increases in the innermost dimension.
out_int32(bool, optional): Data type of the output tensor which can be int32, int64. The default value is False, and it indicates that the output data type is int64.
right(bool, optional): Find the upper or lower bounds of the sorted_sequence range in the innermost dimension based on the given `x`. If the value of the sorted_sequence is nan or inf, return the size of the innermost dimension.
The default value is False and it shows the lower bounds.
name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Tensor(the same sizes of the `x`), return the tensor of int32 if set :attr:`out_int32` is True, otherwise return the tensor of int64.
Examples:
.. code-block:: python
import paddle
sorted_sequence = paddle.to_tensor([2, 4, 8, 16], dtype='int32')
x = paddle.to_tensor([[0, 8, 4, 16], [-1, 2, 8, 4]], dtype='int32')
out1 = paddle.bucketize(x, sorted_sequence)
print(out1)
# Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True,
# [[0, 2, 1, 3],
# [0, 0, 2, 1]])
out2 = paddle.bucketize(x, sorted_sequence, right=True)
print(out2)
# Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True,
# [[0, 3, 2, 4],
# [0, 1, 3, 2]])
out3 = x.bucketize(sorted_sequence)
print(out3)
# Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True,
# [[0, 2, 1, 3],
# [0, 0, 2, 1]])
out4 = x.bucketize(sorted_sequence, right=True)
print(out4)
# Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True,
# [[0, 3, 2, 4],
# [0, 1, 3, 2]])
"""
check_variable_and_dtype(sorted_sequence, 'SortedSequence',
['float32', 'float64', 'int32', 'int64'],
'paddle.searchsorted')
if sorted_sequence.dim() != 1:
raise ValueError(
f"sorted_sequence tensor must be 1 dimension, but got dim {sorted_sequence.dim()}"
)
return searchsorted(sorted_sequence, x, out_int32, right, name)
def searchsorted(sorted_sequence, def searchsorted(sorted_sequence,
values, values,
out_int32=False, out_int32=False,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册