add acos acosh cos tensor interface

This commit is contained in:
mengyuanli 2022-08-19 09:27:55 +08:00
parent cac1150345
commit 3da9d64278
7 changed files with 160 additions and 0 deletions

View File

@ -284,6 +284,9 @@ BuiltInTypeMap &GetMethodMap() {
{"argmin_with_value", std::string("argmin_with_value")}, // P.ArgMinWithValue
{"top_k", std::string("top_k")}, // P.TopK()
{"isfinite", std::string("isfinite")}, // P.isfinite()
{"cos", std::string("cos")}, // cos()
{"acos", std::string("acos")}, // acos()
{"acosh", std::string("acosh")}, // acosh()
}},
{kObjectTypeRowTensorType,
{

View File

@ -2820,3 +2820,24 @@ def isfinite(x):
Refer to :func:`mindspore.ops.isfinite` for more details.
"""
return F.isfinite(x)
def cos(x):
r"""
Computes cosine of input element-wise.
"""
return F.cos(x)
def acos(x):
r"""
Computes arccosine of input tensors element-wise.
"""
return F.acos(x)
def acosh(x):
r"""
Computes inverse hyperbolic cosine of the inputs element-wise.
"""
return F.acosh(x)

View File

@ -1120,6 +1120,85 @@ class Tensor(Tensor_):
self._init_check()
return tensor_operator_registry.get('cosh')()(self)
def acos(self):
r"""
Computes arccosine of input tensors element-wise.
.. math::
out_i = cos^{-1}(x_i)
Returns:
Tensor, has the same shape as `x`.
Supported Platforms:
``Ascend`` ``GPU`` ``CPU``
Examples:
>>> from mindspore import Tensor
>>> a = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32)
>>> output = a.acos()
>>> print(output)
[0.737726 1.5307857 1.2661036 0.9764105]
"""
self._init_check()
return tensor_operator_registry.get('acos')(self)
def cos(self):
r"""
Computes cosine of input element-wise.
.. math::
out_i = cos(x_i)
.. warning::
Currently support Float16, Float32 data type. If use Float64, there may
be a problem of missing precision.
Returns:
Tensor, has the same shape as `x`.
Supported Platforms:
``Ascend`` ``GPU`` ``CPU``
Examples:
>>> from mindspore import Tensor
>>> a = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32)
>>> output = a.cos()
>>> print(output)
[0.971338 0.6748758 0.95233357 0.9959527]
"""
self._init_check()
return tensor_operator_registry.get('cos')(self)
def acosh(self):
r"""
Computes inverse hyperbolic cosine of the inputs element-wise.
.. math::
out_i = \cosh^{-1}(input_i)
.. warning::
Given an input tensor x, the function computes inverse hyperbolic cosine of every element.
Input range is [1, inf].
Returns:
Tensor, has the same shape as `x`.
Supported Platforms:
``Ascend`` ``GPU`` ``CPU``
Examples:
>>> from mindspore import Tensor
>>> a = Tensor(np.array([1.0, 1.5, 3.0, 100.0]), mindspore.float32)
>>> output = a.acosh()
>>> print(output)
[0. 0.9624237 1.7627472 5.298292]
"""
self._init_check()
return tensor_operator_registry.get('acosh')(self)
def abs(self):
"""
Return absolute value element-wisely.

View File

@ -347,6 +347,9 @@ tensor_operator_registry.register('any', P.ReduceAny)
tensor_operator_registry.register('atan2', atan2)
tensor_operator_registry.register('abs', P.Abs)
tensor_operator_registry.register('tan', P.Tan)
tensor_operator_registry.register('acos', acos)
tensor_operator_registry.register('cos', cos)
tensor_operator_registry.register('acosh', acosh)
tensor_operator_registry.register('cosh', P.Cosh)
tensor_operator_registry.register('pow', P.Pow)
tensor_operator_registry.register('amin', amin)

View File

@ -50,3 +50,21 @@ def test_acos(dtype):
print(output)
expect = np.arccos(np_array)
assert np.allclose(output.asnumpy(), expect)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
def test_acos_tensor(dtype):
"""
Feature: acos tensor interface
Description: test the rightness of acos tensor interface
Expectation: Success.
"""
np_array = np.array([-1, -0.5, 0, 0.5, 1]).astype(dtype)
input_x = Tensor(np_array)
output = input_x.acos()
print(output)
expect = np.arccos(np_array)
assert np.allclose(output.asnumpy(), expect)

View File

@ -50,3 +50,21 @@ def test_acosh(dtype):
print(output)
expect = np.arccosh(np_array)
assert np.allclose(output.asnumpy(), expect)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
def test_acosh_tensor(dtype):
"""
Feature: acosh tensor interface
Description: test the rightness of acosh tensor interface
Expectation: Success.
"""
np_array = np.array([1, 2, 3, 4, 5]).astype(dtype)
input_x = Tensor(np_array)
output = input_x.acosh()
print(output)
expect = np.arccosh(np_array)
assert np.allclose(output.asnumpy(), expect)

View File

@ -52,3 +52,21 @@ def test_cos():
print(output)
expect = np.cos(np_array)
assert np.allclose(output.asnumpy(), expect)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
def test_cos_tensor(dtype):
"""
Feature: cos tensor interface
Description: test the rightness of cos tensor interface
Expectation: Success.
"""
np_array = np.array([-1, -0.5, 0, 0.5, 1]).astype(dtype)
input_x = Tensor(np_array)
output = input_x.cos()
print(output)
expect = np.cos(np_array)
assert np.allclose(output.asnumpy(), expect)