|
| 1 | +--- |
| 2 | +Title: '.copysign()' |
| 3 | +Description: 'Returns a tensor with the magnitude of the first input and the sign of the second input, element-wise.' |
| 4 | +Subjects: |
| 5 | + - 'AI' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Functions' |
| 9 | + - 'PyTorch' |
| 10 | + - 'Tensor' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-pytorch' |
| 13 | + - 'paths/data-science' |
| 14 | +--- |
| 15 | + |
| 16 | +PyTorch's **`.copysign()`** function creates a new tensor by combining the magnitude of the first input with the sign of the second input, element-wise. |
| 17 | + |
| 18 | +> **Note:** If the first input contains booleans, they are cast to floats (`True` becomes `1.0`, `False` becomes `0.0`). `-0.0` or `-0` in the second input is treated as a negative sign, while `0.0` or `0` is treated as positive, and these signs are preserved when copied. |
| 19 | +
|
| 20 | +## Syntax |
| 21 | + |
| 22 | +```pseudo |
| 23 | +torch.copysign(input, other, *, out=None) |
| 24 | +``` |
| 25 | + |
| 26 | +**Parameters:** |
| 27 | + |
| 28 | +- `input` (Tensor): The input tensor providing magnitudes. |
| 29 | +- `other` (Tensor): The tensor whose sign will be copied to input. Must be broadcastable to the shape of `input`. |
| 30 | +- `out` (Tensor, optional): The tensor to store the output. Must have the same shape as the broadcasted result. |
| 31 | + |
| 32 | +> **Note:** The tensors must be [broadcastable](https://www.codecademy.com/resources/docs/numpy/array-broadcasting) in order to perform the operation. Otherwise, this will result in a `RuntimeError`. |
| 33 | +
|
| 34 | +**Return value:** |
| 35 | + |
| 36 | +A tensor where each element has the absolute value of the corresponding element in `input` and the sign of the corresponding element in `other`. |
| 37 | + |
| 38 | +## Example 1 |
| 39 | + |
| 40 | +This example shows how to copy signs from the `signs` tensor to the `magnitudes` tensor using `.copysign()`: |
| 41 | + |
| 42 | +```py |
| 43 | +import torch |
| 44 | + |
| 45 | +# Initializing tensors |
| 46 | +magnitudes = torch.tensor([-5.0, True, -8.0]) |
| 47 | +signs = torch.tensor([-0.0, 0.0, 3.0]) |
| 48 | + |
| 49 | +# Using torch.copysign to copy the sign from 'signs' to 'magnitudes' |
| 50 | +result = torch.copysign(magnitudes, signs) |
| 51 | +print(result) |
| 52 | +``` |
| 53 | + |
| 54 | +The output of this code is: |
| 55 | + |
| 56 | +```shell |
| 57 | +tensor([-5., 1., 8.]) |
| 58 | +``` |
| 59 | + |
| 60 | +## Example 2 |
| 61 | + |
| 62 | +This example shows how to copy the sign from a one-dimensional `signs` tensor to a two-dimensional `magnitudes` tensor using broadcasting. Each row of `magnitudes` is aligned with the corresponding value in `signs`, and the sign is applied column-wise: |
| 63 | + |
| 64 | +```py |
| 65 | +import torch |
| 66 | + |
| 67 | +torch.manual_seed(0) |
| 68 | + |
| 69 | +# Create a 3x3 tensor of magnitudes |
| 70 | +magnitudes = torch.rand(3, 3) |
| 71 | +print("Magnitudes:\n", magnitudes) |
| 72 | + |
| 73 | +# 1D tensor whose signs will be broadcast across rows |
| 74 | +signs = torch.tensor([0.6323, 0.3489, -0.4017]) |
| 75 | + |
| 76 | +# Apply copysign with broadcasting |
| 77 | +result = torch.copysign(magnitudes, signs) |
| 78 | +print("\nResult:\n", result) |
| 79 | +``` |
| 80 | + |
| 81 | +The output of this code is: |
| 82 | + |
| 83 | +```shell |
| 84 | +Magnitudes: |
| 85 | + tensor([[0.4963, 0.7682, 0.0885], |
| 86 | + [0.1320, 0.3074, 0.6341], |
| 87 | + [0.4901, 0.8964, 0.4556]]) |
| 88 | + |
| 89 | +Result: |
| 90 | + tensor([[ 0.4963, 0.7682, -0.0885], |
| 91 | + [ 0.1320, 0.3074, -0.6341], |
| 92 | + [ 0.4901, 0.8964, -0.4556]]) |
| 93 | +``` |
0 commit comments