This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*My post explains log() and log1p().
log2() can get the 0D or more D tensor of the zero or more logarithms based on 2
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
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). - There is
out
argument withtorch
(Optional-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
- *A
float
tensor is returned unless an input tensor iscomplex
tensor. - 's formula is y = log2(x).
- 's graph in Desmos:
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.]]])
log10() can get the 0D or more D tensor of the zero or more logarithms based on 2
from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
log10()
can be used withtorch
or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). - There is
out
argument withtorch
(Optional-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
- *A
float
tensor is returned unless an input tensor iscomplex
tensor. - 's formula is y = log10(x).
- 's graph in Desmos:
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.]]])
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) | Sciencx (2024-09-02T17:32:09+00:00) log2() and log10() in PyTorch. Retrieved from https://www.scien.cx/2024/09/02/log2-and-log10-in-pytorch/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.