TUTORIALAI 与数据科学FETCHED
PyTorch torch.roll 函数
PyTorch torch.roll 函数 Pytorch torch 参考手册 torch.roll 是 PyTorch 中用于滚动张量元素的函数。 函数定义 torch.roll(input, shifts, dims) 使用示例 实例 [mycode4 type='python'] import torch x = torch.arange(8) # 向前滚动 2 位 print(tor..
更新于 2026/7/16
教程详情页默认 noindex。
PyTorch torch.roll 函数
torch.roll 是 PyTorch 中用于滚动张量元素的函数。
函数定义
torch.roll(input, shifts, dims)
使用示例
实例
import torch
x = torch.arange(8)
# 向前滚动 2 位
print(torch.roll(x, 2))
# 向后滚动 2 位
print(torch.roll(x, -2))
x = torch.arange(8)
# 向前滚动 2 位
print(torch.roll(x, 2))
# 向后滚动 2 位
print(torch.roll(x, -2))
输出结果为:
tensor([6, 7, 0, 1, 2, 3, 4, 5]) tensor([2, 3, 4, 5, 6, 7, 0, 1])
Pytorch torch 参考手册