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

PyTorch torch.equal function

PyTorch torch.equal function


PyTorch torch reference manual PyTorch torch reference manual

torch.equal is a PyTorch function for checking whether two tensors are equal. It returns True if two tensors have the same shape and elements.

Function definition

torch.equal(input, other)


Usage examples

Example

import

torch

Create two identical tensors

x

=

torch.

tensor

(

[

1

,

2

,

3

]

)

y

=

torch.

tensor

(

[

1

,

2

,

3

]

)

Check whether they are equal

result

=

torch.

equal

(

x

,

y

)

print

(

result

)

Create different tensors

a

=

torch.

tensor

(

[

1

,

2

,

4

]

)

b

=

torch.

tensor

(

[

1

,

2

,

3

]

)

result2

=

torch.

equal

(

a

,

b

)

print

(

result2

)

The output is:

True

False


PyTorch torch reference manual PyTorch torch reference manual