tf.reduce_sum - Sum Along Axis Using TensorFlow reduce_sum

tf.reduce_sum - Sum Along Axis Using TensorFlow reduce_sum

We first 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 do a column sum in TensorFlow using tf.reduce_sum to get the sum of all the elements in the columns of a tensor.

All right, let’s get started.

First, let’s create a TensorFlow tensor variable that will hold random numbers between 0 to 10 that are of the data type int32.

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

We assign it to the Python variable random_int_var_one_ex.

Next, let’s print out the random_int_var_one_ex Python variable to see what we have.

```
print(random_int_var_one_ex)
```

We see that it’s a tensor that has the shape of 2x3x4, the data type is int32, and it is a TensorFlow tensor.

Because we haven’t run it in a TensorFlow session yet, we don’t see any values.

Now that we’ve created the TensorFlow variable, 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 TensorFlow variable in a TensorFlow session to see what’s inside.

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

So we do a print(sess.run(random_int_var_one_ex)) Python variable.

We see the data.

We see that it is indeed a 2x3x4 tensor.

We see that all the numbers are between 0 and 10 and they are integers.

We will now use the tf.reduce_sum in order to do a column sum.

That means we want to sum up each of the columns for each of the matrices.

And even though we know the shape of the tensor because we defined it using the initializer initially, let’s check it just in case.

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

So we use tf.shape and we pass our random_int_var_one_ex Python variable.

We see that the shape is in fact 2x3x4.

That means it’s two matrices that are three rows by four columns.

Since we want to do a column sum, we’ll have to tell the TensorFlow reduction operation which dimension we want to reduce across.

In this example, the first dimension is the number of matrices.

In this example, the second dimension is the number of rows.

In this example, the third dimension is the number of columns. When we do a column sum, it means that we want to reduce the sum across the rows so that we end up with one row that has the result of the summation down each column, which means that we’ll want to use the second dimension.

However, because Python is 0 indexed, it means that we’ll use the number 1 instead of the number 2.

Let’s now use the tf.reduce_sum operation on our random_int_var_one_ex Python variable across the second dimension.

```
print(sess.run(tf.reduce_sum(random_int_var_one_ex, 1)))
```

Again, we use a 1 here because Python is zero-indexed.

Perfect.

We end up with two vectors, both of which have the same number of columns as our original tensor.

One, two, three, four columns.

One, two, three, four.

One, two, three, four columns.

One, two, three, four.

Let’s now manually do a sum across a few columns to make sure the results make sense.

2+4 is 6 + 7 is 13.

We have 13 here.

2+3 is 5 + 5 is 10.

8+8 is 16.

4+2+3 is 9.

That’s what we get.

Perfect.

7+3+1 is 11.

0+1+2 is 3.

6+1+0 is 7.

2+6+6 is 14.

They worked.

Great.

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

```
sess.close()
```

That is how you do a column sum in TensorFlow using TensorFlow’s reduce sum operation to get the sum of all the elements in the columns of a tensor.

Receive the Data Science Weekly Newsletter every Thursday

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