DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

positive(), neg() and copysign() in PyTorch

positive() can just return a 0D or more D input tensor as shown below:

*Memos:

  • positive() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float or complex) with torch or using a tensor(tensor of int, float or complex) is input(Required).
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]]])
Enter fullscreen mode Exit fullscreen mode

neg() can change + to - and - to + of the zero or more values of a 0D or more D tensor as shown below:

*Memos:

  • neg() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float or complex) with torch or using a tensor(tensor of int, float or complex) is input(Required).
  • negative() is the alias of neg().
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]]])
Enter fullscreen mode Exit fullscreen mode

copysign() can get the 0D or more D tensor of zero or more floating point numbers by two of 0D or more D tensors (the magnitude of input and the sign of other as shown below:

*Memos:

  • copysign() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, bool) with torch or using a tensor(tensor of int, float or bool) is input(Required). *This is magnitude.
  • The 2st argument(tensor or scalar of int, float or bool) with torch or the 1st argument(tensor or scalar of int, float or bool) is other(Required). *The sign(+ or -) is applied to input.
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.]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)