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