square() and pow() in PyTorch

*My post explains float_power().

square() can get the 0D or more D tensor of squared zero or more elements, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

square() can be used with torch or a tensor.
The 1st argum…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

*My post explains float_power().

square() can get the 0D or more D tensor of squared zero or more elements, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • square() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

my_tensor = torch.tensor(-3)

torch.square(input=my_tensor)
my_tensor.square()
# tensor(9)

my_tensor = torch.tensor([-3, 1, -2, 3, 5, -5, 0, -4])

torch.square(input=my_tensor)
# tensor([9, 1, 4, 9, 25, 25, 0, 16])

my_tensor = torch.tensor([[-3, 1, -2, 3],
                          [5, -5, 0, -4]])
torch.square(input=my_tensor)
# tensor([[9, 1, 4, 9],
#         [25, 25, 0, 16]])

my_tensor = torch.tensor([[[-3, 1], [-2, 3]],
                          [[5, -5], [0, -4]]])
torch.square(input=my_tensor)
# tensor([[[9, 1], [4, 9]],
#         [[25, 25], [0, 16]]])

my_tensor = torch.tensor([[[-3., 1.], [-2., 3.]],
                          [[5., -5.], [0., -4.]]])
torch.square(input=my_tensor)
# tensor([[[9., 1.], [4., 9.]],
#         [[25., 25.], [0., 16.]]])

my_tensor = torch.tensor([[[-3.+0.j, 1.+0.j], [-2.+0.j, 3.+0.j]],
                          [[5.+0.j, -5.+0.j], [0.+0.j, -4.+0.j]]])
torch.square(input=my_tensor)
# tensor([[[9.-0.j, 1.+0.j], [4.-0.j, 9.+0.j]],
#         [[25.+0.j, 25.-0.j], [0.+0.j, 16.-0.j]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.square(input=my_tensor)
# tensor([[[1, 0], [1, 0]],
#         [[0, 1], [0, 1]]])

pow() can get the 0D or more D tensor of zero or more powers from two of the 0D or more D tensors of zero or more elements or the 0D or more D tensor of zero or more elements and a scalar as shown below:

*Memos:

  • pow() can be used with torch or a tensor.
  • The 1st argument(input) with torch(Type:tensor or scalar of int, float or complex) or using a tensor(Type:tensor of int, float or complex)(Required). *torch must use a scalar without input=.
  • The 2nd argument with torch or the 1st argument with a tensor is exponent(Required-Type:tensor or scalar of int, float or complex). *A negative scalar cannot be used.
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • The combination of a scalar(input or a tensor) and a scalar(exponent) cannot be used.
  • The combination of a tensor(input(bool) or a tensor(bool) and a scalar(exponent(bool) works.
import torch

tensor1 = torch.tensor(-3)
tensor2 = torch.tensor([-4, -3, -2, -1, 0, 1, 2, 3])

torch.pow(input=tensor1, exponent=tensor2)
tensor1.pow(exponent=tensor2)
# tensor([0, 0, 0, 0, 1, -3, 9, -27])

torch.pow(-3, exponent=tensor2)
# tensor([0, 0, 0, 0, 1, -3, 9, -27])

torch.pow(input=tensor1, exponent=3)
# tensor(-27)

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

torch.pow(input=tensor1, exponent=tensor2)
# tensor([0, 1, 0, 0, 1, -5, 0, -64])

torch.pow(-3, exponent=tensor2)
# tensor([0, 0, 0, 0, 1, -3, 9, -27])

torch.pow(input=tensor1, exponent=3)
# tensor([-27, 1, -8, 27, 125, -125, 0, -64])

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

torch.pow(input=tensor1, exponent=tensor2)
# tensor([[1, 1, 4, 27], [1, -5, 0, -64]])

torch.pow(-3, exponent=tensor2)
# tensor([1, -3, 9, -27])

torch.pow(input=tensor1, exponent=3)
# tensor([[-27, 1, -8, 27], [125, -125, 0, -64]])

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

torch.pow(input=tensor1, exponent=tensor2)
# tensor([[[9, 1], [4, 27]],
#         [[25, -125], [0, -64]]])

torch.pow(-3, exponent=tensor2)
# tensor([9, -27])

torch.pow(input=tensor1, exponent=3)
# tensor([[[-27, 1], [-8, 27]],
#         [[125, -125], [0, -64]]])

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

torch.pow(input=tensor1, exponent=tensor2)
# tensor([[[9., 1.], [4., 27.]],
#         [[25., -125.], [0., -64.]]])

torch.pow(-3., exponent=tensor2)
# tensor([9., -27.])

torch.pow(input=tensor1, exponent=3.)
# tensor([[[-27., 1.], [-8., 27.]],
#         [[125., -125.], [0., -64.]]])

tensor1 = torch.tensor([[[-3.+0.j, 1.+0.j], [-2.+0.j, 3.+0.j]],
                        [[5.+0.j, -5.+0.j], [0.+0.j, -4.+0.j]]])
tensor2 = torch.tensor([2.+0.j, 3.+0.j])

torch.pow(input=tensor1, exponent=tensor2)
# tensor([[[9.0000+1.5736e-06j, 1.0000+0.0000e+00j],
#          [4.0000+6.9938e-07j, 27.0000+0.0000e+00j]],
#         [[25.0000+0.0000e+00j, -125.0000-2.9812e-06j],
#          [0.0000-0.0000e+00j, -64.0000-1.5264e-06j]]])

torch.pow(-3.+0.j, exponent=tensor2)
# tensor([9.0000+1.5736e-06j, -27.0000-6.4394e-07j])

torch.pow(input=tensor1, exponent=3.+0.j)
# tensor([[[-27.+0.j, 1.+0.j],
#          [-8.+0.j, 27.+0.j]],
#         [[125.+0.j, -125.+0.j],
#          [0.+0.j, -64.+0.j]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.pow(input=my_tensor, exponent=True)
# tensor([[[True, False], [True, False]],
#         [[False, True], [False, True]]])


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-14T20:31:38+00:00) square() and pow() in PyTorch. Retrieved from https://www.scien.cx/2024/07/14/square-and-pow-in-pytorch/

MLA
" » square() and pow() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Sunday July 14, 2024, https://www.scien.cx/2024/07/14/square-and-pow-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday July 14, 2024 » square() and pow() in PyTorch., viewed ,<https://www.scien.cx/2024/07/14/square-and-pow-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » square() and pow() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/14/square-and-pow-in-pytorch/
CHICAGO
" » square() and pow() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/14/square-and-pow-in-pytorch/
IEEE
" » square() and pow() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/14/square-and-pow-in-pytorch/. [Accessed: ]
rf:citation
» square() and pow() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/14/square-and-pow-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.