*Memos:
- My post explains select().
- My post explains index_select().
masked_select() can get the 1D tensor of the zero or more elements selected with zero or more masks from the 0D or more D tensor of zero or more elements as shown below:
*Memos:regularization
-
masked_select()
can be used with torch or a tensor. - The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). *It must be the 0D or more D tensor of zero or more elements. - The 2nd argument with
torch
or the 1st argument with a tensor ismask
(Required-Type:tensor
ofbool
). *It must be the 0D or more D tensor of zero or more boolean values. - There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
import torch
my_tensor = torch.tensor([8, -3, 0, 1, 5, -2])
torch.masked_select(input=my_tensor,
mask=torch.tensor([False, True, True, False, True, False]))
my_tensor.masked_select(
mask=torch.tensor([False, True, True, False, True, False]))
# tensor([-3, 0, 5])
torch.masked_select(input=my_tensor, mask=torch.tensor(True))
torch.masked_select(input=my_tensor,
mask=torch.tensor([True, True, True, True, True, True]))
# tensor([8, -3, 0, 1, 5, -2])
torch.masked_select(input=my_tensor, mask=torch.tensor(False))
torch.masked_select(input=my_tensor,
mask=torch.tensor([False, False, False, False, False, False]))
# tensor([], dtype=torch.int64)
my_tensor = torch.tensor([[8, -3, 0],
[1, 5, -2]])
torch.masked_select(input=my_tensor,
mask=torch.tensor([[False, True, True],
[False, True, False]]))
# tensor([-3, 0, 5])
torch.masked_select(input=my_tensor, mask=torch.tensor(True))
# tensor([8, -3, 0, 1, 5, -2])
torch.masked_select(input=my_tensor, mask=torch.tensor(False))
# tensor([], dtype=torch.int64)
my_tensor = torch.tensor([[[8], [-3], [0]],
[[1], [5], [-2]]])
torch.masked_select(input=my_tensor,
mask=torch.tensor([[[False], [True], [True]],
[[False], [True], [False]]]))
# tensor([-3, 0, 5])
torch.masked_select(input=my_tensor, mask=torch.tensor(True))
# tensor([8, -3, 0, 1, 5, -2])
torch.masked_select(input=my_tensor, mask=torch.tensor(False))
# tensor([], dtype=torch.int64)
my_tensor = torch.tensor([[[8.], [-3.], [0.]],
[[1.], [5.], [-2.]]])
torch.masked_select(input=my_tensor,
mask=torch.tensor([[[False], [True], [True]],
[[False], [True], [False]]]))
# tensor([-3., 0., 5.])
my_tensor = torch.tensor([[[8.+0.j], [-3.+0.j], [0.+0.j]],
[[1.+0.j], [5.+0.j], [-2.+0.j]]])
torch.masked_select(input=my_tensor,
mask=torch.tensor([[[False], [True], [True]],
[[False], [True], [False]]]))
# tensor([-3.+0.j, 0.+0.j, 5.+0.j])
my_tensor = torch.tensor([[[True], [False], [True]],
[[False], [True], [False]]])
torch.masked_select(input=my_tensor,
mask=torch.tensor([[[False], [True], [True]],
[[False], [True], [False]]]))
# tensor([False, True, True])
Top comments (0)