positive(), neg() and copysign() in PyTorch
Tags: pytorch, positive, neg, copysign
Published: 2024-05-17 12:32:45

[positive()](https://pytorch.org/docs/stable/generated/torch.positive.html) can just get the 0D or more D `input` tensor of zero or more elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `positive()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `complex`).

```python
import torch

my_tensor = torch.tensor(7)

torch.positive(input=my_tensor)
my_tensor.positive()
# tensor(7)

my_tensor = torch.tensor([7, -1, 5, -7, -9, -3, 0, 6])

torch.positive(input=my_tensor)
# tensor([7, -1, 5, -7, -9, -3, 0, 6])

my_tensor = torch.tensor([[7, -1, 5, -7],
                          [-9, -3, 0, 6]])
torch.positive(input=my_tensor)
# tensor([[7, -1, 5, -7],
#         [-9, -3, 0, 6]])

my_tensor = torch.tensor([[[7, -1], [5, -7]],
                          [[-9, -3], [0, 6]]])
torch.positive(input=my_tensor)
# tensor([[[7, -1], [5, -7]],
#         [[-9, -3], [0, 6]]])

my_tensor = torch.tensor([[[7., -1.], [5., -7.]],
                          [[-9., -3.], [0., 6.]]])
torch.positive(input=my_tensor)
# tensor([[[7., -1.], [5., -7.]],
#         [[-9., -3.], [0., 6.]]])

my_tensor = torch.tensor([[[7.+0.j, -1.+0.j], [5.+0.j, -7.+0.j]],
                          [[-9.+0.j, -3.+0.j], [0.+0.j, 6.+0.j]]])
torch.positive(input=my_tensor)
# tensor([[[7.+0.j, -1.+0.j],
#          [5.+0.j, -7.+0.j]],
#         [[-9.+0.j, -3.+0.j],
#          [0.+0.j, 6.+0.j]]])
```

[neg()](https://pytorch.org/docs/stable/generated/torch.neg.html) can get the 0D or more D tensor of the zero or more elements changed from `+` to `-` and from `-` to `+` from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `neg()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `complex`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- [negative()](https://pytorch.org/docs/stable/generated/torch.negative.html) is the alias of `neg()`.

```python
import torch

my_tensor = torch.tensor(7)

torch.neg(input=my_tensor)
my_tensor.neg()
# tensor(-7)

my_tensor = torch.tensor([7, -1, 5, -7, -9, -3, 0, 6])

torch.neg(input=my_tensor)
# tensor([-7, 1, -5, 7, 9, 3, 0, -6])

my_tensor = torch.tensor([[7, -1, 5, -7],
                          [-9, -3, 0, 6]])
torch.neg(input=my_tensor)
# tensor([[-7, 1, -5, 7],
#         [9, 3, 0, -6]])

my_tensor = torch.tensor([[[7, -1], [5, -7]],
                          [[-9, -3], [0, 6]]])
torch.neg(input=my_tensor)
# tensor([[[-7, 1], [-5, 7]],
#         [[9, 3], [0, -6]]])

my_tensor = torch.tensor([[[7., -1.], [5., -7.]],
                          [[-9., -3.], [0., 6.]]])
torch.neg(input=my_tensor)
# tensor([[[-7., 1.], [-5., 7.]],
#         [[9., 3.], [-0., -6.]]])

my_tensor = torch.tensor([[[7.+0.j, -1.+0.j], [5.+0.j, -7.+0.j]],
                          [[-9.+0.j, -3.+0.j], [0.+0.j, 6.+0.j]]])
torch.neg(input=my_tensor)
# tensor([[[-7.+0.j, 1.+0.j], [-5.+0.j, 7.+0.j]],
#         [[9.+0.j, 3.+0.j], [0.+0.j, -6.+0.j]]])
```

[copysign()](https://pytorch.org/docs/stable/generated/torch.copysign.html) can get the 0D or more D tensor of the zero or more floating point numbers changed `+` and `-` by `other` tensor from two of 0D or more D tensors as shown below:

*Memos:
- `copysign()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `bool`).
- The 2st argument with `torch` or the 1st argument is `other`(Required-Type:`tensor` or `scalar` of `int`, `float` or `bool`). *The sign(`+` or `-`) is applied to `input` tensor.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

tensor1 = torch.tensor([[7., -1., 5., -7.],
                        [-9., -3., 0., 6.]])
tensor2 = torch.tensor([-1., 0., 1., 2.])

torch.copysign(input=tensor1, other=tensor2)
tensor1.copysign(other=tensor2)
# tensor([[-7., 1., 5., 7.],
#         [-9., 3., 0., 6.]])

torch.copysign(input=tensor1, other=2.)
# tensor([[7., 1., 5., 7.],
#         [9., 3., 0., 6.]])

tensor1 = torch.tensor([[[7., -1.], [5., -7.]],
                        [[-9., -3.], [0., 6.]]])
tensor2 = torch.tensor(-1.)

torch.copysign(input=tensor1, other=tensor2)
# tensor([[[-7., -1.], [-5., -7.]],
#         [[-9., -3.], [-0., -6.]]])

torch.copysign(input=tensor1, other=2.)
# tensor([[[7., 1.], [5., 7.]],
#         [[9., 3.], [0., 6.]]])

tensor1 = torch.tensor([[[7, -1], [5, -7]],
                        [[-9, -3], [0, 6]]])
tensor2 = torch.tensor(-1)

torch.copysign(input=tensor1, other=tensor2)
torch.copysign(input=tensor1, other=-1)
# tensor([[[-7., -1.], [-5., -7.]],
#         [[-9., -3.], [-0., -6.]]])

torch.copysign(input=tensor1, other=2)
# tensor([[[7., 1.], [5., 7.]],
#         [[9., 3.], [0., 6.]]])

tensor1 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
tensor2 = torch.tensor(True)

torch.copysign(input=tensor1, other=tensor2)
torch.copysign(input=tensor1, other=False)
# tensor([[[1., 0.], [1., 0.]],
#         [[0., 1.], [0., 1.]]])
```