介绍pytorch中的random模块。
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
torch.initial_seed()
Return type:
int
返回用于生成随机数的初始种子,类型为 Python 的 long。
*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类型。
*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 是 CUDA 张量时,此函数将其设备与 CPU 同步。
**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 定义。
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 的张量。
*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.