TensorFlow Element Wise Multiply of Tensors to get the Hadamard product

TensorFlow Element Wise Multiply of Tensors to get the Hadamard product

We start by importing 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 calculate the element-wise Hadamard multiplication of two TensorFlow tensors by using tf.multiply.

First, let’s create two tensors we can use to calculate an element-wise multiplication.

The first tensor we create will be a tensor shaped 2x3x4 with minimum values of zero, max values of 20, the data type is going to be int32, and we give it the name random_int_var_0_to_20.

```
random_int_var_one = tf.get_variable("random_int_var_0_to_20",
initializer=tf.random_uniform([2, 3, 4],
minval=0,
maxval=20,
dtype=tf.int32))
```

We use tf.get_variable operation and we assign it to the Python variable random_int_var_one.

Next, let’s print out what this random_int_var_one Python variable has.

```
print(random_int_var_one)
```

We see that it is a TensorFlow tensor, it has the name that we gave it, the shape is 2x3x4, and the data type is int32.

Because we haven’t run it in a TensorFlow session, right now it doesn’t have any values.

The same will apply to the other tensor we’re about to create.

This tensor will also be a TensorFlow tensor shaped 2x3x4.

```
random_int_var_two = tf.get_variable("random_int_var_0_to_2",
initializer=tf.random_uniform([2, 3, 4],
minval=0,
maxval=2,
dtype=tf.int32))
```

However, it will have minimum values of zero and max values of two, the data type will be int32, and we give it the name random_int_var_0_to_2, we assign it to the Python variable random_int_var_two.

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

```
print(random_int_var_two)
```

We see again that it’s a TensorFlow tensor, we have the name that we gave it, the shape is 2x3x4, and the data type is int32.

Now that we have created our TensorFlow tensors, it’s time to run the computational graph.

We launch the graph in a TensorFlow session

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

Then we initialize all the global variables in the graph.

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

In this case, we have two variables to initialize.

Let’s now print our two tensors to see what we have before we do the element-wise multiplication to get the Hadamard product.

```
print(sess.run(random_int_var_one))
```

First, we print our random_int_var_one Python variable in a TensorFlow session and we see that we have a tensor that is 2x3x4 and all the numbers are between zero and 20.

They are also all integers which you can visually check by seeing that none of the numbers have decimal points.

Next, we print our other Python variable we’re using in this example.

```
print(sess.run(random_int_var_two))
```

So we do a print(sess.run(random_int_var_two)), and we see that we have a 2x3x4 tensor, all the numbers are either zero or one.

The reason that we did a min of zero and a max of two is so that this tensor will be full of ones and zeros.

This makes it very easy to check element-wise multiplication.

All right, let’s now do the element-wise multiplication using the tf.multiply operation.

So we use tf.multiply:

```
tf_hadamard_prod = tf.multiply(random_int_var_one, random_int_var_two)
```

We pass in our first tensor random_int_var_one, then we pass in our second tensor random_int_var_two, and we’re using tf.multiply, and we’re going to assign it to the Python variable tf_hadamard_prod.

Let’s now check our result.

So we do a print(sess.run) and the Python variable we just created.

```
print(sess.run(tf_hadamard_prod))
```

So the first thing we check is that we have a 2x3x4 tensor to make sure that the shape remains the same.

It is 2x3x4.

The second thing we do is we can do a visual inspection of the results to make sure it was an element-wise multiplication.

So we’re going to start with our tensor full of zeros and ones because it’s easier that way.

0x2=0, 0x19=0, 1x18=18, 0x9=0.

Rather than check all the rows, we’ll check this final row of the second matrix.

1x8=8, 1x5=5, 0x12=0, 0x17=0.

Perfect - we were able to do an element-wise multiplication of our two tensors.

That is how you calculate the element-wise Hadamard multiplication of two TensorFlow tensors by using tf.multiply.

Receive the Data Science Weekly Newsletter every Thursday

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