Skip to content
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

Merged
merged 29 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3d43a00
init atanh
WangYizhang01 May 21, 2021
1bddb9c
add atanh
WangYizhang01 May 24, 2021
4f9d9e8
add /n in the tail'
WangYizhang01 May 24, 2021
6dd8de3
Merge branch 'master' into wyz
WangYizhang01 May 25, 2021
17d3f7a
update atanh
WangYizhang01 May 26, 2021
9a6f95b
Merge branch 'wyz' of https://github.com/Oneflow-Inc/oneflow into wyz
WangYizhang01 May 26, 2021
c1cc17a
format
WangYizhang01 May 28, 2021
cd3da54
add tan
WangYizhang01 May 28, 2021
408a109
add test_square.py test_sqrt.py
WangYizhang01 May 28, 2021
26163dc
update test_atanh grad
WangYizhang01 May 31, 2021
99493c0
update test_tensor.py
WangYizhang01 May 31, 2021
0e7a848
add doctest
WangYizhang01 May 31, 2021
7ee49fe
update
WangYizhang01 Jun 3, 2021
954e61b
split tensor
WangYizhang01 Jun 4, 2021
4afb156
add 3 dim testcase
WangYizhang01 Jun 4, 2021
ac076fb
resolve conflict
WangYizhang01 Jun 4, 2021
9c18d7d
Merge branch 'master' into wyz
WangYizhang01 Jun 4, 2021
748c754
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
3a32727
auto format by CI
oneflow-ci-bot Jun 4, 2021
d7ff28d
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
a4af390
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
d726d7b
Update test_atanh.py
aajjjtntn Jun 4, 2021
c9275bc
Revert "Update test_atanh.py"
jackalcooper Jun 4, 2021
7ba731b
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
dc0fa9f
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
7c73ca1
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
55d54c9
fix
jackalcooper Jun 4, 2021
133ac8b
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
6698c02
Merge branch 'master' into wyz
oneflow-ci-bot Jun 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/source/experimental.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,11 @@ Experimental features
.. autofunction:: oneflow.experimental.nn.Upsample
.. autofunction:: oneflow.experimental.nn.UpsamplingNearest2d
.. autofunction:: oneflow.experimental.nn.UpsamplingBilinear2d
.. autofunction:: oneflow.experimental.atanh
.. autofunction:: oneflow.experimental.Tensor.atanh
.. autofunction:: oneflow.experimental.arctanh
.. autofunction:: oneflow.experimental.Tensor.arctanh
.. autofunction:: oneflow.experimental.tan
.. autofunction:: oneflow.experimental.Tensor.tan
.. autofunction:: oneflow.experimental.log1p
.. autofunction:: oneflow.experimental.Tensor.log1p
95 changes: 95 additions & 0 deletions oneflow/python/nn/modules/atanh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
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 oneflow as flow
from oneflow.python.oneflow_export import oneflow_export, experimental_api
from oneflow.python.nn.module import Module
from oneflow.python.framework.tensor import register_tensor_op


class Atanh(Module):
def __init__(self):
super().__init__()
self._op = flow.builtin_op("atanh").Input("x").Output("y").Build()

def forward(self, x):
return self._op(x)[0]


@oneflow_export("atanh")
@experimental_api
def atanh_op(input):
r"""Returns a new tensor with the inverse hyperbolic tangent of the elements of :attr:`input`.

.. math::
\text{out}_{i} = \tanh^{-1}(\text{input}_{i})
Args:
input (Tensor): the input tensor.

For example:

.. code-block:: python

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> np_arr = np.array([0.5, 0.6, 0.7]).astype(np.float32)
>>> input = flow.Tensor(np_arr)
>>> output = flow.atanh(input)
>>> print(output.numpy())
[0.54930615 0.6931472 0.8673005 ]
"""

return Atanh()(input)


@register_tensor_op("atanh")
@experimental_api
def atanh_op_tensor(x):
r"""
atanh() -> Tensor
See :func:`oneflow.experimental.atanh`

"""

return Atanh()(x)


@oneflow_export("arctanh")
@experimental_api
def arctanh_op(input):
r"""

Alias for :func:`oneflow.experimental.atanh`
"""

return Atanh()(input)


@register_tensor_op("arctanh")
@experimental_api
def arctanh_op_tensor(input):
r"""

Alias for :func:`oneflow.experimental.atanh`
"""

return Atanh()(input)


if __name__ == "__main__":
import doctest

doctest.testmod(raise_on_error=True)
73 changes: 73 additions & 0 deletions oneflow/python/nn/modules/tan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
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 oneflow as flow
from oneflow.python.oneflow_export import oneflow_export, experimental_api
from oneflow.python.nn.module import Module
from oneflow.python.framework.tensor import register_tensor_op


class Tan(Module):
def __init__(self):
super().__init__()
self._op = flow.builtin_op("tan").Input("x").Output("y").Build()

def forward(self, x):
return self._op(x)[0]


@oneflow_export("tan")
@experimental_api
def tan_op(input):
r"""Returns the tan value of the elements of :attr:`input`.

.. math::
\text{out}_{i} = \tan(\text{input}_{i})
Args:
input (Tensor): the input tensor.

For example:

.. code-block:: python

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> np_arr = np.array([-1/4*np.pi, 0, 1/4*np.pi]).astype(np.float32)
>>> input = flow.Tensor(np_arr)
>>> output = flow.tan(input)
>>> print(output.numpy())
[-1. 0. 1.]
"""

return Tan()(input)


@register_tensor_op("tan")
@experimental_api
def tan_op_tensor(input):
r"""
tan() -> Tensor
See :func:`oneflow.experimental.tan`

"""

return Tan()(input)


if __name__ == "__main__":
import doctest

doctest.testmod(raise_on_error=True)
80 changes: 80 additions & 0 deletions oneflow/python/test/modules/test_atanh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
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(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)
)


def _test_arctanh_impl(test_case, shape, device):
np_input = np.random.random(shape)
of_input = flow.Tensor(
np_input, dtype=flow.float32, device=flow.device(device), requires_grad=True
)

of_out = flow.arctanh(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, 3, 4), (2, 4, 5, 6)]
arg_dict["device"] = ["cpu", "cuda"]
for arg in GenArgList(arg_dict):
_test_atanh_impl(test_case, *arg)
_test_arctanh_impl(test_case, *arg)


if __name__ == "__main__":
unittest.main()
59 changes: 59 additions & 0 deletions oneflow/python/test/modules/test_tan.py
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试样例中缺少对Tensor.xxx方法的测试,Tensor.xxx方法的测试放到test_tensor.py中。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你再确认一个问题就差不多了,就是确定一下这些函数的定义域,随机生成数据的时候限制一下,让它们的值域不溢出,比如变成nan。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,刚又确认了一下,定义域没有问题。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approve了,合进来吧。

Copy link
Contributor

Choose a reason for hiding this comment

The 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, 3, 4), (2, 4, 5, 6)]
arg_dict["device"] = ["cpu", "cuda"]
for arg in GenArgList(arg_dict):
_test_tan_impl(test_case, *arg)


if __name__ == "__main__":
unittest.main()
63 changes: 63 additions & 0 deletions oneflow/python/test/tensor/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,69 @@ def test_pow_tensor_function(test_case):
np_out = np.power(input.numpy(), 2.1)
test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))

@unittest.skipIf(
not flow.unittest.env.eager_execution_enabled(),
"numpy doesn't work in lazy mode",
)
def test_tensor_atanh(test_case):
np_input = np.random.random((2, 3))
of_input = flow.Tensor(np_input, dtype=flow.float32, requires_grad=True)

of_out = of_input.atanh()
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",
)
def test_tensor_arctanh(test_case):
np_input = np.random.random((2, 3))
of_input = flow.Tensor(np_input, dtype=flow.float32, requires_grad=True)

of_out = of_input.arctanh()
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",
)
def test_tensor_tan(test_case):
np_input = np.random.random((2, 3))
of_input = flow.Tensor(np_input, dtype=flow.float32, requires_grad=True)

of_out = of_input.tan()
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)
)


if __name__ == "__main__":
unittest.main()