本文源码基于版本1.0,交互界面基于0.4.1
import torch
按照指定轴上的坐标进行过滤
index_select()
沿着某tensor的一个轴dim筛选若干个坐标
>>> x = torch.randn(3, 4) # 目标矩阵
>>> x
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-0.4664, 0.2647, -0.1228, -1.1068],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> indices = torch.tensor([0, 2]) # 在轴上筛选坐标
>>> torch.index_select(x, dim=0, indices) # 指定筛选对象、轴、筛选坐标
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> torch.index_select(x, dim=1, indices)
tensor([[ 0.1427, -0.5414],
[-0.4664, -0.1228],
[-1.1734, 0.7230]])
where()
用于将两个broadcastable的tensor组合成新的tensor,类似于c++中的三元操作符“?:”
>>> x = torch.randn(3, 2)
>>> y = torch.ones(3, 2)