TensorFlow tf.add_n() function can allow us to add a list of tensors. In this tutorial, we will introduce how to use this function using some examples.
Syntax
tf.add_n( inputs, name=None )
Add a list of tensors and return a tensor.
Parameter
inputs: A list of tensors
How to get a list of tensors?
There are two ways to get a list of tensors:
(1) Use python list comprehension
Understand Python List Comprehension for Beginners – Python Tutorial
For example:
lossL2 = tf.add_n([tf.nn.l2_loss(v) for v in vars if v.name not in ['bias', 'gamma', 'b', 'g', 'beta']]) * l2_coef
(2) Use tf.split(), tf.unstack() function and so on. These tensorflow function will return a list of tensors.
TensorFlow tf.split(): Splits a Tensor into Sub Tensors
How to use tf.add_n() in tensorflow?
We will use an example to show you how to use it.
Create a tensor with the shape (2, 3)
import tensorflow as tf import numpy as np x1 = tf.convert_to_tensor(np.array([[1, 2, 3],[2, 3, 4]]), dtype = tf.float32)
x1 is a (2, 3) tensor.
Split x1 to a list of tensors
z1 = tf.unstack(x1)
z1 will be [tensor(1, 2, 3), tensor([2, 3, 4])]
Add z1 using tf.add_n()
y1 = tf.add_n(z1) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print(sess.run(y1))
Run this code, y1 will be:
tensor([3, 5, 7])