Buy Me a Coffee☕
*My post explains all().
any() can check if any elements of a 0D or more D tensor are True, getting the 0D or more D tensor of zero or more elements as shown below:
*Memos:
import torch my_tensor = torch.tensor(True) torch.any(input=my_tensor) my_tensor.any() torch.any(input=my_tensor, dim=0) torch.any(input=my_tensor, dim=-1) torch.any(input=my_tensor, dim=(0,)) torch.any(input=my_tensor, dim=(-1,)) # tensor(True) torch.any(input=my_tensor, dim=0, keepdim=True) # tensor(True) my_tensor = torch.tensor([True, False, True, False]) torch.any(input=my_tensor) torch.any(input=my_tensor, dim=0) torch.any(input=my_tensor, dim=-1) torch.any(input=my_tensor, dim=(0,)) torch.any(input=my_tensor, dim=(-1,)) # tensor(True) torch.any(input=my_tensor, dim=0, keepdim=True) # tensor([True]) my_tensor = torch.tensor([[True, False, True, False], [True, False, True, False]]) torch.any(input=my_tensor) torch.any(input=my_tensor, dim=(0, 1)) torch.any(input=my_tensor, dim=(0, -1)) torch.any(input=my_tensor, dim=(1, 0)) torch.any(input=my_tensor, dim=(1, -2)) torch.any(input=my_tensor, dim=(-1, 0)) torch.any(input=my_tensor, dim=(-1, -2)) torch.any(input=my_tensor, dim=(-2, 1)) torch.any(input=my_tensor, dim=(-2, -1)) # tensor(True) torch.any(input=my_tensor, dim=0) torch.any(input=my_tensor, dim=(0,)) torch.any(input=my_tensor, dim=-2) # tensor([True, False, True, False]) torch.any(input=my_tensor, dim=1) torch.any(input=my_tensor, dim=-1) torch.any(input=my_tensor, dim=(-1,)) # tensor([True, True]) torch.any(input=my_tensor, dim=0, keepdim=True) # tensor([[True, False, True, False]]) my_tensor = torch.tensor([[0, 1, 2, 3], [4, 5, 6, 7]]) torch.any(input=my_tensor) # tensor(True) my_tensor = torch.tensor([[0., 1., 2., 3.], [4., 5., 6., 7.]]) torch.any(input=my_tensor) # tensor(True) my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j], [4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]]) torch.any(input=my_tensor) # tensor(True) my_tensor = torch.tensor([[]]) torch.any(input=my_tensor) # tensor(False) torch.any(input=my_tensor, dim=0) torch.any(input=my_tensor, dim=-2) # tensor([], dtype=torch.bool) torch.any(input=my_tensor, dim=1) torch.any(input=my_tensor, dim=-1) # tensor([False])
The above is the detailed content of any in PyTorch. For more information, please follow other related articles on the PHP Chinese website!