TensorFlow Add: Add Two TensorFlow Tensors Together
TensorFlow Add - Use TensorFlow's tf.add to add two Tensors together
< > Code:
You must be a Member to view code
Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.
or Log In
We start by importing TensorFlow as tf.
import tensorflow as tf
Then we print out the TensorFlow version that we are using.
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.
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
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.
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.
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.
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.
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.
That is how you can add two TensorFlow tensors together by using the tf.add operation.