-
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.15】为 Paddle 新增 count_nonzero #44169
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d2fe13
add count_nonzero api
thunder95 3f8427c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 4e9c216
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 1844892
remove grad test
thunder95 6ce4fa9
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 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
112 changes: 112 additions & 0 deletions
112
python/paddle/fluid/tests/unittests/test_count_nonzero_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,112 @@ | ||
# 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 | ||
|
||
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 TestCountNonzeroAPI(unittest.TestCase): | ||
# test paddle.tensor.math.count_nonzero | ||
|
||
def setUp(self): | ||
self.x_shape = [2, 3, 4, 5] | ||
self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32) | ||
self.place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda() \ | ||
else paddle.CPUPlace() | ||
|
||
def test_api_static(self): | ||
paddle.enable_static() | ||
with paddle.static.program_guard(paddle.static.Program()): | ||
x = paddle.fluid.data('X', self.x_shape) | ||
out1 = paddle.count_nonzero(x) | ||
out2 = paddle.tensor.count_nonzero(x) | ||
out3 = paddle.tensor.math.count_nonzero(x) | ||
axis = np.arange(len(self.x_shape)).tolist() | ||
out4 = paddle.count_nonzero(x, axis) | ||
out5 = paddle.count_nonzero(x, tuple(axis)) | ||
exe = paddle.static.Executor(self.place) | ||
res = exe.run(feed={'X': self.x}, | ||
fetch_list=[out1, out2, out3, out4, out5]) | ||
out_ref = np.count_nonzero(self.x) | ||
for out in res: | ||
self.assertEqual(np.allclose(out, out_ref), True) | ||
|
||
def test_api_dygraph(self): | ||
paddle.disable_static(self.place) | ||
|
||
def test_case(x, axis=None, keepdim=False): | ||
x_tensor = paddle.to_tensor(x) | ||
out = paddle.count_nonzero(x_tensor, axis=axis, keepdim=keepdim) | ||
if isinstance(axis, list): | ||
axis = tuple(axis) | ||
if len(axis) == 0: | ||
axis = None | ||
|
||
out_ref = np.count_nonzero(x, axis, keepdims=keepdim) | ||
self.assertEqual(np.allclose(out.numpy(), out_ref), True) | ||
|
||
test_case(self.x) | ||
test_case(self.x, None) | ||
test_case(self.x, -1) | ||
test_case(self.x, keepdim=True) | ||
test_case(self.x, 2, keepdim=True) | ||
test_case(self.x, [0, 2]) | ||
test_case(self.x, (0, 2)) | ||
test_case(self.x, (0, 1, 3)) | ||
test_case(self.x, [0, 1, 2, 3]) | ||
paddle.enable_static() | ||
|
||
def test_errors(self): | ||
paddle.enable_static() | ||
with paddle.static.program_guard(paddle.static.Program()): | ||
x = paddle.fluid.data('X', [10, 12], 'int32') | ||
self.assertRaises(ValueError, paddle.count_nonzero, x, axis=10) | ||
|
||
def test_api_dygraph_grad(self): | ||
paddle.disable_static(self.place) | ||
|
||
def test_case(x, axis=None, keepdim=False): | ||
if isinstance(axis, list): | ||
axis = list(axis) | ||
if len(axis) == 0: | ||
axis = None | ||
x_tensor = paddle.to_tensor(x, stop_gradient=False) | ||
y = paddle.count_nonzero(x_tensor, axis, keepdim) | ||
dx = paddle.grad(y, x_tensor)[0].numpy() | ||
self.assertEqual( | ||
np.allclose(np.sum(np.ones_like(x, dtype=np.int64)), dx.sum()), | ||
True) | ||
|
||
test_case(self.x) | ||
test_case(self.x, None) | ||
test_case(self.x, -1) | ||
test_case(self.x, keepdim=True) | ||
test_case(self.x, 2, keepdim=True) | ||
test_case(self.x, [0, 2]) | ||
test_case(self.x, (0, 2)) | ||
test_case(self.x, (0, 1, 3)) | ||
test_case(self.x, [0, 1, 2, 3]) | ||
paddle.enable_static() | ||
|
||
|
||
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.
Is the gradient of x obtained by this code correct in math? or x should have gradient?
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.
Thanks for reminding me, Mr. Gao. Here this count operator was mistaken for sum operator by me. According to gradient law, x is not supposed to have gradient. @jeff41404