select() in PyTorch

Buy Me a Coffee☕

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:

*Memos:

select() can be used with torch or a tensor.
The 1st argument(input)…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

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:

*Memos:

  • select() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 1st argument is dim(Required-Type:int).
  • The 3rd argument with torch or the 1st argument is index(Required-Type:int).
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]])


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-09-25T10:31:57+00:00) select() in PyTorch. Retrieved from https://www.scien.cx/2024/09/25/select-in-pytorch/

MLA
" » select() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Wednesday September 25, 2024, https://www.scien.cx/2024/09/25/select-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Wednesday September 25, 2024 » select() in PyTorch., viewed ,<https://www.scien.cx/2024/09/25/select-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » select() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/25/select-in-pytorch/
CHICAGO
" » select() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/09/25/select-in-pytorch/
IEEE
" » select() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/09/25/select-in-pytorch/. [Accessed: ]
rf:citation
» select() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/09/25/select-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.