DEV Community

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

Posted on • Edited on

exp and exp2 in PyTorch

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay