PyTorch torch.where function
PyTorch torch reference manual
torch.where A function in PyTorch that returns elements based on a condition.
Function definition
torch.where(condition, input, other)
Usage examples
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
)
(
result
)
The output is:
tensor([[ 1, 20],
[30, 4]])