📜  Python PyTorch log2() 方法

📅  最后修改于: 2022-05-13 01:54:31.090000             🧑  作者: Mango

Python PyTorch log2() 方法

PyTorch log2() 方法计算输入张量元素的以 2 为底的对数。它按元素计算对数值。它将一个张量作为输入,并返回一个具有计算对数值的新张量。输入张量的元素必须在零和正无穷之间,因为函数log 2 (x) 的域是 (0,∞)。我们将为此方法使用以下语法-

让我们借助一些Python 3 程序示例来了解torch.log2()方法。

示例 1:

在这个例子中,我们定义了一个 1-D 张量,并计算其元素的以 2 为底的值的对数。

Python3
# Python3 program to demonstrate torch.log2() method
# importing torch
import torch
 
# defining a torch tensor
t = torch.tensor([2., 4., 7., 3.])
print('Tensor:', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:', result)


Python3
# Python3 program to demonstrate torch.log2() method
# for 2D tensors
# importing torch
import torch
 
# defining a 2D torch tensor with numbers sampled
# from the discrete uniform distribution
t = torch.empty((3, 4), dtype=torch.float32).random_(1, 50)
print('Tensor:\n', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:\n', result)


Python3
# Python3 program to demonstrate torch.log2() method
%matplotlib qt
 
# importing required libraries
import torch
import numpy as np
 
# import matplotlib.pyplot as plt
# defining a torch tensor
x = np.arange(1,50, 1)
t = torch.tensor(x)
print('Tensor:', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:', result)
 
# tensor to numpy array
y = result.numpy()
 
# plot the result using matplotlib
plt.plot(x, y, color = 'red', marker = "o")
plt.xlabel("x")
plt.ylabel("log2(x)")
plt.show()


Python3
# Python3 program to demonstrate torch.log2() method
# importing libraries
import torch
import numpy as np
 
# defining a torch tensor
t = torch.tensor([-3., -5., 0, 0.5, 7., 3., np.inf])
print('Original Tensor:\n', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:\n', result)


输出:

说明:对数是按元素计算的。

示例 2:

在下面的示例中,我们计算以 2 为底的二维张量的对数。

Python3

# Python3 program to demonstrate torch.log2() method
# for 2D tensors
# importing torch
import torch
 
# defining a 2D torch tensor with numbers sampled
# from the discrete uniform distribution
t = torch.empty((3, 4), dtype=torch.float32).random_(1, 50)
print('Tensor:\n', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:\n', result)

输出:

Tensor:
 tensor([[19., 32.,  2., 29.],
        [30., 10., 16., 18.],
        [49., 10.,  7., 25.]])
Logarithm of Tensor:
 tensor([[4.2479, 5.0000, 1.0000, 4.8580],
        [4.9069, 3.3219, 4.0000, 4.1699],
        [5.6147, 3.3219, 2.8074, 4.6439]])

说明:请注意,上述代码中的输入张量是用离散均匀分布采样的数字生成的,所以在执行代码时可能会生成不同的数字。

示例 3:

在下面的示例中,我们计算输入张量元素的以 2 为底的对数,并借助 Matplotlib 图将结果可视化。

Python3

# Python3 program to demonstrate torch.log2() method
%matplotlib qt
 
# importing required libraries
import torch
import numpy as np
 
# import matplotlib.pyplot as plt
# defining a torch tensor
x = np.arange(1,50, 1)
t = torch.tensor(x)
print('Tensor:', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:', result)
 
# tensor to numpy array
y = result.numpy()
 
# plot the result using matplotlib
plt.plot(x, y, color = 'red', marker = "o")
plt.xlabel("x")
plt.ylabel("log2(x)")
plt.show()

输出:

示例 4:

在此示例中,我们尝试计算以 2 为底的负值、零值和无穷大值的对数。看看输出如何。

Python3

# Python3 program to demonstrate torch.log2() method
# importing libraries
import torch
import numpy as np
 
# defining a torch tensor
t = torch.tensor([-3., -5., 0, 0.5, 7., 3., np.inf])
print('Original Tensor:\n', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:\n', result)

输出:

解释:看到负数的对数是nan(Not a Number)。 0 的对数是-inf。无穷大的对数是无穷大(inf)。