-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【PaddlePaddle Hackathon 3 No.5】为 Paddle 新增 bucketize #44195
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d48548
add paddle.bucketize api
Li-fAngyU 5be4e33
updata paddle.bucketize code style.
Li-fAngyU e7070ab
upgrade unittests/test_bucketize_api.py.
Li-fAngyU 056cf0a
Merge branch 'PaddlePaddle:develop' into develop
Li-fAngyU c1b11e1
update tests/unittests/test_bucketize_api.py file.
Li-fAngyU 5cf4741
Merge branch 'PaddlePaddle:develop' into develop
Li-fAngyU d2e5a9a
修改bucketize API 的英文参数描述
Li-fAngyU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
python/paddle/fluid/tests/unittests/test_bucketize_api.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a test case "错误检查:未输入x和sorted_sequence时,能否正确抛出错误" in rfc, shall we add this test case?