Table of Contents
Shortcuts

# torch.copysign

torch.copysign(input, other, *, out=None) → Tensor

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

  out i={−∣input i∣if other i≤−0.0∣input i∣if other i≥0.0\text{out}_{i} = \begin{cases} -|\text{input}_{i}| & \text{if } \text{other}_{i} \leq -0.0 \\ |\text{input}_{i}| & \text{if } \text{other}_{i} \geq 0.0 \\ \end{cases} out i​={−∣input i​∣∣input i​∣​if other i​≤−0.0 if other i​≥0.0​

  Supports broadcasting to a common shape, and integer and float inputs.

  Parameters:

    - input (Tensor) – magnitudes.

    - other (Tensor or Number) – contains value(s) whose signbit(s) are applied to the magnitudes in `input`.

  Keyword Arguments:

    out (Tensor, optional) – the output tensor.

  Example:

      >>> a = torch.randn(5)
      >>> a
      tensor([-1.2557, -0.0026, -0.5387,  0.4740, -0.9244])
      >>> torch.copysign(a, 1)
      tensor([1.2557, 0.0026, 0.5387, 0.4740, 0.9244])
      >>> a = torch.randn(4, 4)
      >>> a
      tensor([[ 0.7079,  0.2778, -1.0249,  0.5719],
              [-0.0059, -0.2600, -0.4475, -1.3948],
              [ 0.3667, -0.9567, -2.5757, -0.1751],
              [ 0.2046, -0.0742,  0.2998, -0.1054]])
      >>> b = torch.randn(4)
      tensor([ 0.2373,  0.3120,  0.3190, -1.1128])
      >>> torch.copysign(a, b)
      tensor([[ 0.7079,  0.2778,  1.0249, -0.5719],
              [ 0.0059,  0.2600,  0.4475, -1.3948],
              [ 0.3667,  0.9567,  2.5757, -0.1751],
              [ 0.2046,  0.0742,  0.2998, -0.1054]])
      >>> a = torch.tensor([1.])
      >>> b = torch.tensor([-0.])
      >>> torch.copysign(a, b)
      tensor([-1.])

  Note

  copysign handles signed zeros. If the other argument has a negative zero (-0), the corresponding output value will be negative.

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.