exp and exp2 in PyTorch

Super Kai (Kazuya Ito) - Sep 3 - - Dev Community

Buy Me a Coffee

*Memos:

exp() can get the 0D or more D tensor of the zero or more elements by ex from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • exp() 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-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • *A float tensor is returned unless an input tensor is complex tensor.
  • The formula is y = ex.
  • The graph in Desmos: Image description
import torch

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

torch.exp(input=my_tensor)
my_tensor.exp()
# tensor([0.1353, 0.3679, 1.0000, 2.7183, 7.3891, 20.0855])

my_tensor = torch.tensor([[-2., -1., 0.],
                          [1., 2., 3.]])
torch.exp(input=my_tensor)
# tensor([[0.1353, 0.3679, 1.0000],
#         [2.7183, 7.3891, 20.0855]])

my_tensor = torch.tensor([[-2, -1, 0],
                          [1, 2, 3]])
torch.exp(input=my_tensor)
# tensor([[0.1353, 0.3679, 1.0000],
#         [2.7183, 7.3891, 20.0855]])

my_tensor = torch.tensor([[-2.+0.j, -1.+0.j, 0.+0.j],
                          [1.+0.j, 2.+0.j, 3.+0.j]])
torch.exp(input=my_tensor)
# tensor([[0.1353+0.j, 0.3679+0.j, 1.0000+0.j],
#         [2.7183+0.j, 7.3891+0.j, 20.0855+0.j]])

my_tensor = torch.tensor([[True, False, True],
                          [False, True, False]])
torch.exp(input=my_tensor)
# tensor([[2.7183, 1.0000, 2.7183],
#         [1.0000, 2.7183, 1.0000]])
Enter fullscreen mode Exit fullscreen mode

exp2() can get the 0D or more D tensor of the zero or more elements by 2x from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • exp2() 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-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • *A float tensor is returned unless an input tensor is complex tensor.
  • torch.exp2() is the alias of torch.special.exp2().
  • The formula is y = 2x.
  • The graph in Desmos: Image description
import torch

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

torch.exp2(input=my_tensor)
my_tensor.exp2()
# tensor([0.2500, 0.5000, 1.0000, 2.0000, 4.0000, 8.0000])

my_tensor = torch.tensor([[-2., -1., 0.],
                          [1., 2., 3.]])
torch.exp2(input=my_tensor)
# tensor([[0.2500, 0.5000, 1.0000],
#         [2.0000, 4.0000, 8.0000]])

my_tensor = torch.tensor([[-2, -1, 0],
                          [1, 2, 3]])
torch.exp2(input=my_tensor)
# tensor([[0.2500, 0.5000, 1.0000],
#         [2.0000, 4.0000, 8.0000]])

my_tensor = torch.tensor([[-2.+0.j, -1.+0.j, 0.+0.j],
                          [1.+0.j, 2.+0.j, 3.+0.j]])
torch.exp2(input=my_tensor)
# tensor([[0.2500+0.j, 0.5000+0.j, 1.0000+0.j],
#         [2.0000+0.j, 4.0000+0.j, 8.0000+0.j]])

my_tensor = torch.tensor([[True, False, True],
                          [False, True, False]])
torch.exp2(input=my_tensor)
# tensor([[2., 1., 2.],
#         [1., 2., 1.]])
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .