當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python Pytorch logspace()用法及代碼示例


PyTorch是由Facebook開發的開源機器學習庫。它用於深度神經網絡和自然語言處理。

函數torch.logspace()返回一階步張量的一維張量,與之間的底數成對數間隔 {\text{base}}^{\text{start}}   and {\text{ base}}^{\text{end}}

輸出張量是尺寸步長的一維。

用法:torch.logspace(start, end, steps=100, base=10, out=None)

參數
start:點集的起始值。
end:點集的最終值
steps:在開始和結束之間要采樣的點數。默認值:100
base:對數函數的基數。默認值:10.0
out(Tensor, optional):輸出張量



返回類型:張量

代碼1:

# Importing the PyTorch library 
import torch 
  
# Applying the logspace function and 
# storing the resulting tensor in 't' 
a = torch.logspace(3, 10, 5) 
print("a = ", a) 
  
b = torch.logspace(start =-10, end = 10, steps = 5) 
print("b = ", b)

輸出:

a =  tensor([1.0000e+03, 5.6234e+04, 3.1623e+06, 1.7783e+08, 1.0000e+10])
b =  tensor([1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])

代碼2:可視化

# Importing the PyTorch library 
import torch 
# Importing the NumPy library 
import numpy as np 
  
# Importing the matplotlib.pylot function 
import matplotlib.pyplot as plt 
  
# Applying the logspace function to get a tensor of size 15 with values from -5 to 5 using base 2 
a = torch.logspace(-5, 5, 15, 2) 
print(a) 
  
# Plotting 
plt.plot(a.numpy(), np.zeros(a.numpy().shape), color = 'red', marker = "o")  
plt.title("torch.linspace")  
plt.xlabel("X")  
plt.ylabel("Y")  
  
plt.show()

輸出:

tensor([3.1250e-02, 5.1271e-02, 8.4119e-02, 1.3801e-01, 2.2643e-01, 3.7150e-01,
        6.0951e-01, 1.0000e+00, 1.6407e+00, 2.6918e+00, 4.4164e+00, 7.2458e+00,
        1.1888e+01, 1.9504e+01, 3.2000e+01])
[torch.FloatTensor of size 15]




相關用法


注:本文由純淨天空篩選整理自sanskar27jain大神的英文原創作品 Python Pytorch logspace() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。