DEV Community

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

Posted on • Edited on

log2 and log10 in PyTorch

Buy Me a Coffee

*Memos:

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

*Memos:

  • log2() 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 = log2(x).
  • The graph in Desmos: Image description
import torch

my_tensor = torch.tensor([-0.1, 0.0, 0.1, 0.9, 1.0, 1.1, 2.0, 100.0])

torch.log2(input=my_tensor)
my_tensor.log2()
# tensor([nan, -inf, -3.3219, -0.1520, 0.0000, 0.1375, 1.0000, 6.6439])

my_tensor = torch.tensor([[-0.1, 0.0, 0.1, 0.9],
                          [1.0, 1.1, 2.0, 100.0]])
torch.log2(input=my_tensor)
# tensor([[nan, -inf, -3.3219, -0.1520],
#         [0.0000, 0.1375, 1.0000, 6.6439]])

my_tensor = torch.tensor([[[-0.1, 0.0], [0.1, 0.9]],
                          [[1.0, 1.1], [2.0, 100.0]]])
torch.log2(input=my_tensor)
# tensor([[[nan, -inf], [-3.3219, -0.1520]],
#         [[0.0000, 0.1375], [1.0000, 6.6439]]])

my_tensor = torch.tensor([[[-0.1+0.j, 0.0+0.j], [0.1+0.j, 0.9+0.j]],
                          [[1.0+0.j, 1.1+0.j], [2.0+0.j, 100.0+0.j]]])
torch.log2(input=my_tensor)
# tensor([[[-3.3219+4.5324j, -inf+0.0000j],
#          [-3.3219+0.0000j, -0.1520+0.0000j]],
#         [[0.0000+0.0000j, 0.1375+0.0000j],
#          [1.0000+0.0000j, 6.6439+0.0000j]]])

my_tensor = torch.tensor([[[-1, 0], [1, 2]],
                          [[5, 8], [10, 100]]])
torch.log2(input=my_tensor)
# tensor([[[nan, -inf], [0.0000, 1.0000]],
#         [[2.3219, 3.0000], [3.3219, 6.6439]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.log2(input=my_tensor)
# tensor([[[0., -inf], [0., -inf]],
#         [[-inf, 0.], [-inf, 0.]]])
Enter fullscreen mode Exit fullscreen mode

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

*Memos:

  • log10() 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 = log10(x).
  • The graph in Desmos: Image description
import torch

my_tensor = torch.tensor([-0.1, 0.0, 0.1, 0.9, 1.0, 1.1, 10.0, 100.0])

torch.log10(input=my_tensor)
my_tensor.log10()
# tensor([nan, -inf, -1.0000, -0.0458, 0.0000, 0.0414, 1.0000, 2.0000])

my_tensor = torch.tensor([[-0.1, 0.0, 0.1, 0.9],
                          [1.0, 1.1, 10.0, 100.0]])
torch.log10(input=my_tensor)
# tensor([[nan, -inf, -1.0000, -0.0458],
#         [0.0000, 0.0414, 1.0000, 2.0000]])

my_tensor = torch.tensor([[[-0.1, 0.0], [0.1, 0.9]],
                          [[1.0, 1.1], [10.0, 100.0]]])
torch.log10(input=my_tensor)
# tensor([[[nan, -inf],
#          [-1.0000, -0.0458]],
#         [[0.0000, 0.0414],
#          [1.0000, 2.0000]]])

my_tensor = torch.tensor([[[-0.1+0.j, 0.0+0.j], [0.1+0.j, 0.9+0.j]],
                          [[1.0+0.j, 1.1+0.j], [10.0+0.j, 100.0+0.j]]])
torch.log10(input=my_tensor)
# tensor([[[-1.0000+1.3644j, -inf+0.0000j],
#          [-1.0000+0.0000j, -0.0458+0.0000j]],
#         [[0.0000+0.0000j, 0.0414+0.0000j],
#          [1.0000+0.0000j, 2.0000+0.0000j]]])

my_tensor = torch.tensor([[[-1, 0], [1, 2]],
                          [[5, 8], [10, 100]]])
torch.log10(input=my_tensor)
# tensor([[[nan, -inf], [0.0000, 0.3010]],
#         [[0.6990, 0.9031], [1.0000, 2.0000]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.log10(input=my_tensor)
# tensor([[[0., -inf], [0., -inf]],
#         [[-inf, 0.], [-inf, 0.]]])
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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

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

Okay