add Tensor.rad2deg deg2rad

This commit is contained in:
emmmmtang 2022-11-09 15:46:45 +08:00
parent 458bb025dc
commit 09c07f5ca9
14 changed files with 164 additions and 3 deletions

View File

@ -181,6 +181,7 @@ mindspore.ops.function
mindspore.ops.ceil
mindspore.ops.cos
mindspore.ops.cosh
mindspore.ops.deg2rad
mindspore.ops.div
mindspore.ops.erf
mindspore.ops.erfc
@ -209,6 +210,7 @@ mindspore.ops.function
mindspore.ops.pinv
mindspore.ops.positive
mindspore.ops.pow
mindspore.ops.rad2deg
mindspore.ops.remainder
mindspore.ops.roll
mindspore.ops.round

View File

@ -0,0 +1,6 @@
mindspore.Tensor.deg2rad
=========================
.. py:method:: mindspore.Tensor.deg2rad()
详情请参考 :func:`mindspore.ops.deg2rad`

View File

@ -0,0 +1,6 @@
mindspore.Tensor.rad2deg
=========================
.. py:method:: mindspore.Tensor.rad2deg()
详情请参考 :func:`mindspore.ops.rad2deg`

View File

@ -76,6 +76,7 @@ mindspore.Tensor
mindspore.Tensor.cummin
mindspore.Tensor.cumprod
mindspore.Tensor.cumsum
mindspore.Tensor.deg2rad
mindspore.Tensor.det
mindspore.Tensor.diag
mindspore.Tensor.diagonal
@ -167,6 +168,7 @@ mindspore.Tensor
mindspore.Tensor.pow
mindspore.Tensor.prod
mindspore.Tensor.ptp
mindspore.Tensor.rad2deg
mindspore.Tensor.random_categorical
mindspore.Tensor.ravel
mindspore.Tensor.remainder

View File

@ -0,0 +1,16 @@
mindspore.ops.deg2rad
======================
.. py:function:: mindspore.ops.deg2rad(x)
计算一个新的Tensor其中 `x` 的每个角度元素都从度转换为弧度。
参数:
- **x** (Tensor[Number]) - 输入的Tensor。其必须是一个正定矩阵数据类型为float16float32或float64。
返回:
Tensor其数据类型与 `x` 相同。
异常:
- **TypeError** - 如果 `x` 不是一个Tensor。
- **TypeError** - 如果 `x` 的数据类型不是float16float32或float64。

View File

@ -0,0 +1,16 @@
mindspore.ops.rad2deg
======================
.. py:function:: mindspore.ops.rad2deg(x)
计算一个新的Tensor其中 `x` 的每个角度元素都从弧度转换为度。
参数:
- **x** (Tensor) - 输入的Tensor。
返回:
Tensor其数据类型和shape与 `x` 相同。
异常:
- **TypeError** - 如果 `x` 不是一个Tensor。
- **TypeError** - 如果 `x` 的数据类型不是float16float32或float64。

View File

@ -82,6 +82,7 @@
mindspore.Tensor.cummin
mindspore.Tensor.cumprod
mindspore.Tensor.cumsum
mindspore.Tensor.deg2rad
mindspore.Tensor.det
mindspore.Tensor.diag
mindspore.Tensor.diagonal
@ -173,6 +174,7 @@
mindspore.Tensor.pow
mindspore.Tensor.prod
mindspore.Tensor.ptp
mindspore.Tensor.rad2deg
mindspore.Tensor.random_categorical
mindspore.Tensor.ravel
mindspore.Tensor.remainder

View File

@ -182,6 +182,7 @@ Element-by-Element Operations
mindspore.ops.ceil
mindspore.ops.cos
mindspore.ops.cosh
mindspore.ops.deg2rad
mindspore.ops.div
mindspore.ops.erf
mindspore.ops.erfc
@ -210,6 +211,7 @@ Element-by-Element Operations
mindspore.ops.pinv
mindspore.ops.positive
mindspore.ops.pow
mindspore.ops.rad2deg
mindspore.ops.remainder
mindspore.ops.roll
mindspore.ops.round

View File

@ -257,6 +257,8 @@ BuiltInTypeMap &GetMethodMap() {
{"minimum", std::string("minimum")}, // P.Minimum()
{"cosh", std::string("cosh")}, // P.Cosh()
{"tanh", std::string("tanh")}, // P.Tanh()
{"rad2deg", std::string("rad2deg")}, // F.rad2deg()
{"deg2rad", std::string("deg2rad")}, // F.deg2rad()
{"round", std::string("round_")}, // P.Round()
{"roll", std::string("roll")}, // P.Roll()
{"rot90", std::string("rot90")}, // rot90()

View File

@ -1084,6 +1084,20 @@ def rot90(x, k, dims):
return F.rot90(x, k, dims)
def rad2deg(x):
"""
Returns a new tensor with each of the elements of `x` converted from angles in radians to degrees.
"""
return F.rad2deg(x)
def deg2rad(x):
"""
Calculates a new tensor with each of the elements of `x` converted from angles in degrees to radians.
"""
return F.deg2rad(x)
def numel(x):
"""
Returns a Scalar of type int that represents the total number of elements in the Tensor.

View File

@ -1766,6 +1766,20 @@ class Tensor(Tensor_):
self._init_check()
return tensor_operator_registry.get('rot90')(self, k, dims)
def deg2rad(self):
r"""
For details, please refer to :func:`mindspore.ops.deg2rad`.
"""
self._init_check()
return tensor_operator_registry.get('deg2rad')(self)
def rad2deg(self):
r"""
For details, please refer to :func:`mindspore.ops.rad2deg`.
"""
self._init_check()
return tensor_operator_registry.get('rad2deg')(self)
def nelement(self):
r"""
Alias for numel().

View File

@ -2624,7 +2624,7 @@ def ldexp(x, other):
out_{i} = x_{i} * ( 2_{i} ^{other} )
.. note::
Note:
Typically this function can create floating point numbers
by multiplying mantissas in input with powers of intger 2
from the exponents in `other`.
@ -4053,11 +4053,11 @@ def lcm(x1, x2):
The shape of two inputs should be broadcastable, and data type of them should be
one of: int32, int64
Inputs:
Args:
- **x1** (Tensor) - The first input tensor.
- **x2** (Tensor) - The second input tensor.
Outputs:
Returns:
Tensor, the shape is the same as the one after broadcasting, and the data type is one
with higher digits in the two inputs.

View File

@ -342,6 +342,8 @@ tensor_operator_registry.register('sort', P.Sort)
tensor_operator_registry.register('trunc', trunc)
tensor_operator_registry.register('imag', imag)
tensor_operator_registry.register('repeat_interleave', repeat_elements)
tensor_operator_registry.register('rad2deg', rad2deg)
tensor_operator_registry.register('deg2rad', deg2rad)
tensor_operator_registry.register('roll', Roll)
tensor_operator_registry.register('rot90', rot90)

View File

@ -0,0 +1,77 @@
# Copyright 2022 Huawei Technologies Co., Ltd
#
# 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 numpy as np
import pytest
import mindspore as ms
import mindspore.nn as nn
from mindspore import Tensor
class Deg2radNet(nn.Cell):
def construct(self, x):
return x.deg2rad()
class Rad2degNet(nn.Cell):
def construct(self, x):
return x.rad2deg()
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.platform_arm_cpu
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
def test_deg2rad(mode):
"""
Feature: test Tensor.deg2rad
Description: Verify the result of Tensor.deg2rad
Expectation: expect correct forward result
"""
ms.set_context(mode=mode)
x_np = np.array([[90.0, -90.0], [180.0, -180.0], [270.0, -270.0]]).astype(np.float32)
x = Tensor(x_np, ms.float32)
net = Deg2radNet()
output_ms = net(x)
expect_output = np.deg2rad(x_np)
assert np.allclose(output_ms.asnumpy(), expect_output)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.platform_arm_cpu
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
def test_rad2deg(mode):
"""
Feature: test Tensor.rad2deg
Description: Verify the result of Tensor.rad2deg
Expectation: expect correct forward result
"""
ms.set_context(mode=mode)
x_np = np.array([[6.283, -3.142], [1.570, -6.283], [3.142, -1.570]]).astype(np.float32)
x = Tensor(x_np, ms.float32)
net = Rad2degNet()
output_ms = net(x)
expect_output = np.rad2deg(x_np)
assert np.allclose(output_ms.asnumpy(), expect_output)