DEV Community

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

Posted on

square(), pow() and float_power() in PyTorch

square() can square the zero or more values of a 0D or more D tensor as shown below:

*Memos:

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

pow() can get the zero or more powers with the zero or more elements of 2 tensors as shown below:

*Memos:

  • pow() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required). *torch must use a scalar without input=.
  • The 2nd argument(tensor of int, float or complex or scalar of int, float, complex or bool) with torch or the 1st argument(tensor of int, float or complex or scalar of int, float, complex or bool) is exponent(Required). *A negative scalar cannot be used.
  • The combination of a scalar(input) and a scalar(exponent) cannot be used.
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(input=tensor1, exponent=3)
# tensor(-27)

torch.pow(-3, exponent=tensor2)
# tensor([0, 0, 0, 0, 1, -3, 9, -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(input=tensor1, exponent=3)
# tensor([-27, 1, -8, 27, 125, -125, 0, -64])

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

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(input=tensor1, exponent=3)
# tensor([[-27, 1, -8, 27], [125, -125, 0, -64]])

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

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(input=tensor1, exponent=3)
# tensor([[[-27, 1], [-8, 27]],
#         [[125, -125], [0, -64]]])

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

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(input=tensor1, exponent=3.)
# tensor([[[-27., 1.], [-8., 27.]],
#         [[125., -125.], [0., -64.]]])

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

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(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]]])

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

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

float_power() can get the zero or more powers of float or complex with the zero or more elements of 2 tensors as shown below:

*Memos:

  • float_power() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required). *torch must use a scalar without input=.
  • The 2nd argument(tensor of int, float or complex or scalar of int, float, complex or bool) with torch or the 1st argument(tensor of int, float or complex or scalar of int, float, complex or bool) is exponent(Required).
  • The combination of a scalar(input) and a scalar(exponent) cannot be used.
import torch

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

torch.float_power(input=tensor1, exponent=tensor2)
tensor1.float_power(exponent=tensor2)
# tensor([1.2346e-02, -3.7037e-02, 1.1111e-01, -3.3333e-01,  
#         1.0000e+00, -3.0000e+00, 9.0000e+00, -2.7000e+01], 
#        dtype=torch.float64)

torch.float_power(input=tensor1, exponent=-3.)
# tensor(-0.0370, dtype=torch.float64)

torch.float_power(-3., exponent=tensor2)
# tensor([1.2346e-02, -3.7037e-02, 1.1111e-01, -3.3333e-01,
#         1.0000e+00, -3.0000e+00, 9.0000e+00, -2.7000e+01],
#        dtype=torch.float64)

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

torch.float_power(input=tensor1, exponent=tensor2)
# tensor([1.2346e-02, 1.0000e+00, 2.5000e-01, 3.3333e-01,
#         1.0000e+00, -5.0000e+00, 0.0000e+00, -6.4000e+01],
#        dtype=torch.float64)

torch.float_power(input=tensor1, exponent=-3.)
# tensor([-0.0370, 1.0000, -0.1250, 0.0370,
#         0.0080, -0.0080, inf, -0.0156], dtype=torch.float64)

torch.float_power(-3., exponent=tensor2)
# tensor([1.2346e-02, -3.7037e-02, 1.1111e-01, -3.3333e-01,  
#         1.0000e+00, -3.0000e+00, 9.0000e+00, -2.7000e+01],
#        dtype=torch.float64)

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

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

torch.float_power(input=tensor1, exponent=-3.)
# tensor([[-0.0370, 1.0000, -0.1250, 0.0370],
#         [0.0080, -0.0080, inf, -0.0156]], 
#        dtype=torch.float64)

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

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

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

torch.float_power(input=tensor1, exponent=-3.)
# tensor([[[-0.0370, 1.0000], [-0.1250, 0.0370]],
#         [[0.0080, -0.0080], [inf, -0.0156]]], 
#        dtype=torch.float64)

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

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

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

torch.float_power(input=tensor1, exponent=-3)
# tensor([[[-0.0370, 1.0000], [-0.1250, 0.0370]],
#         [[0.0080, -0.0080], [inf, -0.0156]]], 
#        dtype=torch.float64)

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

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.float_power(input=tensor1, exponent=tensor2)
# tensor([[[9.0000-2.2044e-15j, 1.0000+0.0000e+00j],
#          [4.0000-9.7972e-16j, 27.0000+0.0000e+00j]],
#         [[25.0000+0.0000e+00j, -125.0000+4.5924e-14j],
#          [0.0000-0.0000e+00j, -64.0000+2.3513e-14j]]],
#        dtype=torch.complex128)

torch.float_power(input=tensor1, exponent=-3.+0.j)
# tensor([[[-0.0370-1.3607e-17j, 1.0000+0.0000e+00j],
#          [-0.1250-4.5924e-17j, 0.0370+0.0000e+00j]],
#         [[0.0080+0.0000e+00j, -0.0080-2.9392e-18j],
#          [inf+nanj, -0.0156-5.7405e-18j]]], 
#        dtype=torch.complex128)

torch.float_power(-3.+0.j, exponent=tensor2)
# tensor([9.0000-2.2044e-15j, -27.0000+9.9196e-15j],
#        dtype=torch.complex128)

my_tensor = torch.tensor([[[False, True], [False, True]],
                          [[False, True], [False, True]]])
torch.float_power(input=my_tensor, exponent=False)
# tensor([[[1., 1.], [1., 1.]],
#         [[1., 1.], [1., 1.]]], dtype=torch.float64)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)