TensorFlow tf.add_n() Vs tf.reduce_sum(): A Beginner Guide – TensorFlow Tutorial

By | August 29, 2020

TensorFlow tf.add_n() and tf.reduce_sum() can add tensors. However, there are some differences between them. In this tutorial, we will discuss this topic.

Compare tf.add_n() and tf.reduce_sum()

(1) tf.add_n() and tf.reduce_sum() can add a list of tensors.

Understand tf.add_n(): Add a List of Tensors

Here we will use an example to introduce this point.

import tensorflow as tf
import numpy as np

x1 = tf.convert_to_tensor(np.array([[1, 2, 3],[2, 3, 4]]), dtype = tf.float32)
z1 = tf.unstack(x1)

First, we create a x1 with the shape (2, 3), then we use tf.unstack() to split it to z1, which is a list of tensors.

z1 is

[<tf.Tensor 'unstack:0' shape=(3,) dtype=float32>, <tf.Tensor 'unstack:1' shape=(3,) dtype=float32>]

Add z1 using tf.add_n() and tf.reduce_sum()

y1 = tf.add_n(z1 )
y2 = tf.reduce_sum(z1, axis = 0)

You shoud notice that we should add z1 on axis = 0 using tf.reduce_sum(). Otherwise, tf.reduce_sum() will add all numbers in z1.

Output y1 and y2, you can get:

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(y1))
    print(sess.run(y2))

y1 and y2 is:

[3. 5. 7.]
[3. 5. 7.]

(2) tf.add_n() can not add a tensor, however, tf.reduce_sum() can add.

For example:

y1 = tf.add_n(x1)

x1 is a tensor, not a list of tensors.

Run this code, you will get this error:

ypeError: Using a `tf.Tensor` as a Python `bool` is not allowed.

However, tf.reduce_sum() can add x1.

Here is an example:

y1 = tf.reduce_sum(x1)
y2 = tf.reduce_sum(x1, axis = 0)
y3 = tf.reduce_sum(x1, axis = 1)

Run this code, you will find y1, y2 and y3 is:

15.0
[3. 5. 7.]
[6. 9.]

Leave a Reply