torch.
rand
(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
#返回從[0,1)均勻分布中抽取的一組隨機數;均勻分布采樣;#*sizes指定張量的形狀;
torch.randn(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) #返回從標准正態分布中抽取的一組隨機數,也即高斯白噪聲;
torch.
normal
(mean, std, *, generator=None, out=None) → Tensor
返回一個由不同正態分布的隨機數組成的張量,其均值和標准差已給出。
均值是一個張量,它是每個輸出元素正態分布的均值;
std是一個張量,它是每個輸出元素正態分布的標准差;
均值和標准差的形狀不需要匹配,但每個張量的元素總數需要相同。
torch.normal()#返回正態分布;
#采樣離散正態分布,相比c於torch.randn,每一個數字都可以來自不同均值和方差的正態分布 #有以下三種形式 torch.normal(mean=0.0, std, out=None) → Tensor #mean為標量(共用),std為tensor指定shape和每個位置的方差 torch.normal(mean, std=1.0, out=None) → Tensor #std為標量(共用),mean為tensor指定shape和每個位置的均值 torch.normal(mean, std, size, out=None) → Tensor #std和mean都是標量,此時需要size指定shape
randn和normal的區別
雖然randn和normal都可以生成服從正態分布的隨機數,但是normal可以自己設定均值和標准差。就這點區別。
torch.randperm(n, out=None, requires_grad=True)#返回從0到n-1的整數的隨機排列數
torch.linespace()
#線性間距采樣 torch.linspace(start, end, steps=100, out=None) → Tensor #start起點,end終點,steps點數
torch.randperm()
randperm(n, out=None, dtype=torch.int64)-> LongTensor
#torch中沒有random.shuffle
#y = torch.randperm(n) y是把1到n這些數隨機打亂得到的一個數字序列,給定參數n
,返回一個從[0, n -1) 的隨機整數排列。n (int) – 上邊界。(不包含)
torch.randint(low=0, high, size, out=None, requires_grad=False)
返回一個張量,該張量填充了在[low,high)均勻生成的隨機整數。
張量的形狀由可變的參數大小定義。
從離散正態分布中隨機抽樣
torch.normal(means, std, out=None) → → Tensor
線性間距向量
torch.linspace(start, end, steps=100, out=None) → Tensor
返回一個1維張量,包含在區間start和end上均勻間隔的step個點。其中包含開頭和結尾點,也就是開頭是start數值,結尾是end對應的數值。
輸出張量的長度由steps決定。如果不指定steps,默認輸出從開始到結尾的100個數值,具體如下:
torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Creates a one-dimensional tensor of size steps
whose values are evenly spaced from start
to end
, inclusive. That is, the value are:
參考:https://pytorch.org/docs/stable/generated/torch.linspace.html#torch.linspace
From PyTorch 1.11 linspace requires the steps argument. Use steps=100 to restore the previous behavior.
#Examples: torch.linspace(3, 10, steps=5) torch.linspace(-10, 10, steps=5) torch.linspace(start=-10, end=10, steps=5) torch.linspace(start=-10, end=10, steps=1)
numpy.linspace
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)[source]
參考:https://numpy.org/devdocs/reference/generated/numpy.linspace.html?highlight=linspace#numpy.linspace
返回一個1維張量,包含在區間start和end上均勻間隔的step個點。其中包含開頭和結尾點,也就是開頭是start數值,結尾是end對應的數值。
Python numpy函數:linspace處()創建等差數列
linspace用於創建一個是等差數列的一維數組。它創建的數組元素的數據格式是浮點型。
常看到的一般是三個參數,分別是:起始值、終止值(默認包含自身)、數列個數
輸出張量的長度由steps決定。如果不指定steps,默認輸出從開始到結尾的50個數值,具體如下:
import numpy as np x=np.linspace(5,6) x.shape,len(x),x.size,x.dtype,x #輸出: ((50,), 50, 50, dtype('float64'), array([5. , 5.02040816, 5.04081633, 5.06122449, 5.08163265, 5.10204082, 5.12244898, 5.14285714, 5.16326531, 5.18367347, 5.20408163, 5.2244898 , 5.24489796, 5.26530612, 5.28571429, 5.30612245, 5.32653061, 5.34693878, 5.36734694, 5.3877551 , 5.40816327, 5.42857143, 5.44897959, 5.46938776, 5.48979592, 5.51020408, 5.53061224, 5.55102041, 5.57142857, 5.59183673, 5.6122449 , 5.63265306, 5.65306122, 5.67346939, 5.69387755, 5.71428571, 5.73469388, 5.75510204, 5.7755102 , 5.79591837, 5.81632653, 5.83673469, 5.85714286, 5.87755102, 5.89795918, 5.91836735, 5.93877551, 5.95918367, 5.97959184, 6. ]))
torch.Tensor.uniform_()
torch.rand和torch.Tensor.uniform_
這樣看到話,兩個都能取0-1之間的均勻分布,但是問題在於rand取不到1,uniform_可以取到1。
此外,可參考:
https://blog.csdn.net/u011699626/article/details/112062173