【torch-Random sampling】随机采样

随机数种子和生成器状态

seed

torch.seed()
  • 功能

    将生成随机数的种子设置为非确定性随机数。返回用于种子RNG的64位数字。

  • 返回值类型

    int

manual_seed

torch.manual_seed(seed)
  • 功能

    设置生成随机数的种子。返回一个torch.Generator的对象

  • 参数

    • seed (int) -所需的种子。取值范围必须在[-0x8000_0000_0000_0000, 0xffff_ffff_ffff]之内。否则,将引发RuntimeError。使用公式0xffff_ffff_ffff_ffff + seed将负输入重映射为正值。
  • 返回值类型 : Generator

initial_seed

torch.initial_seed()
  • 功能

    返回作为Python long生成随机数的初始种子**。**

  • 返回值类型 : int

get_rng_state

torch.get_rng_state()
  • 功能

    返回作为torch.ByteTensor的随机数生成器状态。

  • 返回值类型: Tensor

set_rng_state

torch.set_rng_state(new_state)
  • 功能

    设置随机数生成器状态。

  • 参数

    • new_state (torch.ByteTensor) ****– 预期的状态

pytorch 内置的随机数生成器

bernoulli (伯努利分布)

torch.bernoulli(input, *, generator=None, out=None) → Tensor
  • 功能

从伯努利分布中抽取二元随机数(0 或者 1)。

输入张量须包含用于抽取上述二元随机值的概率。 因此,输入中的所有值都必须在[0,1]区间,即 0 < = i n p u t i < = 1 0<=input_i<=1 0<=inputi<=1

输出张量的第*i个元素值, 将会以输入张量的第i*个概率值等于1

 out  i ∼ Bernoulli ⁡ ( p =  input  i ) \text { out }_i \sim \operatorname{Bernoulli}\left(p=\text { input }_i\right)  out iBernoulli(p= input i)

返回的out张量只有值0或1,并且与input的形状相同。

out可以有整型dtype,但input必须有浮点型dtype。

  • 参数
    • input(tensor) – 伯努利分布的概率值的输入张量
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
    • out (Tensor, optional) – 其他关键参数
  • 例子
>>> a = torch.empty(3, 3).uniform_(0, 1)  # generate a uniform random matrix with range [0, 1]
>>> a
tensor([[ 0.1737,  0.0950,  0.3609],
        [ 0.7148,  0.0289,  0.2676],
        [ 0.9456,  0.8937,  0.7202]])
>>> torch.bernoulli(a)
tensor([[ 1.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 1.,  1.,  1.]])

>>> a = torch.ones(3, 3) # probability of drawing "1" is 1
>>> torch.bernoulli(a)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
>>> a = torch.zeros(3, 3) # probability of drawing "1" is 0
>>> torch.bernoulli(a)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

multinomial (多项分布)

torch.multinomial(input, num_samples, replacement=False, *, generator=None, out=None) → LongTensor
  • 功能: 返回一个张量,每行包含从input相应行中定义的多项分布中抽取的num_samples个样本。
  • 注意:
    • 输入input每行的值不需要总和为1 (这里我们用来做权重),但是必须非负且总和不能为0。
    • 当抽取样本时,依次从左到右排列(第一个样本对应第一列)。
    • 如果输入input是一个向量,输出out也是一个相同长度num_samples的向量。如果输入input是有 mm行的矩阵,输出out是形如m×nm×n的矩阵。
    • 如果参数replacementTrue, 则样本抽取可以重复。否则,一个样本在每行不能被重复抽取。
    • 参数num_samples必须小于input长度(即,input的列数,如果是input是一个矩阵)。
  • 参数
    • input (Tensor) – 包含概率值的张量
    • num_samples (int) – 抽取的样本数
    • replacement (bool, optional) – 布尔值,决定是否能重复抽取
    • out (Tensor, optional) – 结果张量
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
  • 例子
>>> weights = torch.tensor([0, 10, 3, 0], dtype=torch.float) # create a tensor of weights
>>> torch.multinomial(weights, 2)
tensor([1, 2])
>>> torch.multinomial(weights, 4) # ERROR!
RuntimeError: invalid argument 2: invalid multinomial distribution (with replacement=False, not enough non-negative category to sample) at ../aten/src/TH/generic/THTensorRandom.cpp:320
>>> torch.multinomial(weights, 4, replacement=True)
tensor([ 2,  1,  1,  1])

normal (正态分布)

格式1:

torch.normal(mean, std, *, generator=None, out=None) → Tenso
  • 功能

    返回一个张量,包含从给定参数means,std的离散正态分布中抽取随机数。 均值means是一个张量,包含每个输出元素相关的正态分布的均值。 std
    是一个张量,包含每个输出元素相关的正态分布的标准差。 均值和标准差的形状不须匹配,但每个张量的元素个数须相同。

  • 参数

    • means (Tensor) – 均值
    • std (Tensor) – 标准差
    • out (Tensor) – 可选的输出张量
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
  • 例子

>>> torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
tensor([  1.0425,   3.5672,   2.7969,   4.2925,   4.7229,   6.2134,
          8.0505,   8.1408,   9.0563,  10.0566])

格式2:

torch.normal(mean=0.0, std, *, out=None) → Tensor

类似于上面的函数,但是在所有抽取的元素之间共享平均数。

>>> torch.normal(mean=0.5, std=torch.arange(1., 6.))
tensor([-1.2793, -1.0732, -2.0687,  5.1177, -1.2303])

格式3:

torch.normal(mean, std=1.0, *, out=None) → Tensor

类似于上面的函数,但标准差在所有抽取的元素之间共享。

>>> torch.normal(mean=torch.arange(1., 6.))
tensor([ 1.1552,  2.6148,  2.6535,  5.8318,  4.2361])

格式4:

torch.normal(mean, std, size, *, out=None) → Tensor

与上面的函数相似,但是所有绘制的元素之间共享平均值和标准差。得到的张量的大小由size给出。

>>> torch.normal(2, 3, size=(1, 4))
tensor([[-1.3987, -1.9544,  3.6048,  0.7909]])

Poisson (泊松分布)

torch.poisson(input, generator=None) → Tensor
  • 功能

    返回一个与输入大小相同的张量,其中每个元素都从泊松分布中采样,速率参数由输入中相应的元素给出。其中input 必须为非负值

    o u t i ​ ∼ P o i s s o n ( i n p u t i ​ ) out_i​∼Poisson(input_i​) outiPoisson(inputi)

  • 参数

    • input (Tensor) - 包含泊松分布率的输入张量
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
  • 例子

>>> rates = torch.rand(4, 4) * 5  # rate parameter between 0 and 5
>>> torch.poisson(rates)
tensor([[9., 1., 3., 5.],
        [8., 6., 6., 0.],
        [0., 4., 5., 3.],
        [2., 1., 4., 2.]])

rand (区间[0,1)上的均匀分布)

torch.rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor
  • 功能

    返回一个张量,该张量来自于区间[0,1)上的均匀分布的随机数。张量的形状是由可变参数size定义的。

  • 参数

    • size(int) : ****定义输出张量形状的整数序列。可以是可变数量的参数,也可以是列表或元组等集合。
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
    • out(Tensor,optional) 输出张量
    • dtype (torch.dtype, optional) –返回张量的期望数据类型。
    • layout (torch.layout, optional) – 返回张量的期望layout。
    • device (torch.device, optional) – 返回张量的期望device。
    • requires_grad (bool, optional) - 如果autograd应该记录对返回张量的操作。
    • pin_memory (bool, optional) - 如果设置,返回的张量将分配到固定内存中。只适用于CPU张量。
  • 例子

torch.rand(4)
torch.rand(2, 3)

rand_like (区间[0,1)上的均匀分布、输出张量大小同输入张量)

torch.rand_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor
  • 功能: 返回一个与input大小相同的张量,该张量由来自区间[0,1)[0,1)上均匀分布的随机数填充。torch.rand_like(input) ****等价于 ****torch.rand(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)
  • 参数
    • input(Tensor) - 输入的大小将决定输出张量的大小。
    • dtype (torch.dtype, optional) –返回张量的期望数据类型。
    • layout (torch.layout, optional) – 返回张量的期望layout。
    • device (torch.device, optional) – 返回张量的期望device。
    • requires_grad (bool, optional) - 如果autograd应该记录对返回张量的操作。
    • memory_format (torch.memory_format, optional) - 返回张量所需的内存格式。

randint (区间[low,high) 上的均匀分布)

torch.randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
  • 功能

    返回一个张量,其中充满在low(包含)和high(不包含)之间均匀生成的随机整数。张量的形状是由可变参数size定义的。

  • 参数

    • low (int, optional) – 从分布中得到的最小整数(包括)。默认值:0。
    • high (int) – 从分布中抽取的最大整数(不包括)。
    • size (tuple) – 定义输出张量形状的元组。
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
    • out(Tensor,optional) 输出张量
    • dtype (torch.dtype, optional) –返回张量的期望数据类型。
    • layout (torch.layout, optional) – 返回张量的期望layout。
    • device (torch.device, optional) – 返回张量的期望device。
    • requires_grad (bool, optional) - 如果autograd应该记录对返回张量的操作。
  • 例子

>>> torch.randint(3, 5, (3,))
tensor([4, 3, 4])

>>> torch.randint(10, (2, 2))
tensor([[0, 2],
        [5, 5]])

>>> torch.randint(3, 10, (2, 2))
tensor([[4, 5],
        [6, 7]])

randint_like(区间[low,high) 上的均匀分布、输出张量大小同输入张量)

torch.randint_like(input, low=0, high, \*, dtype=None, layout=torch.strided, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor
  • 功能

    返回一个与张量输入形状相同的张量,填充在low(包含)和high(不包含)之间均匀生成的随机整数。

  • 参数

    • input(Tensor) - 输入的大小将决定输出张量的大小。
    • low (int, optional) – 从分布中得到的最小整数(包括)。默认值:0。
    • high (int) – 从分布中抽取的最大整数(不包括)。
    • dtype (torch.dtype, optional) –返回张量的期望数据类型。
    • layout (torch.layout, optional) – 返回张量的期望layout。
    • device (torch.device, optional) – 返回张量的期望device。
    • requires_grad (bool, optional) - 如果autograd应该记录对返回张量的操作。
    • memory_format (torch.memory_format, optional) - 返回张量所需的内存格式。

randn (标准正态分布)

torch.randn(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor
  • 功能

    返回一个由均值为0,方差为1的正态分布(也称为标准正态分布)中的随机数填充的张量。张量的形状是由可变参数size定义的。

  • 参数

    • size (int…) - 一个整数序列,它定义了输出张量的形状。可以是可变数量的参数,也可以是列表或元组等集合。
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
    • out(Tensor,optional) 输出张量
    • dtype (torch.dtype, optional) –返回张量的期望数据类型。
    • layout (torch.layout, optional) – 返回张量的期望layout。
    • device (torch.device, optional) – 返回张量的期望device。
    • requires_grad (bool, optional) - 如果autograd应该记录对返回张量的操作。
    • pin_memory (bool, optional) - 如果设置,返回的张量将分配到固定内存中。只适用于CPU张量。
  • 例子

>>> torch.randn(4)
tensor([-2.1436,  0.9966,  2.3426, -0.6366])
>>> torch.randn(2, 3)
tensor([[ 1.5954,  2.8929, -1.0923],
        [ 1.1719, -0.4709, -0.1996]])

randn_like (标准正态分布、输出张量大小同输入张量)

torch.randn_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor
  • 功能

    返回一个与input大小相同的张量,该张量由来自均值为0和方差为1的正态分布的随机数填充。torch.randn_like(input)等价于 torch.randn(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

  • 参数

    • input(Tensor) - 输入的大小将决定输出张量的大小。
    • dtype (torch.dtype, optional) –返回张量的期望数据类型。
    • layout (torch.layout, optional) – 返回张量的期望layout。
    • device (torch.device, optional) – 返回张量的期望device。
    • requires_grad (bool, optional) - 如果autograd应该记录对返回张量的操作。
    • memory_format (torch.memory_format, optional) - 返回张量所需的内存格式。

randperm (区间[0,n-1]上的随机排列)

torch.randperm(n, *, generator=None, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor
  • 功能

    返回从0到n -1的整数的随机排列。

  • 参数

    • n(int) -上界(排除)
    • generator (torch.generator, optional) – 用于采样的伪随机数发生器
    • out(Tensor,optional) 输出张量
    • dtype (torch.dtype, optional) –返回张量的期望数据类型。
    • layout (torch.layout, optional) – 返回张量的期望layout。
    • device (torch.device, optional) – 返回张量的期望device。
    • requires_grad (bool, optional) - 如果autograd应该记录对返回张量的操作。
    • pin_memory (bool, optional) - 如果设置,返回的张量将分配到固定内存中。只适用于CPU张量。
  • 例子

>>> torch.randperm(4)
tensor([2, 1, 0, 3])

参考

官方文档

seed Sets the seed for generating random numbers to a non-deterministic random number.
manual_seed Sets the seed for generating random numbers.
initial_seed Returns the initial seed for generating random numbers as a Python long.
get_rng_state Returns the random number generator state as a torch.ByteTensor.
set_rng_state Sets the random number generator state.
bernoulli Draws binary random numbers (0 or 1) from a Bernoulli distribution.
multinomial Returns a tensor where each row contains num_samples indices sampled from the multinomial probability distribution located in the corresponding row of tensor input.
normal Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.
poisson Returns a tensor of the same size as input with each element sampled from a Poisson distribution with rate parameter given by the corresponding element in input i.e.,
rand Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)
rand_like Returns a tensor with the same size as input that is filled with random numbers from a uniform distribution on the interval [0, 1)[0,1).
randint Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).
randint_like Returns a tensor with the same shape as Tensor input filled with random integers generated uniformly between low (inclusive) and high (exclusive).
randn Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).
randn_like Returns a tensor with the same size as input that is filled with random numbers from a normal distribution with mean 0 and variance 1.
randperm Returns a random permutation of integers from 0 to n - 1.

猜你喜欢

转载自blog.csdn.net/zyw2002/article/details/128207772
今日推荐