!32164 [assistant][ops] Add new Rad2Deg

Merge pull request !32164 from wxy220/Rad2Deg
This commit is contained in:
i-robot 2022-06-01 02:41:27 +00:00 committed by Gitee
commit 9351d048d4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 63 additions and 1 deletions

View File

@ -184,6 +184,7 @@ from .math_func import (
exp2,
deg2rad,
isreal,
rad2deg,
)
from .nn_func import (
deformable_conv2d,

View File

@ -2764,6 +2764,46 @@ def deg2rad(x):
return out
def rad2deg(x):
"""
Returns a new tensor with each of the elements of `x` converted from angles in radians to degrees.
Args:
x (Tensor): The input tensor.
Outputs:
Tensor, has the same shape and dtype as the `x`.
Raises:
TypeError: If `x` is not a Tensor.
TypeError: If dtype of `x` isn't float16, float32 or float64.
Supported Platforms:
``Ascend`` ``GPU`` ``CPU``
Examples:
>>> import mindspore
>>> from mindspore import Tensor
>>> import mindspore.ops as ops
>>> x = Tensor([[6.283, -3.142],[1.570, -6.283],[3.142, -1.570]], mindspore.float32)
>>> output = rad2deg(x)
>>> print(output)
[[ 359.98935 -180.02333]
[ 89.95438 -359.98935]
[ 180.02333 -89.95438]]
"""
if not isinstance(x, (Tensor, Tensor_)):
raise TypeError("The input x must be tensor")
dtype_op = P.DType()
x_dtype = dtype_op(x)
_check_input_dtype("x", x_dtype, [mstype.float16, mstype.float32, mstype.float64], "")
if x_dtype == mstype.float16:
out = x * (Tensor(180.0 / math.pi).astype(mstype.float16))
else:
out = x * 180.0 / math.pi
return out
#####################################
# Reduction Operation Functions.
#####################################
@ -2904,6 +2944,7 @@ __all__ = [
'bessel_k1',
'bessel_k1e',
'exp2',
'deg2rad'
'deg2rad',
'rad2deg'
]
__all__.sort()

View File

@ -445,6 +445,15 @@ class IsRealFunc(nn.Cell):
return y
class Rad2degNet(nn.Cell):
def __init__(self):
super(Rad2degNet, self).__init__()
self.rad2deg = ops.rad2deg
def construct(self, x):
return self.rad2deg(x)
test_case_math_ops = [
('MatMulGrad', {
'block': GradWrap(NetWithLoss(MatMulNet())),
@ -530,6 +539,11 @@ test_case_math_ops = [
'block': IsRealFunc(),
'desc_inputs': [Tensor([1, 1+1j, 2+0j])],
}),
('Rad2deg', {
'block': Rad2degNet(),
'desc_inputs': [Tensor(np.array([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]], np.float32))],
'desc_bprop': [Tensor(np.array([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]], np.float32))],
}),
]
test_case_lists = [test_case_math_ops]
@ -594,6 +608,12 @@ raise_set = [
('Deg2rad_2_Error', {
'block': (lambda x: Deg2radNet(), {'exception': TypeError}),
'desc_inputs': [Tensor(np.array([[90, -90], [180, -180], [270, -270]], np.int32))]}),
('Rad2deg_1_Error', {
'block': (lambda x: Rad2degNet(), {'exception': TypeError}),
'desc_inputs': [0]}),
('Rad2deg_2_Error', {
'block': (lambda x: Rad2degNet(), {'exception': TypeError}),
'desc_inputs': [Tensor(np.array([[3, -3], [6, -6], [1, -1]], np.int32))]}),
]