TensorFlow Add - Use TensorFlow's tf.add to add two Tensors together

TensorFlow Add - Use TensorFlow's tf.add to add two Tensors together

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 add two TensorFlow tensors together by using the tf.add operation.

In order to do that, we’re going to create two TensorFlow variables that will hold random numbers between 0 to 10 that are of the data type 32-bit signed integers.

Then we’re going to add them together.

We create the first one using the tf.get_variable operation and assign it to the Python variable random_int_var_one_ex.

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

Let’s print our random_int_var_one_ex Python variable to see what’s in it.

```
print(random_int_var_one_ex)
```

We can see that it is a tensor, it is named random_int_var_one, which is what we gave it, the shape is 2x3x4, and the data type is int32.

Now, let’s create our second TensorFlow variable the same way and assign it to the Python variable random_int_var_two_ex.

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

We can print our random_int_var_two_ex

```
print(random_int_var_two_ex)
```

And we see that it is also a tensor, it has a name, and the shape is 2x3x4, and the data type is int32.

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

So 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())
```

So we have two variables to initialize.

Let’s print our variables again.

We print out random_int_var_one_ex variable in a TensorFlow session to see its values.

```
print(sess.run(random_int_var_one_ex))
```

We see that it is a 2x3x4 tensor with numbers between 0 to 10 and they are all integers.

Let’s print our random_int_var_two_ex Python variable and we can see the values there.

```
print(sess.run(random_int_var_two_ex))
```

Now that we have these two tensors, we’ll now do the addition using the TensorFlow’s add operation.

So we are going to run the add operation inside the TensorFlow session and we use tf.add.

```
print(sess.run(tf.add(random_int_var_one_ex, random_int_var_two_ex)))
```

That’s the add operation.

We pass in our first tensor, random_int_var_one_ex and we pass in our second tensor, random_int_var_two_ex.

When we evaluate that, we can see that an addition has been done.

Note that the TensorFlow add operation does element-wise addition of the tensors.

So if we look at the second matrix of our first variable and the second matrix of our second variable, we should be able to do the addition ourselves visually.

So 9 + 2 = 11

2 + 4 = 6

6 + 0 = 6.

So all of the elements in the first tensor were added element-wise to all of the elements in the second tensor to get our tf.add result.

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

```
sess.close()
```

That is how you can add two TensorFlow tensors together by using the tf.add operation.

Receive the Data Science Weekly Newsletter every Thursday

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