torch.randint
- torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
-
返回一个包含从
low(包括)到high(不包括)之间的均匀分布随机整数的张量。张量的形状由参数
size定义。注意
在使用全局数据类型默认值(
torch.float32)的情况下,该函数返回一个数据类型为torch.int64的张量。- 参数
- 关键字参数
-
-
generator (
torch.Generator, 可选) – 用于样本采集的伪随机数生成器 -
out (Tensor, 可选) – 指定输出张量。
-
dtype (torch.dtype, 可选) – 如果为
None,此函数将返回一个数据类型为torch.int64的张量。 -
layout (
torch.layout, 可选) - 指定返回张量的布局。默认值为torch.strided。 -
device (
torch.device, 可选) – 返回张量所需的设备。默认情况下,如果未指定None,则使用当前的默认张量类型设备(参见torch.set_default_device())。对于 CPU 张量类型,默认为 CPU 设备;对于 CUDA 张量类型,则为当前的 CUDA 设备。 -
requires_grad (bool, optional) – 是否启用自动求导记录返回的张量上的操作。默认值:
False。
-
示例:
>>> 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]])