PyTorch log2() 方法計算輸入張量元素以 2 為底的對數。它按元素計算對數值。它將張量作為輸入並返回具有計算對數值的新張量。輸入張量的元素必須介於零和正無窮大之間,作為函數 log 的域2(x) 為 (0, ∞)。我們將對此方法使用以下語法 -
用法: torch.log2(input, out=None)
參數:
- input:輸入張量。
- out:輸出張量。
返回:它返回一個新張量,其對數為輸入張量元素的以 2 為底的值。
讓我們借助一些 Python 3 程序示例來了解 torch.log2() 方法。
示例 1:
在此示例中,我們定義一個一維張量,並計算其元素以 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)
輸出:
Tensor: tensor([2., 4., 7., 3.])
Logarithm of Tensor: tensor([1.0000, 2.0000, 2.8074, 1.5850])
說明: 對數是按元素計算的。
示例 2:
在下麵的示例中,我們計算 2D 張量以 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()
輸出:
Tensor: tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49], dtype=torch.int32)
Logarithm of Tensor: tensor([0.0000, 1.0000, 1.5850, 2.0000, 2.3219, 2.5850, 2.8074, 3.0000, 3.1699,
3.3219, 3.4594, 3.5850, 3.7004, 3.8074, 3.9069, 4.0000, 4.0875, 4.1699,
4.2479, 4.3219, 4.3923, 4.4594, 4.5236, 4.5850, 4.6439, 4.7004, 4.7549,
4.8074, 4.8580, 4.9069, 4.9542, 5.0000, 5.0444, 5.0875, 5.1293, 5.1699,
5.2095, 5.2479, 5.2854, 5.3219, 5.3576, 5.3923, 5.4263, 5.4594, 5.4919,
5.5236, 5.5546, 5.5850, 5.6147])
示例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)
輸出:
Original Tensor:
tensor([-3.0000, -5.0000, 0.0000, 0.5000, 7.0000, 3.0000, inf])
Logarithm of Tensor:
tensor([ nan, nan, -inf, -1.0000, 2.8074, 1.5850, inf])
解釋:看到負數的對數是nan(Not a Number)。 0的對數是-inf。無窮大的對數就是無窮大(inf)。
相關用法
- Python PyTorch log()用法及代碼示例
- Python PyTorch acos()用法及代碼示例
- Python PyTorch asin()用法及代碼示例
- Python PyTorch atan()用法及代碼示例
- Python PyTorch cos()用法及代碼示例
- Python PyTorch cosh()用法及代碼示例
- Python PyTorch sin()用法及代碼示例
- Python PyTorch sinh()用法及代碼示例
- Python PyTorch tan()用法及代碼示例
- Python PyTorch tanh()用法及代碼示例
- Python PyTorch from_numpy()用法及代碼示例
- Python PyTorch div()用法及代碼示例
- Python PyTorch clamp()用法及代碼示例
- Python PyTorch ceil()用法及代碼示例
- Python PyTorch add()用法及代碼示例
- Python PyTorch abs()用法及代碼示例
- Python PyTorch exp()用法及代碼示例
- Python PyTorch numel()用法及代碼示例
- Python PyTorch is_storage()用法及代碼示例
- Python PyTorch is_tensor()用法及代碼示例
- Python PyTorch trunc()用法及代碼示例
- Python PyTorch frac()用法及代碼示例
- Python PyTorch fmod()用法及代碼示例
- Python PyTorch floor()用法及代碼示例
- Python PyTorch zeros()用法及代碼示例
注:本文由純淨天空篩選整理自shahidedu7大神的英文原創作品 Python PyTorch log2() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。