select() in PyTorch – DEV Community

select() in PyTorch – DEV Community


select() can get the 0D or more D view tensor of the zero or more elements selected from the 0D or more D tensor of zero or more elements as shown below:

import torch

my_tensor = torch.tensor([8, -3, 0, 1, 5, -2, -1, 4])

torch.select(input=my_tensor, dim=0, index=1)
my_tensor.select(dim=0, index=1)
torch.select(input=my_tensor, dim=0, index=-7)
torch.select(input=my_tensor, dim=-1, index=1)
torch.select(input=my_tensor, dim=-1, index=-7)
# tensor(-3)

torch.select(input=my_tensor, dim=0, index=4)
torch.select(input=my_tensor, dim=0, index=-4)
torch.select(input=my_tensor, dim=-1, index=4)
torch.select(input=my_tensor, dim=-1, index=-4)
# tensor(5)

my_tensor = torch.tensor([[8, -3, 0, 1],
                          [5, -2, -1, 4]])
torch.select(input=my_tensor, dim=0, index=1)
torch.select(input=my_tensor, dim=0, index=-1)
torch.select(input=my_tensor, dim=-2, index=1)
torch.select(input=my_tensor, dim=-2, index=-1)
# tensor([5, -2, -1, 4])

torch.select(input=my_tensor, dim=1, index=3)
torch.select(input=my_tensor, dim=1, index=-1)
torch.select(input=my_tensor, dim=-1, index=3)
torch.select(input=my_tensor, dim=-1, index=-1)
# tensor([1, 4])

my_tensor = torch.tensor([[[8, -3], [0, 1]],
                          [[5, -2], [-1, 4]]])
torch.select(input=my_tensor, dim=2, index=1)
torch.select(input=my_tensor, dim=2, index=-1)
torch.select(input=my_tensor, dim=-1, index=1)
torch.select(input=my_tensor, dim=-1, index=-1)
# tensor([[-3, 1],
#         [-2, 4]])

my_tensor = torch.tensor([[[8., -3.], [0., 1.]],
                          [[5., -2.], [-1., 4.]]])
torch.select(input=my_tensor, dim=2, index=1)
# tensor([[-3., 1.],
#         [-2., 4.]])

my_tensor = torch.tensor([[[8.+0.j, -3.+0.j], [0.+0.j, 1.+0.j]],
                          [[5.+0.j, -2.+0.j], [-1.+0.j, 4.+0.j]]])
torch.select(input=my_tensor, dim=2, index=1)
# tensor([[-3.+0.j, 1.+0.j],
#         [-2.+0.j, 4.+0.j]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.select(input=my_tensor, dim=2, index=1)
# tensor([[False, False],
#         [True, True]])
Enter fullscreen mode

Exit fullscreen mode



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *