Random sampling

torch.normal()

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

从一个给定均值和标准差的正态分布中生成随机数张量。

mean是张量,包含每个输出元素相关正态分布的均值。

std是张量,具有每个输出元素相关正态分布的标准差。

mean和std的形状不需要匹配,但每个张量中的元素总数需要相同。

参数:

  • mean (Tensor) – 每个元素均值的张量
  • std (Tensor) – 每个元素标准差的张量
  • out (Tensor, optional) – 输出张量

示例:

output = torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1., 0., -0.1))
print(output)

# 结果
tensor([1.6026, 1.7199, 1.9146, 4.6393, 4.4990, 5.6012, 6.7808, 8.1370, 8.9669,
        9.9901])

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

与上面函数类似,但是均值由所有元素共享。

参数:

  • mean (float, optional) – 整个分布的均值

  • std (Tensor) – 每个元素标准差的张量

  • out (Tensor*, *optional) – 输出张量

示例:

output = torch.normal(mean=0.5, std=torch.arange(1., 6.))
print(output)

# 结果
tensor([ 0.0717,  1.9929, -0.5948,  2.4134,  2.4349])

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

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

参数:

  • mean (Tensor) – 每个元素均值的张量

  • std (float, optional) – 整个分布的标准差

  • out (Tensor, optional) – 输出张量

示例:

output = torch.normal(mean=torch.arange(1., 6.))
print(output)

# 结果
tensor([1.3809, 2.2868, 4.4226, 2.6401, 4.3659])

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

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

参数:

  • mean (float) – 整个分布的均值

  • std (float) – 整个分布的标准差

  • size (int...) – 定义输出张量形状的整数序列

  • out (Tensor*, *optional) – 输出张量

示例:

output = torch.normal(2, 3, size=(1, 4))
print(output)

# 结果
tensor([[ 0.3201,  5.6169,  6.0626, -0.2256]])

torch.rand()

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

返回在区间[0,1] 上的均匀分布填充的随机数张量。

张量的形状由变量参数size定义。

参数:

  • size (int...) – 定义输出张量形状的整数序列。可以是可变数量的参数,也可以是列表或元组之类的集合

  • out (Tensor, optional) – 输出张量

  • dtype (torch.dtype, optional) – 返回张量的所需数据类型。默认值:如果为None,则使用全局默认值(请参阅 torch.set_default_tensor_type())

  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.

  • device (torch.device, optional) – 返回张量的所需设备。默认值:如果为None,则使用当前设备作为默认张量类型(请参阅torch.set_default_tensor_type())。 device是CPU时为CPU张量类型和当前CUDA设备时为CUDA张量类型。

  • requires_grad (bool, optional) – 是否autograd应该在返回的张量上记录操作。默认值:False

示例:

output1 = torch.rand(4)
print(output1)
output2 = torch.rand(2, 3)
print(output2)

# 结果
tensor([0.3985, 0.1540, 0.6752, 0.8953])
tensor([[0.8526, 0.4698, 0.4647],
        [0.9193, 0.5770, 0.9554]])

torch.randint()

torch.randint(low=0, high, size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

返回一个填充了在lowhigh之间均匀生成的随机整数的张量。

张量的形状由变量参数size定义。

参数:

  • low (int, optional) – 从分布中提取的最小整数。默认值:0。

  • high (int) – 从分布中提取的最高整数。

  • size (tuple) – 定义输出张量形状的元组。

  • out (Tensor, optional) – 输出张量

  • dtype (torch.dtype, optional) – 返回张量的所需数据类型。默认值:如果为None,则使用全局默认值(请参阅 torch.set_default_tensor_type())

  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.

  • device (torch.device, optional) – 返回张量的所需设备。默认值:如果为None,则使用当前设备作为默认张量类型(请参阅torch.set_default_tensor_type())。 device是CPU时为CPU张量类型和当前CUDA设备时为CUDA张量类型。

  • requires_grad (bool, optional) – 是否autograd应该在返回的张量上记录操作。默认值:False

示例:

output1 = torch.randint(3, 5, (3, ))
print(output1)
output2 = torch.randint(10, (2, 2))
print(output2)
output3 = torch.randint(3, 10, (2, 2))
print(output3)

# 结果
tensor([4, 4, 4])
tensor([[1, 0],
        [0, 0]])
tensor([[9, 8],
        [9, 3]])

torch.randn()

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

返回一个填充了正态分布中随机数的张量,其均值为0,方差为1(也称为标准正态分布)

out_i ~ N(0, 1)\tag{.1}

张量的形状由变量参数size定义。

参数:

  • size (int...) – 定义输出张量形状的整数序列。可以是可变数量的参数,也可以是列表或元组之类的集合。

  • out (Tensor, optional) – 输出张量

  • dtype (torch.dtype, optional) – 返回张量的所需数据类型。默认值:如果为None,则使用全局默认值(请参阅 torch.set_default_tensor_type())

  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.

  • device (torch.device, optional) – 返回张量的所需设备。默认值:如果为None,则使用当前设备作为默认张量类型(请参阅torch.set_default_tensor_type())。 device是CPU时为CPU张量类型和当前CUDA设备时为CUDA张量类型。

  • requires_grad (bool, optional) – 是否autograd应该在返回的张量上记录操作。默认值:False

示例:

output1 = torch.randn(4)
print(output1)
output2 = torch.randn(2, 3)
print(output2)

# 结果
tensor([-0.2287,  1.2439,  1.1917,  0.1586])
tensor([[ 0.1352, -0.0118, -0.0640],
        [-0.0554,  0.5129,  1.2477]])

完整示例:

import torch
import torch.nn as nn

"""
torch.normal
"""
# torch.normal(mean, std, out=None) → Tensor
print(torch.arange(1., 11.))
print(torch.arange(1., 0., -0.1))

output = torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1., 0., -0.1))
print(output)

# torch.normal(mean=0.0, std, out=None) → Tensor
output = torch.normal(mean=0.5, std=torch.arange(1., 6.))
print(output)

# torch.normal(mean, std=1.0, out=None) → Tensor
output = torch.normal(mean=torch.arange(1., 6.))
print(output)

# torch.normal(mean, std, size, *, out=None) → Tensor
output = torch.normal(2, 3, size=(1, 4))
print(output)

"""
torch.rand
"""
# torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
output1 = torch.rand(4)
print(output1)
output2 = torch.rand(2, 3)
print(output2)

# torch.randint(low=0, high, size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
output1 = torch.randint(3, 5, (3, ))
print(output1)
output2 = torch.randint(10, (2, 2))
print(output2)
output3 = torch.randint(3, 10, (2, 2))
print(output3)

"""
torch.randn (also called the standard normal distribution)
"""
# torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
output1 = torch.randn(4)
print(output1)
output2 = torch.randn(2, 3)
print(output2)

# 结果
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
tensor([1.0000, 0.9000, 0.8000, 0.7000, 0.6000, 0.5000, 0.4000, 0.3000, 0.2000,
        0.1000])
tensor([1.6026, 1.7199, 1.9146, 4.6393, 4.4990, 5.6012, 6.7808, 8.1370, 8.9669,
        9.9901])
tensor([ 0.0717,  1.9929, -0.5948,  2.4134,  2.4349])
tensor([1.3809, 2.2868, 4.4226, 2.6401, 4.3659])
tensor([[ 0.3201,  5.6169,  6.0626, -0.2256]])
tensor([0.3985, 0.1540, 0.6752, 0.8953])
tensor([[0.8526, 0.4698, 0.4647],
        [0.9193, 0.5770, 0.9554]])
tensor([4, 4, 4])
tensor([[1, 0],
        [0, 0]])
tensor([[9, 8],
        [9, 3]])
tensor([-0.2287,  1.2439,  1.1917,  0.1586])
tensor([[ 0.1352, -0.0118, -0.0640],
        [-0.0554,  0.5129,  1.2477]])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容