# TensorFlow Equal: Compare Two Tensors Element Wise

TensorFlow Equal - Compare two tensors element wise for equality

TensorFlow Equal - Compare two tensors element wise for equality

## Video Transcript

We import TensorFlow as tf.

``````import tensorflow as tf
``````

Then we print out the TensorFlow version that we are using.

``````print(tf.__version__)
``````

We are using TensorFlow 1.0.1.

In this video, we’re going to check for element-wise equality between two TensorFlow tensors by using the tf.equal operator.

To demonstrate this, we’re going to create two TensorFlow constant tensors and do a comparison between them.

All right, let’s get started.

First, let’s create our first constant tensor using TensorFlow’s constant operation.

``````tf_constant_ex_one = tf.constant(
[
[
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]
]
,
[
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]
]
]
)
``````

It’s going to be a 2x3x4 tensor.

The first matrix is full of ones that are all integers.

The second matrix is full of twos that are all integers.

We assign it to the Python variable tf_constant_ex_one.

Let’s print out the tf_constant_ex_one Python variable to see what we have.

``````print(tf_constant_ex_one)
``````

We see that it is a TensorFlow tensor.

We see that the shape is 2x3x4, and the data type has been inferred to be int32.

TensorFlow gave it a name.

Let’s now create our second TensorFlow constant using the tf.constant operation.

``````tf_constant_ex_two = tf.constant(
[
[
[1, 3, 1, 3],
[1, 3, 1, 3],
[1, 3, 1, 3]
]
,
[
[4, 2, 4, 2],
[4, 2, 4, 2],
[4, 2, 4, 2]
]
]
)
``````

We see that it is a 2x3x4 tensor.

This time, the first matrix rather than being all ones is ones, then a column of threes, then ones, then a column of threes.

The second matrix is a column of fours, then twos, then fours, then twos.

So scrolling up a little bit, we see that the second matrix of the first constant was all twos.

This one is a combination of columns of fours and columns of twos.

So when we compare them, we should expect that two will not be equal to four.

This column of twos will be equal to this column of twos.

This column of twos will not be equal to this column of fours.

And this column of twos will be equal to this column of twos.

We assign this tf.constant to the Python variable tf_constant_ex_two.

Let’s print out the tf_constant_ex_two Python variable and see what we have.

``````print(tf_constant_ex_two)
``````

We see that it’s a TensorFlow tensor.

TensorFlow gave it a name.

The shape is 2x3x4, and the data type is int32.

We don’t see any values yet for either of these two constants because we haven’t run them in a TensorFlow session.

Just to see what happens, let’s try the tf.equal operation using our two constants, the tf_constant_ex_one and tf_constant_ex_two.

``````tf.equal(tf_constant_ex_one, tf_constant_ex_two)
``````

Because this isn’t being run in a TensorFlow session, what it returns is a TensorFlow tensor with the name of Equal, the shape is 2x3x4 (Again, remember that tf.equal operation does element-wise so it’s not going to change the shape of the two tensors that we are comparing.), and the data type now is bool which stands for Boolean, so it’ll be either True or False.

However, we don’t see the actual Boolean values because we haven’t run it in a TensorFlow session.

So now that we’ve created the TensorFlow constants, it’s time to run the computational graph.

We launch the graph in a session.

``````sess = tf.Session()
``````

Then we initialize all the global variables in the graph.

``````sess.run(tf.global_variables_initializer())
``````

Let’s now print our tf_constant_ex_one Python variable in the TensorFlow session to see what’s inside.

``````print(sess.run(tf_constant_ex_one))
``````

We see that it is indeed a 2x3x4 tensor.

All the values are ones in the first matrix.

All the values are two in the second matrix, and the data type looks to be integers because we don’t see any decimal points.

Next, let’s print our tf_constant_ex_two Python variable in a TensorFlow session to see what’s inside.

``````print(sess.run(tf_constant_ex_two))
``````

The first matrix is a column of ones, then a column of threes, then a column of ones, and a column of threes.

The second matrix is a column of fours, a column of twos, a column of fours, a column of twos, which is exactly what we expect.

Now, let’s do the element-wise comparison between the two TensorFlow tensors using the tf.equal operation, this time within a TensorFlow session.

``````print(sess.run(tf.equal(tf_constant_ex_one, tf_constant_ex_two)))
``````

When we do that, we see that we get back a tensor that is 2x3x4 which is what we would expect given we’re comparing two tensors that have that shape, and the results are Booleans, all of them are either True or False, and we can actually double check.

So checking the first matrix, row of ones, compared to a row of ones, each element.

One is equal to one, True.

One is equal to one, True.

One is equal to one, True.

The second column, we’re comparing a row of ones versus a row of threes.

So we know that those are all False.

The same for the third column of each one and the fourth column of the first matrix.

Comparing the second matrix, we have two versus four.

Those are not equal.

Two compared to two, those are all True.

Two compared to four, those are False.

And two compared to two, those are all True.

Perfect - we were able to test the element-wise equality of two TensorFlow tensors.

Finally, we close the TensorFlow session to release the TensorFlow resources used within the session.

``````sess.close()
``````

That is how you check for element-wise equality between two TensorFlow tensors by using the tf.equal operator.

Receive the Data Science Weekly Newsletter every Thursday

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