-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
add nansum api to math #38137
Merged
Merged
add nansum api to math #38137
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
29b2698
add nansum api
esythan b43ef83
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
esythan 111d418
delete layerhelper
esythan 281769e
add nansum to all and tensor_method_func
esythan 3d074e5
update doc
esythan 8702dc0
update doc
esythan e50f724
update doc
esythan 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
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,101 @@ | ||
# Copyright (c) 2018 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 | ||
|
||
|
||
class API_Test_Nansum(unittest.TestCase): | ||
def test_static_graph(self): | ||
paddle.enable_static() | ||
startup_program = fluid.Program() | ||
train_program = fluid.Program() | ||
with fluid.program_guard(train_program, startup_program): | ||
input = fluid.data(name='input', dtype='float32', shape=[2, 4]) | ||
out1 = paddle.nansum(input) | ||
out2 = paddle.nansum(input, axis=0) | ||
out3 = paddle.nansum(input, axis=-1) | ||
out4 = paddle.nansum(input, axis=1, keepdim=True) | ||
place = fluid.CPUPlace() | ||
if fluid.core.is_compiled_with_cuda(): | ||
place = fluid.CUDAPlace(0) | ||
exe = fluid.Executor(place) | ||
exe.run(startup_program) | ||
|
||
x = np.array([[float('nan'), 3, 5, 9], | ||
[1, 2, float('-nan'), 7]]).astype(np.float32) | ||
res = exe.run(train_program, | ||
feed={'input': x}, | ||
fetch_list=[out1, out2, out3, out4]) | ||
|
||
out1_np = np.array(res[0]) | ||
out2_np = np.array(res[1]) | ||
out3_np = np.array(res[2]) | ||
out4_np = np.array(res[3]) | ||
out1_ref = np.array([27]).astype(np.float32) | ||
out2_ref = np.array([1, 5, 5, 16]).astype(np.float32) | ||
out3_ref = np.array([17, 10]).astype(np.float32) | ||
out4_ref = np.array([[17], [10]]).astype(np.float32) | ||
|
||
self.assertTrue( | ||
(out1_np == out1_ref).all(), | ||
msg='nansum output is wrong, out =' + str(out1_np)) | ||
self.assertTrue( | ||
(out2_np == out2_ref).all(), | ||
msg='nansum output is wrong, out =' + str(out2_np)) | ||
self.assertTrue( | ||
(out3_np == out3_ref).all(), | ||
msg='nansum output is wrong, out =' + str(out3_np)) | ||
self.assertTrue( | ||
(out4_np == out4_ref).all(), | ||
msg='nansum output is wrong, out =' + str(out4_np)) | ||
|
||
def test_error_api(self): | ||
paddle.enable_static() | ||
|
||
## input dtype error | ||
def run1(): | ||
input = fluid.data(name='input', dtype='float16', shape=[2, 3]) | ||
output = paddle.nansum(input) | ||
|
||
self.assertRaises(TypeError, run1) | ||
|
||
## axis type error | ||
def run2(): | ||
input = fluid.data(name='input', dtype='float16', shape=[2, 3]) | ||
output = paddle.nansum(input, axis=1.2) | ||
|
||
self.assertRaises(TypeError, run2) | ||
|
||
def test_dygraph(self): | ||
x = np.array([[float('nan'), 3, 5, 9], | ||
[1, 2, float('-nan'), 7]]).astype(np.float32) | ||
with fluid.dygraph.guard(): | ||
inputs = fluid.dygraph.to_variable(x) | ||
out = paddle.nansum(inputs) | ||
out_ref = np.array([27]).astype(np.float32) | ||
|
||
self.assertTrue( | ||
(out.numpy() == out_ref).all(), | ||
msg='nansum output is wrong, out =' + str(out.numpy())) | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,7 @@ | |
from .math import square # noqa: F401 | ||
from .math import stanh # noqa: F401 | ||
from .math import sum # noqa: F401 | ||
from .math import nansum # noqa: F401 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for paddle.Tensor.nansum, we should add nansum in list of tensor_method_func below |
||
from .math import tanh # noqa: F401 | ||
from .math import tanh_ # noqa: F401 | ||
from .math import add_n # noqa: F401 | ||
|
@@ -298,6 +299,7 @@ | |
'square', | ||
'stanh', | ||
'sum', | ||
'nansum', | ||
'tanh', | ||
'tanh_', | ||
'add_n', | ||
|
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.
public api should be add in all list below