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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay