TUTORIALAI 与数据科学FETCHED

PyTorch torch.range 函数

PyTorch torch.range 函数 Pytorch torch 参考手册 torch.range 是 PyTorch 中用于创建包含 end 值的等差序列张量的函数。 函数定义 torch.range(start=0, end, step=1, dtype=None, device=None, requires_grad=False) 使用示例 实例 [mycode4 type='python&#..

更新于 2026/7/16

教程详情页默认 noindex。

PyTorch torch.range 函数


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

torch.range 是 PyTorch 中用于创建包含 end 值的等差序列张量的函数。

函数定义

torch.range(start=0, end, step=1, dtype=None, device=None, requires_grad=False)

使用示例

实例

import torch

# 创建从 0 到 5 的等差序列(包含 5)
x = torch.range(5)
print(x)

# 创建从 1 到 10,步长为 2 的等差序列
y = torch.range(1, 10, 2)
print(y)

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