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
【Hackathon No.27】为 Paddle 新增 frac 数学计算API #41226
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# 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 | ||
|
||
|
||
def ref_frac(x): | ||
return x - np.trunc(x) | ||
|
||
|
||
class TestFracAPI(unittest.TestCase): | ||
def setUp(self): | ||
self.x_np = np.random.uniform(-3, 3, [2, 3]).astype('float64') | ||
self.place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda() \ | ||
else paddle.CPUPlace() | ||
|
||
def test_static_api(self): | ||
paddle.enable_static() | ||
with program_guard(Program()): | ||
input = fluid.data('X', self.x_np.shape, self.x_np.dtype) | ||
out = paddle.frac(input) | ||
place = fluid.CPUPlace() | ||
if fluid.core.is_compiled_with_cuda(): | ||
place = fluid.CUDAPlace(0) | ||
exe = fluid.Executor(place) | ||
res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) | ||
out_ref = ref_frac(self.x_np) | ||
self.assertTrue(np.allclose(out_ref, res)) | ||
|
||
def test__dygraph_api(self): | ||
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. test__dygraph_api->test_dygraph_api |
||
paddle.disable_static(self.place) | ||
x = paddle.to_tensor(self.x_np) | ||
out = paddle.frac(x) | ||
out_ref = ref_frac(self.x_np) | ||
self.assertTrue(np.allclose(out_ref, out.numpy())) | ||
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. 根据验收标准,请多补充下测试case。
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,7 @@ | |
from .math import fmin # noqa: F401 | ||
from .math import inner # noqa: F401 | ||
from .math import outer # noqa: F401 | ||
from .math import frac # noqa: F401 | ||
|
||
from .random import multinomial # noqa: F401 | ||
from .random import standard_normal # noqa: F401 | ||
|
@@ -452,6 +453,7 @@ | |
'digamma', | ||
'diagonal', | ||
'trunc', | ||
'frac' | ||
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.
static-check流水线显示:https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/5338157/job/13849637 |
||
'bitwise_and', | ||
'bitwise_or', | ||
'bitwise_xor', | ||
|
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.
需要在下面的
__all__
列表里加上frac