YouTip LogoYouTip

Pytorch Torch Where

# PyTorch torch.where Function * * * [![Image 3: Pytorch torch Reference Manual](https://example.com/images/up.gif) Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html) `torch.where` is a function in PyTorch used to return elements based on a condition. ### Function Definition torch.where(condition, input, other) * * * ## Usage Example ## Example import torch condition = torch.tensor([[True,False],[False,True]]) x = torch.tensor([[1,2],[3,4]]) y = torch.tensor([[10,20],[30,40]]) result = torch.where(condition, x, y) print(result) Output: tensor([[ 1, 20], [30, 4]]) * * * [![Image 4: Pytorch torch Reference Manual](https://example.com/images/up.gif) Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html)
← Pytorch Torch ZerosPytorch Torch Vstack β†’