Check for element wise equality between two PyTorch tensors using the PyTorch eq equality comparison operation

Check for element wise equality between two PyTorch tensors using the PyTorch eq equality comparison operation

This video will show you how to check for element-wise equality between two PyTorch tensors using the PyTorch eq equality comparison operation.

First, we import PyTorch.

```
import torch
```

Then we print the PyTorch version we are using.

```
print(torch.__version__)
```

We are using PyTorch 0.3.1.post2.

Let's create our first tensor using the PyTorch ones operation.

```
pt_tensor_ex_one = torch.ones(2, 3, 3)
```

So use torch.ones, and we want it to be 2x3x3, and we're going to assign it to the Python variable, pt_tensor_ex_one.

Let's now print the pt_tensor_ex_one Python variable to see what we have.

```
print(pt_tensor_ex_one)
```

Now, we see that we have two matrices, each of which is 3x3, and it's all ones, and it's a PyTorch FloatTensor

Next, let's manually create our second tensor using the PyTorch FloatTensor operation.

```
pt_tensor_ex_two = torch.FloatTensor(
[
[
[5, 1, 1],
[5, 1, 1],
[5, 1, 1]
]
,
[
[1, 1, 8],
[1, 1, 8],
[1, 1, 8]
]
])
```

So we say torch.FloatTensor, and then we're going to pass in our construction.

We can see that it is made up of two 3x3 matrices.

The first column is fives, then ones, ones.

Then the second matrix, it's column of ones, column of ones, and column of eights.

We construct this tensor this way so that it's easy to visually do a manual element-wise equality comparison.

Next, let's print the pt_tensor_ex_two Python variable that we just created to see what it shows up as.

```
print(pt_tensor_ex_two)
```

We see that it's a torch.FloatTensor of size 2x3x3.

The first column is five in the first matrix, and the second matrix the last column is eights.

Now, let's do the element-wise equality comparison between pt_tensor_ex_one and pt_tensor_ex_two Python variables using the PyTorch eq operation.

```
equality_tensor_ex = pt_tensor_ex_one.eq(pt_tensor_ex_two)
```

Then we're going to set the result equal to the Python variable, equality_tensor_ex.

Let's print the equality_tensor_ex Python variable to see what we have.

```
print(equality_tensor_ex)
```

We see that we have a tensor that's two matrices, where each matrix has three rows and three columns.

Each matrix is full of ones and zeros.

In this case, the ones represent the true Boolean value, and the zeros represent the false Boolean value.

Doing the visual manual check, we see that the first column of the first matrix is all zeros, which is as we expect because the number one is not equal to the number five.

Remember, we had ones for all pt_tensor_ex_one, and then we had fives in the first column so we see zeros here.

The rest of the columns are all ones because we had ones in the second column and ones in the third column for both matrices.

In the second matrix, we see that the third column is all zeros, which is what we expect because the number one is not equal to the number eight.

Remember that the second example Python variable had a column of eights in the second matrix, whereas the first one had a column of ones.

So eights are not equal to ones, so we get a column of zeros.

Perfect!

We were able to check for element-wise equality between two PyTorch tensors using the PyTorch eq equality comparison operation.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.