## TensorFlow Sum: Use tf.add_n To Sum List of Tensors

TensorFlow Sum - Use TensorFlow's add_n (tf.add_n) to sum list of Tensors

### < > 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

### Transcript:

We start by importing TensorFlow as tf.

```
import tensorflow as tf
```

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

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

We are using TensorFlow 1.0.1.

In this video, we’re going to sum a list of TensorFlow tensors using the tf.add_n operation so that you can add more than two TensorFlow tensors together at the same time.

All right, let’s get started.

We’re going to create three TensorFlow tensor variables that will each hold random numbers between 0 and 10 that are of the data type 32-bit signed integers.

```
random_int_var_one_ex = tf.get_variable("random_tf_var_one",
initializer=tf.random_uniform([2, 3, 4],
minval=0,
maxval=10,
dtype=tf.int32))
random_int_var_two_ex = tf.get_variable("random_tf_var_two",
initializer=tf.random_uniform([2, 3, 4],
minval=0,
maxval=10,
dtype=tf.int32))
random_int_var_tre_ex = tf.get_variable("random_tf_var_tre",
initializer=tf.random_uniform([2, 3, 4],
minval=0,
maxval=10,
dtype=tf.int32))
```

Note that we gave them the name of random_int_var_one_ex, random_int_var_two_ex, random_int_var_tre_ex.

They’re all constructed using the tf.get_variable operation and they’re assigned random_tf_var_one, random_tf_var_two, random_tf_var_tre.

The initializer uses the tf.random_uniform functionality with the min value of 0, max value of 10, and the data type is int32 for all of them.

Since they are all created the same way, let’s only look at the first one to see what happens when we print it.

```
print(random_int_var_one_ex)
```

We see that we have a TensorFlow tensor.

We can see the name, "random_tf_var_one".

We see that the shape is 2x3x4, and the data type is int32.

The other TensorFlow variables we created will be the same.

Now, let’s create a Python list data structure to hold these three tensors.

```
random_list = [random_int_var_one_ex, random_int_var_two_ex, random_int_var_tre_ex]
```

We assign it to the Python variable random_list.

We see the square bracket then we have our first variable, our second variable, and our third variable.

We’ll use the tf.add_n operation to add all the tensors in this list together.

Let’s now print this random list to see what we have.

```
print(random_list)
```

We can see that it’s a Python list of three TensorFlow variable objects.

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

Let’s launch the graph in a session.

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

Next, we initialize all the global variables in the graph.

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

Now, let’s print our random variables to see what’s inside.

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

We print random_int_var_one_ex in a TensorFlow session and we see that it is indeed a 2x3x4 tensor composed of integers between 0 and 10, and they are random numbers.

Next, we print random_int_var_two_ex in a TensorFlow session:

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

And again, we see the numbers that have been generated.

Lastly, we print the third random_int_var_tre_ex and we see the numbers generated there.

```
print(sess.run(random_int_var_tre_ex))
```

Next, let’s use the tf.add_n operation to add all the tensors together that were in our random_list Python variable.

```
random_sum = tf.add_n(random_list)
```

We use tf.add_n, we pass in the random_list variable, and we assign it to the Python variable random_sum.

Then we print the sum in a TensorFlow session and you can see the result.

```
print(sess.run(random_sum))
```

Now, let’s double check those results versus manually adding the last row of the second matrix of each tensor.

So what we’re looking for is the number 5.

So we have 0+1+4 is 5.

That’s correct.

For 19, we see 8+5 is 13 + 6 is 19.

Next, we’re looking for a 22.

We see 6+7 is 13 + 9 is 22.

Lastly, for the last one, we have 6+5 is 11 + 9 is 20.

Perfect.

It worked, we were able to produce a single tensor containing the sum of three tensors that were the same size and shape.

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

```
sess.close()
```

That is how you sum a list of TensorFlow tensors using the tf.add_n operation so that you can add more than two TensorFlow tensors together at the same time.