PyTorch-1.10(三)--torch张量随机生成、采样

随机采样

设置生成随机数的种子为非确定性随机数。torch.seed() 设置生成随机数的种子,返回Generator。torch.manual_seed(2022) 返回生成随机数的初始种子。torch.initial_seed() 将随机数生成器状态以torch.ByteTensor返回。torch.get_rng_state()

默认返回CPU torch.Generator

从伯努利分布中提取二进制随机数(0或1),torch.bernoulli(torch.empty(3, 3).uniform_(0, 1)) 返回一个张量,其中每行包含从位于张量输入对应行的多项式概率分布中采样的num_samples索引。 返回从单独的正态分布中提取的随机数张量,其平均值和标准偏差均已给出。torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1)) 返回与输入大小相同的张量,每个元素从泊松分布中采样,速率参数由输入中的相应元素给出. torch.poisson(torch.rand(4,4)*5) 返回一个由区间[0,1)上均匀分布的随机数填充的张量,torch.rand(3,4) 返回一个与输入大小相同的张量,该张量由区间[0,1)上均匀分布的随机数填充。 返回一个由随机整数填充的张量,该随机整数在[低,高)之间生成。torch.randint(3,9,(3,4)) 返回一个与张量输入形状相同的张量,该张量由在[低,高)之间均匀生成的随机整数填充。 返回一个由均值为0、方差为1的标准正态分布中的随机数填充的张量。torch.randn(3,4) 返回一个与输入大小相同的张量,由均值为0、方差为1的正态分布中的随机数填充。 返回从0到n-1的整数的随机排列。torch.randperm(9)

就地随机抽样

还有一些在张量上定义的随机抽样函数:

    - in-place version of - numbers drawn from the Cauchy distribution - numbers drawn from the exponential distribution - elements drawn from the geometric distribution - samples from the log-normal distribution - in-place version of - numbers sampled from the discrete uniform distribution - numbers sampled from the continuous uniform distribution

准随机抽样

torch.quasirandom.SoboEngine是生成Sobol序列的引擎。
经验分享 程序员 微信小程序 职场和发展