官方解释
select(dim, index)
按index中选定的维度对tensor进行切片。
例如,
tensor.select(0, index)等价于tensor[index]
tensor.select(2, index)等价于tensor[:,:,index]
(个人理解,dim是几,等价时[]里就有几个冒号)
代码举例
>>> q = torch.rand(2, 3, 4)
>>> print(q)
tensor([[[0.1712, 0.0957, 0.8843, 0.0246],
[0.5592, 0.9146, 0.1219, 0.5181],
[0.7194, 0.8109, 0.8055, 0.1343]],
[[0.7044, 0.3359, 0.2135, 0.0129],
[0.1681, 0.0096, 0.9381, 0.2684],
[0.5321, 0.7533, 0.4190, 0.3163]]])
>>> print(q.select(0,0))
>>q.select(0,0)<=>q[0]
tensor([[0.1712, 0.0957, 0.8843, 0.0246],
[0.5592, 0.9146, 0.1219, 0.5181],
[0.7194, 0.8109, 0.8055, 0.1343]])
>>> print(q.select(1,0))
>>q.select(0,0)<=>q[:,0] 取每个二维张量的第一行
tensor([[0.1712, 0.0957, 0.8843, 0.0246],
[0.7044, 0.3359, 0.2135, 0.0129]])
>>> print(q.select(2,0))
>>q.select(0,0)<=>q[:,:,0] 取每个二维张量的所有行的第一个值
tensor([[0.1712, 0.5592, 0.7194],
[0.7044, 0.1681, 0.5321]])