-
Notifications
You must be signed in to change notification settings - Fork 697
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
atanh module #4960
atanh module #4960
Changes from 10 commits
3d43a00
1bddb9c
4f9d9e8
6dd8de3
17d3f7a
9a6f95b
c1cc17a
cd3da54
408a109
26163dc
99493c0
0e7a848
7ee49fe
954e61b
4afb156
ac076fb
9c18d7d
748c754
3a32727
d7ff28d
a4af390
d726d7b
c9275bc
7ba731b
dc0fa9f
7c73ca1
55d54c9
133ac8b
6698c02
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,59 @@ | ||
""" | ||
Copyright 2020 The OneFlow 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. | ||
""" | ||
import unittest | ||
from collections import OrderedDict | ||
|
||
import numpy as np | ||
|
||
import oneflow.experimental as flow | ||
from test_util import GenArgList | ||
|
||
|
||
def _test_atanh_impl(test_case, shape, device): | ||
np_input = np.random.random(size=shape) | ||
of_input = flow.Tensor( | ||
np_input, dtype=flow.float32, device=flow.device(device), requires_grad=True | ||
) | ||
|
||
of_out = flow.atanh(of_input) | ||
np_out = np.arctanh(np_input) | ||
test_case.assertTrue( | ||
np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
of_out = of_out.sum() | ||
of_out.backward() | ||
np_out_grad = 1.0 / (1.0 - np.square(np_input)) | ||
test_case.assertTrue( | ||
np.allclose(of_input.grad.numpy(), np_out_grad, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
|
||
@unittest.skipIf( | ||
not flow.unittest.env.eager_execution_enabled(), | ||
".numpy() doesn't work in lazy mode", | ||
) | ||
class TestAtanh(flow.unittest.TestCase): | ||
def test_atanh(test_case): | ||
arg_dict = OrderedDict() | ||
arg_dict["shape"] = [(2, 3), (2, 4, 5, 6)] | ||
arg_dict["device"] = ["cpu", "cuda"] | ||
for arg in GenArgList(arg_dict): | ||
_test_atanh_impl(test_case, *arg) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Copyright 2020 The OneFlow 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. | ||
""" | ||
import unittest | ||
from collections import OrderedDict | ||
|
||
import numpy as np | ||
|
||
import oneflow.experimental as flow | ||
from test_util import GenArgList | ||
|
||
|
||
def _test_sqrt_impl(test_case, shape, device): | ||
np_input = np.random.random(size=shape) | ||
of_input = flow.Tensor( | ||
np_input, dtype=flow.float32, device=flow.device(device), requires_grad=True | ||
) | ||
|
||
of_out = flow.sqrt(of_input) | ||
np_out = np.sqrt(np_input) | ||
test_case.assertTrue( | ||
np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
of_out = of_out.sum() | ||
of_out.backward() | ||
np_out_grad = 1.0 / (2 * np_out) | ||
test_case.assertTrue( | ||
np.allclose(of_input.grad.numpy(), np_out_grad, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
|
||
@unittest.skipIf( | ||
not flow.unittest.env.eager_execution_enabled(), | ||
".numpy() doesn't work in lazy mode", | ||
) | ||
class TestSqrt(flow.unittest.TestCase): | ||
def test_sqrt(test_case): | ||
arg_dict = OrderedDict() | ||
arg_dict["shape"] = [(2, 3), (2, 4, 5, 6)] | ||
arg_dict["device"] = ["cpu", "cuda"] | ||
for arg in GenArgList(arg_dict): | ||
_test_sqrt_impl(test_case, *arg) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Copyright 2020 The OneFlow 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. | ||
""" | ||
import unittest | ||
from collections import OrderedDict | ||
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. 这个PR为什么会改square和sqrt的测试? |
||
|
||
import numpy as np | ||
|
||
import oneflow.experimental as flow | ||
from test_util import GenArgList | ||
|
||
|
||
def _test_square_impl(test_case, shape, device): | ||
np_input = np.random.random(size=shape) | ||
of_input = flow.Tensor( | ||
np_input, dtype=flow.float32, device=flow.device(device), requires_grad=True | ||
) | ||
|
||
of_out = flow.square(of_input) | ||
np_out = np.square(np_input) | ||
test_case.assertTrue( | ||
np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
of_out = of_out.sum() | ||
of_out.backward() | ||
np_out_grad = 2 * np_input | ||
test_case.assertTrue( | ||
np.allclose(of_input.grad.numpy(), np_out_grad, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
|
||
@unittest.skipIf( | ||
not flow.unittest.env.eager_execution_enabled(), | ||
".numpy() doesn't work in lazy mode", | ||
) | ||
class TestSquare(flow.unittest.TestCase): | ||
def test_square(test_case): | ||
arg_dict = OrderedDict() | ||
arg_dict["shape"] = [(2, 3), (2, 4, 5, 6)] | ||
arg_dict["device"] = ["cpu", "cuda"] | ||
for arg in GenArgList(arg_dict): | ||
_test_square_impl(test_case, *arg) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Copyright 2020 The OneFlow 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. | ||
""" | ||
import unittest | ||
from collections import OrderedDict | ||
|
||
import numpy as np | ||
|
||
import oneflow.experimental as flow | ||
from test_util import GenArgList | ||
|
||
|
||
def _test_tan_impl(test_case, shape, device): | ||
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. 测试样例中缺少对Tensor.xxx方法的测试,Tensor.xxx方法的测试放到test_tensor.py中。 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. 好的 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. |
||
np_input = np.random.random(size=shape) | ||
of_input = flow.Tensor( | ||
np_input, dtype=flow.float32, device=flow.device(device), requires_grad=True | ||
) | ||
|
||
of_out = flow.tan(of_input) | ||
np_out = np.tan(np_input) | ||
test_case.assertTrue( | ||
np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
of_out = of_out.sum() | ||
of_out.backward() | ||
np_out_grad = 1 + np.square(np_out) | ||
test_case.assertTrue( | ||
np.allclose(of_input.grad.numpy(), np_out_grad, 1e-4, 1e-4, equal_nan=True) | ||
) | ||
|
||
|
||
@unittest.skipIf( | ||
not flow.unittest.env.eager_execution_enabled(), | ||
".numpy() doesn't work in lazy mode", | ||
) | ||
class TestTan(flow.unittest.TestCase): | ||
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. 你再确认一个问题就差不多了,就是确定一下这些函数的定义域,随机生成数据的时候限制一下,让它们的值域不溢出,比如变成nan。 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. 好的,刚又确认了一下,定义域没有问题。 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. approve了,合进来吧。 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. 不过有冲突了,你手动解决一下。 |
||
def test_tan(test_case): | ||
arg_dict = OrderedDict() | ||
arg_dict["shape"] = [(2, 3), (2, 4, 5, 6)] | ||
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. 这里加一个3dim的case 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. 好的,已添加。 |
||
arg_dict["device"] = ["cpu", "cuda"] | ||
for arg in GenArgList(arg_dict): | ||
_test_tan_impl(test_case, *arg) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
flow.xxx
和flow.Tensor.xxx
需要分别封装,这样才能让 docstring 不一样,可以参考#4987
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.
好的,已修改 @doombeaker