TUTORIALAI 与数据科学FETCHED

PyTorch torch.randint 函数

PyTorch torch.randint 函数 Pytorch torch 参考手册 torch.randint 是 PyTorch 中用于创建整数随机张量的函数。 函数定义 torch.randint(low, high, size, dtype, device, requires_grad) 使用示例 实例 [mycode4 type='python'] import torch # 创建..

更新于 2026/7/16

教程详情页默认 noindex。

PyTorch torch.randint 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.randint 是 PyTorch 中用于创建整数随机张量的函数。

函数定义

torch.randint(low, high, size, dtype, device, requires_grad)

使用示例

实例

import torch

# 创建 0 到 9 的随机整数张量
x = torch.randint(0, 10, (3, 4))

print(x)

输出结果为:

tensor([[4, 5, 2, 6],
        [7, 9, 3, 1],
        [2, 8, 5, 4]])

Pytorch torch 参考手册 Pytorch torch 参考手册