PyTorch 中的 pow
请我喝杯咖啡☕
*备忘录:
pow() 可以从零个或多个元素的 0d 或多个 d 张量中的两个或零个或多个元素的 0d 或多个 d 张量和标量中获得零次或多次幂的 0d 或多个 d 张量,如图所示下图:
*备忘录:
import torchtensor1 = 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]]])