【PyTorch】PyTorch之Random模块


前言

介绍pytorch中的random模块。

一、SEED

torch.seed()
Return type:
int

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

二、使用步骤

torch.manual_seed(seed)
Parameters:
seed (int) – The desired seed. Value must be within the inclusive range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula 0xffff_ffff_ffff_ffff + seed.
Return type:
Generator

三、INITIAL_SEED

torch.initial_seed()
Return type:
int

返回用于生成随机数的初始种子,类型为 Python 的 long。

四、BERNOULLI

*torch.bernoulli(input, , generator=None, out=None) → Tensor
Parameters:
input (Tensor) – the input tensor of probability values for the Bernoulli distribution
Keyword Arguments:
generator (torch.Generator, optional) – a pseudorandom number generator for sampling
out (Tensor, optional) – the output tensor.

从伯努利分布中绘制二进制随机数(0或1)。输入张量应该是一个包含用于生成二进制随机数的概率的张量。因此,输入的所有值都应该在[0, 1]范围内。
第i个元素的输出值,以输入的第i值为概率,生成0或者1。
在这里插入图片描述
输出的tensor里的值为0或者1,并且其形状大小与输入相同。out可以是整型,但是input必须是float类型。
在这里插入图片描述

五、NORMAL

*torch.normal(mean, std, , generator=None, out=None) → Tensor
Parameters:
mean (Tensor) – the tensor of per-element means
std (Tensor) – the tensor of per-element standard deviations
Keyword Arguments:
generator (torch.Generator, optional) – a pseudorandom number generator for sampling
out (Tensor, optional) – the output tensor.

返回从具有给定均值和标准差的不同正态分布中抽取的随机数的张量。

  • 均值是一个张量时,表示每个输出元素的正态分布的均值。
  • std 是一个张量时,表示每个输出元素的正态分布的标准差。

均值和 std 的形状不需要匹配,但是每个张量中元素的总数需要相同。
注意:
当形状不匹配时,均值的形状将用作返回的输出张量的形状。
当 std 是 CUDA 张量时,此函数将其设备与 CPU 同步。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

六、RAND

**torch.rand(size, , generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor
Parameters:
size (int…) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
Keyword Arguments:
generator (torch.Generator, optional) – a pseudorandom number generator for sampling
out (Tensor, optional) – the output tensor.
dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).
layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

返回一个张量,其中填充有在区间[0, 1)上均匀分布的随机数。
张量的形状由可变参数 size 定义。
在这里插入图片描述

七、RANDINT

torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Parameters:
low (int, optional) – Lowest integer to be drawn from the distribution. Default: 0.
high (int) – One above the highest integer to be drawn from the distribution.
size (tuple) – a tuple defining the shape of the output tensor.
Keyword Arguments:
generator (torch.Generator, optional) – a pseudorandom number generator for sampling
out (Tensor, optional) – the output tensor.
dtype (torch.dtype, optional) – if None, this function returns a tensor with dtype torch.int64.
layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

返回一个张量,其中填充有在 low(包含)和 high(不包含)之间均匀生成的随机整数。
张量的形状由可变参数 size 定义。
注意:
使用全局 dtype 默认值(torch.float32)时,此函数返回一个 dtype 为 torch.int64 的张量。
在这里插入图片描述

九、RANDPERM

*torch.randperm(n, , generator=None, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor
Parameters:
n (int) – the upper bound (exclusive)
Keyword Arguments:
generator (torch.Generator, optional) – a pseudorandom number generator for sampling
out (Tensor, optional) – the output tensor.
dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: torch.int64.
layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

返回从0到n - 1的整数的随机排列。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值