【PyTorch】PyTorch之Random模块

文章目录

  • 前言
  • 一、SEED
  • 二、使用步骤
  • 三、INITIAL_SEED
  • 四、BERNOULLI
  • 五、NORMAL
  • 六、RAND
  • 七、RANDINT
  • 九、RANDPERM


前言

介绍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类型。
【PyTorch】PyTorch之Random模块_第1张图片

五、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 定义。
【PyTorch】PyTorch之Random模块_第2张图片

七、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 的张量。
【PyTorch】PyTorch之Random模块_第3张图片

九、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的整数的随机排列。
在这里插入图片描述

你可能感兴趣的:(pytorch,人工智能,python)