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定义。注意
使用全局 dtype 默认值(
torch.float32),此函数返回一个 dtype 为torch.int64的张量。- 参数
- 关键字参数
generator (
torch.Generator, 可选) – 用于采样的伪随机数生成器out (Tensor, 可选) – 输出张量。
dtype (torch.dtype, 可选) – 如果为
None,此函数返回一个 dtype 为torch.int64的张量。layout (
torch.layout, 可选) – 返回的张量的所需布局。默认值:torch.strided。device (
torch.device, 可选) – 返回的张量的所需设备。默认值:如果为None,则使用当前设备作为默认张量类型(请参见torch.set_default_device())。对于 CPU 张量类型,device将是 CPU,对于 CUDA 张量类型,将是当前 CUDA 设备。requires_grad (bool, 可选) – 是否应记录返回的张量上的操作。默认值:
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]])