DEV Community

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

Posted on • Edited on

log and log1p in PyTorch

Buy Me a Coffee

*Memos:

log() can get the 0D or more D tensor of the zero or more elements by ln(x) which is the natural logarithm based on e, from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • log() 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 = ln(x) or y = loge(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, torch.e, 100.0])

torch.log(input=my_tensor)
my_tensor.log()
# tensor([nan, -inf, -2.3026, -0.1054, 0.0000, 0.0953, 1.0000, 4.6052])

my_tensor = torch.tensor([[-0.1, 0.0, 0.1, 0.9],
                          [1.0, 1.1, torch.e, 100.0]])
torch.log(input=my_tensor)
# tensor([[nan, -inf, -2.3026, -0.1054],
#         [0.0000, 0.0953, 1.0000, 4.6052]])

my_tensor = torch.tensor([[[-0.1, 0.0], [0.1, 0.9]],
                          [[1.0, 1.1], [torch.e, 100.0]]])
torch.log(input=my_tensor)
# tensor([[[nan, -inf], [-2.3026, -0.1054]],
#         [[0.0000, 0.0953], [1.0000, 4.6052]]])

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], [torch.e+0.j, 100.0+0.j]]])
torch.log(input=my_tensor)
# tensor([[[-2.3026+3.1416j, -inf+0.0000j],
#          [-2.3026+0.0000j, -0.1054+0.0000j]],
#         [[0.0000+0.0000j, 0.0953+0.0000j],
#          [1.0000+0.0000j, 4.6052+0.0000j]]])

my_tensor = torch.tensor([[[-1, 0], [1, 2]],
                          [[5, 8], [10, 100]]])
torch.log(input=my_tensor)
# tensor([[[nan, -inf], [0.0000, 0.6931]],
#         [[1.6094, 2.0794], [2.3026, 4.6052]]])

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

log1p() can get the 0D or more D tensor of the zero or more elements by ln(x + 1) which is the natural logarithm based on e, from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • log1p() 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 = ln(x + 1) or y = loge(x + 1).
  • The graph in Desmos: Image description
import torch

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

torch.log1p(input=my_tensor)
my_tensor.log1p()
# tensor([nan, -inf, -2.3026, -0.1054, 0.0000, 0.0953, 1.3133, 2.3979])

my_tensor = torch.tensor([[-1.1, -1.0, -0.9, -0.1],
                          [0.0, 0.1, torch.e, 10.0]])
torch.log1p(input=my_tensor)
# tensor([[nan, -inf, -2.3026, -0.1054],
#         [0.0000, 0.0953, 1.3133, 2.3979]])

my_tensor = torch.tensor([[[-1.1, -1.0], [-0.9, -0.1]],
                          [[0.0, 0.1], [torch.e, 10.0]]])
torch.log1p(input=my_tensor)
# tensor([[[nan, -inf], [-2.3026, -0.1054]],
#         [[0.0000, 0.0953], [1.3133, 2.3979]]])

my_tensor = torch.tensor([[[-1.1+0.j, -1.0+0.j], [-0.9+0.j, -0.1+0.j]],
                          [[0.0+0.j, 0.1+0.j], [torch.e+0.j, 10.0+0.j]]])
torch.log1p(input=my_tensor)
# tensor([[[-2.3026+3.1416j, -inf+0.0000j],
#          [-2.3026+0.0000j, -0.1054+0.0000j]],
#         [[0.0000+0.0000j, 0.0953+0.0000j],
#          [1.3133+0.0000j, 2.3979+0.0000j]]])

my_tensor = torch.tensor([[[-1, 0], [1, 2]],
                          [[5, 8], [10, 100]]])

my_tensor = torch.tensor([[[-2, -1], [0, 1]],
                          [[2, 5], [8, 10]]])
torch.log1p(input=my_tensor)
# tensor([[[nan, -inf], [0.0000, 0.6931]],
#         [[1.0986, 1.7918], [2.1972, 2.3979]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.log1p(input=my_tensor)
# tensor([[[0.6931, 0.0000], [0.6931, 0.0000]],
#         [[0.0000, 0.6931], [0.0000, 0.6931]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)