copysign

paddle. copysign ( x, y, name=None ) [source]

Create a new floating-point tensor with the magnitude of input x and the sign of y, elementwise.

Equation:
\[\begin{split}copysign(x_{i},y_{i})=\left\{\begin{matrix} & -|x_{i}| & if \space y_{i} <= -0.0\\ & |x_{i}| & if \space y_{i} >= 0.0 \end{matrix}\right.\end{split}\]
Parameters
  • x (Tensor) – The input Tensor, magnitudes, the data type is bool, uint8, int8, int16, int32, int64, bfloat16, float16, float32, float64.

  • y (Tensor, number) – contains value(s) whose signbit(s) are applied to the magnitudes in input.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

out (Tensor), the output tensor. The data type is the same as the input tensor.

Examples

>>> import paddle
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([-1, 1, -1], dtype='float64')
>>> out = paddle.copysign(x, y)
>>> print(out)
Tensor(shape=[3], dtype=float64, place=Place(gpu:0), stop_gradient=True,
       [-1.,  2., -3.])
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([-2], dtype='float64')
>>> res = paddle.copysign(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float64, place=Place(gpu:0), stop_gradient=True,
       [-1.,  -2.,  -3.])
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([0.0], dtype='float64')
>>> out = paddle.copysign(x, y)
>>> print(out)
Tensor(shape=[3], dtype=float64, place=Place(gpu:0), stop_gradient=True,
    [1., 2., 3.])
>>> x = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> y = paddle.to_tensor([-0.0], dtype='float64')
>>> out = paddle.copysign(x, y)
>>> print(out)
Tensor(shape=[3], dtype=float64, place=Place(gpu:0), stop_gradient=True,
    [-1., -2., -3.])