Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.where function

PyTorch torch.where function


PyTorch torch reference manual 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

)

print

(

result

)

The output is:

tensor([[ 1, 20],

        [30,  4]])


PyTorch torch reference manual PyTorch torch reference manual