forked from mindspore-Ecosystem/mindspore
add Tensor.copysign
This commit is contained in:
parent
43c452f099
commit
629452e3d8
|
@ -183,6 +183,7 @@ mindspore.ops.function
|
|||
mindspore.ops.bitwise_or
|
||||
mindspore.ops.bitwise_xor
|
||||
mindspore.ops.ceil
|
||||
mindspore.ops.copysign
|
||||
mindspore.ops.cos
|
||||
mindspore.ops.cosh
|
||||
mindspore.ops.deg2rad
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
mindspore.Tensor.copysign
|
||||
==========================
|
||||
|
||||
.. py:method:: mindspore.Tensor.copysign(other)
|
||||
|
||||
详情请参考 :func:`mindspore.ops.copysign`。
|
|
@ -71,6 +71,7 @@ mindspore.Tensor
|
|||
mindspore.Tensor.col2im
|
||||
mindspore.Tensor.conj
|
||||
mindspore.Tensor.copy
|
||||
mindspore.Tensor.copysign
|
||||
mindspore.Tensor.cosh
|
||||
mindspore.Tensor.cross
|
||||
mindspore.Tensor.cummax
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
mindspore.ops.copysign
|
||||
=======================
|
||||
|
||||
.. py:function:: mindspore.ops.copysign(x, other)
|
||||
|
||||
逐元素地创建一个新的浮点Tensor,其大小为 `x`,符号为 `other` 的符号。
|
||||
|
||||
参数:
|
||||
- **x** (Union[Tensor]) - 要更改符号的值。
|
||||
- **other** (Union[int, float, Tensor]) - `other` 的符号被复制到 `x`。如果 `x.shape != other.shape`,`other` 必须可广播为 `x` 的shape(这也是输出的shape)。
|
||||
|
||||
返回:
|
||||
Tensor。数据类型为float。`x` 的值加上 `other` 的符号,shape与 `x` 相同。
|
||||
|
||||
异常:
|
||||
- **TypeError** - 如果输入的数据类型不在给定的类型中,或者输入不能转换为Tensor。
|
|
@ -77,6 +77,7 @@
|
|||
mindspore.Tensor.col2im
|
||||
mindspore.Tensor.conj
|
||||
mindspore.Tensor.copy
|
||||
mindspore.Tensor.copysign
|
||||
mindspore.Tensor.cosh
|
||||
mindspore.Tensor.cross
|
||||
mindspore.Tensor.cummax
|
||||
|
|
|
@ -184,6 +184,7 @@ Element-by-Element Operations
|
|||
mindspore.ops.bitwise_or
|
||||
mindspore.ops.bitwise_xor
|
||||
mindspore.ops.ceil
|
||||
mindspore.ops.copysign
|
||||
mindspore.ops.cos
|
||||
mindspore.ops.cosh
|
||||
mindspore.ops.deg2rad
|
||||
|
|
|
@ -234,6 +234,7 @@ BuiltInTypeMap &GetMethodMap() {
|
|||
{"index_fill", std::string("index_fill")}, // index_fill()
|
||||
{"repeat_interleave", std::string("repeat_interleave")}, // repeat_interleave()
|
||||
{"copy", std::string("copy")}, // copy()
|
||||
{"copysign", std::string("copysign")}, // copysign()
|
||||
{"inplace_update", std::string("inplace_update")}, // P.InplaceUpdate
|
||||
{"lerp", std::string("lerp")}, // lerp()
|
||||
{"lcm", std::string("lcm")}, // F.lcm()
|
||||
|
|
|
@ -1118,6 +1118,13 @@ def deg2rad(x):
|
|||
return F.deg2rad(x)
|
||||
|
||||
|
||||
def copysign(x, other):
|
||||
"""
|
||||
Create a new floating-point tensor with the magnitude of `x` and the sign of `other`, element-wise.
|
||||
"""
|
||||
return F.copysign(x, other)
|
||||
|
||||
|
||||
def numel(x):
|
||||
"""
|
||||
Returns a Scalar of type int that represents the total number of elements in the Tensor.
|
||||
|
|
|
@ -1696,6 +1696,13 @@ class Tensor(Tensor_):
|
|||
self._init_check()
|
||||
return tensor_operator_registry.get('rad2deg')(self)
|
||||
|
||||
def copysign(self, other):
|
||||
r"""
|
||||
For details, please refer to :func:`mindspore.ops.copysign`.
|
||||
"""
|
||||
self._init_check()
|
||||
return tensor_operator_registry.get('copysign')(self, other)
|
||||
|
||||
def nelement(self):
|
||||
r"""
|
||||
Alias for :func:`mindspore.Tensor.numel`.
|
||||
|
|
|
@ -4898,13 +4898,7 @@ def vstack(inputs):
|
|||
|
||||
def copysign(x, other):
|
||||
r"""
|
||||
Changes the sign of `x` to that of `other`, element-wise.
|
||||
|
||||
If `other` is a scalar, its sign will be copied to all elements of `x`.
|
||||
|
||||
Note:
|
||||
Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`, and `extobj` are
|
||||
not supported. Complex inputs are not supported.
|
||||
Create a new floating-point tensor with the magnitude of `x` and the sign of `other`, element-wise.
|
||||
|
||||
Args:
|
||||
x (Union[Tensor]): Values to change the sign of.
|
||||
|
@ -4912,7 +4906,8 @@ def copysign(x, other):
|
|||
`other` must be broadcastable to the shape of `x` (which is also the shape of the output).
|
||||
|
||||
Returns:
|
||||
Tensor, float, the values of `x` with the sign of `other`, the shape is the same as `x`.
|
||||
Tensor. The dtype of the tensor is float.
|
||||
The values of `x` with the sign of `other`, the shape is the same as `x`.
|
||||
|
||||
Raises:
|
||||
TypeError: If dtype of the input is not in the given types or
|
||||
|
@ -4923,6 +4918,7 @@ def copysign(x, other):
|
|||
|
||||
Examples:
|
||||
>>> import mindspore.numpy as np
|
||||
>>> import mindspore.ops as ops
|
||||
>>> x = np.array([[0.3, -0.7], [0.5, 0.5]])
|
||||
>>> other = np.array([[-0.4, 0.6], [0.4, -0.6]])
|
||||
>>> out = ops.copysign(x, other)
|
||||
|
|
|
@ -345,6 +345,7 @@ tensor_operator_registry.register('imag', imag)
|
|||
tensor_operator_registry.register('repeat_interleave', repeat_interleave)
|
||||
tensor_operator_registry.register('rad2deg', rad2deg)
|
||||
tensor_operator_registry.register('deg2rad', deg2rad)
|
||||
tensor_operator_registry.register('copysign', copysign)
|
||||
tensor_operator_registry.register('roll', Roll)
|
||||
tensor_operator_registry.register('rot90', rot90)
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
# 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 CopysginNet(nn.Cell):
|
||||
def construct(self, x, other):
|
||||
return x.copysign(other)
|
||||
|
||||
|
||||
@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_copysign(mode):
|
||||
"""
|
||||
Feature: test Tensor.copysign
|
||||
Description: Verify the result of Tensor.copysign
|
||||
Expectation: expect correct forward result
|
||||
"""
|
||||
ms.set_context(mode=mode)
|
||||
x_np = np.array([[0.3, -0.7], [0.5, 0.5]]).astype(np.float32)
|
||||
other_np = np.array([[-0.4, 0.6], [0.4, -0.6]]).astype(np.float32)
|
||||
x = Tensor(x_np, ms.float32)
|
||||
other = Tensor(other_np, ms.float32)
|
||||
net = CopysginNet()
|
||||
output_ms = net(x, other)
|
||||
expect_output = np.copysign(x_np, other_np)
|
||||
assert np.allclose(output_ms.asnumpy(), expect_output)
|
Loading…
Reference in New Issue