TUTORIALAI 与数据科学FETCHED
PyTorch torch.index_select 函数
PyTorch torch.index_select 函数 Pytorch torch 参考手册 torch.index_select 是 PyTorch 中用于沿指定维度选择索引对应元素的函数。 函数定义 torch.index_select(input, dim, index) 使用示例 实例 [mycode4 type='python'] import torch x = torch.ra..
更新于 2026/7/16
教程详情页默认 noindex。
PyTorch torch.index_select 函数
torch.index_select 是 PyTorch 中用于沿指定维度选择索引对应元素的函数。
函数定义
torch.index_select(input, dim, index)
使用示例
实例
import torch
x = torch.randn(4, 5)
# 选择第 0 和第 2 行
indices = torch.tensor([0, 2])
result = torch.index_select(x, dim=0, index=indices)
print("结果形状:", result.shape)
x = torch.randn(4, 5)
# 选择第 0 和第 2 行
indices = torch.tensor([0, 2])
result = torch.index_select(x, dim=0, index=indices)
print("结果形状:", result.shape)
Pytorch torch 参考手册